Skip to content

Commit

Permalink
add UptimeGauge (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mweirauch authored Aug 23, 2016
1 parent ab1e35c commit a8aac98
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ A set of additional metrics complementing Dropwizards [metrics-jvm](https://gith
```java
metrics.register("jvm.native", new NativeMemoryUsageGaugeSet());
metrics.register("jvm.fds.count", new FileDescriptorCountGauge());
metrics.register("jvm.uptime", new UptimeGauge());
```

## Available Metrics
Expand All @@ -49,6 +50,10 @@ The `NativeMemoryUsageGaugeSet` reads values from `/proc/self/smaps`.

Provides the count of open file descriptors in use by the JVM process.

### UptimeGauge

Provides uptime in `ms`.

## Notes
* procfs data is cached for `100ms` in order to relief the filesystem pressure
when `Metric`s based on this data are queried by the registry one after
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright © 2016 Michael Weirauch ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.mweirauch.metrics.jvm.extras;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Objects;

import com.codahale.metrics.Gauge;

public class UptimeGauge implements Gauge<Long> {

private final RuntimeMXBean runtimeBean;

public UptimeGauge() {
this(ManagementFactory.getRuntimeMXBean());
}

public UptimeGauge(RuntimeMXBean runtimeBean) {
this.runtimeBean = Objects.requireNonNull(runtimeBean);
}

@Override
public Long getValue() {
return runtimeBean.getUptime();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2016 Michael Weirauch ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.mweirauch.metrics.jvm.extras;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import org.junit.Test;

import com.google.common.testing.NullPointerTester;
import com.google.common.testing.NullPointerTester.Visibility;

public class UptimeGaugeUnit0Test {

private final RuntimeMXBean runtimeBean = mock(RuntimeMXBean.class);

@Test
public void testNullContract() {
final UptimeGauge uut = new UptimeGauge(runtimeBean);

final NullPointerTester npt = new NullPointerTester();

npt.testConstructors(uut.getClass(), Visibility.PACKAGE);
npt.testStaticMethods(uut.getClass(), Visibility.PACKAGE);
npt.testInstanceMethods(uut, Visibility.PACKAGE);
}

@Test
public void testInstantiation() {
new UptimeGauge();
}

@Test
public void testGetValue() throws Exception {
final UptimeGauge uut = new UptimeGauge(runtimeBean);
when(runtimeBean.getUptime()).thenReturn(Long.valueOf(1337));

final Long value = uut.getValue();

assertNotNull(value);
assertEquals(Long.valueOf(1337), value);
}

@Test
public void testGetValueReal() throws Exception {
final UptimeGauge uut = new UptimeGauge(ManagementFactory.getRuntimeMXBean());
when(runtimeBean.getUptime()).thenReturn(Long.valueOf(1337));

final Long value = uut.getValue();

assertNotNull(value);
assertTrue(value > 0);
}

}

0 comments on commit a8aac98

Please sign in to comment.