Skip to content

Commit

Permalink
Don't register unsupported metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mweirauch authored Jan 20, 2023
1 parent 26d6cec commit 59ddd59
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017-2021 Michael Weirauch ([email protected])
* Copyright © 2017-2022 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.
Expand Down Expand Up @@ -40,6 +40,10 @@ public ProcessMemoryMetrics() {
public void bindTo(MeterRegistry registry) {
final KEY[] keys = { KEY.VSS, KEY.RSS, KEY.SWAP };
for (final KEY key : keys) {
if (status.get(key) == -1D) {
continue;
}

final String name = "process.memory." + key.name().toLowerCase(Locale.ENGLISH);
Gauge.builder(name, status, statusRef -> value(key)) //
.baseUnit("bytes") //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,26 @@ public ProcessThreadMetrics() {

@Override
public void bindTo(MeterRegistry registry) {
Gauge.builder("process.threads", status, statusRef -> value(KEY.THREADS)) //
.description("The number of process threads") //
.register(registry);

FunctionCounter.builder("process.threads.context.switches.voluntary", //
status, statusRef -> value(KEY.VOLUNTARY_CTXT_SWITCHES)) //
.description("Voluntary context switches") //
.register(registry);

FunctionCounter.builder("process.threads.context.switches.nonvoluntary", //
status, statusRef -> value(KEY.NONVOLUNTARY_CTXT_SWITCHES))//
.description("Non-voluntary context switches") //
.register(registry);
if (status.get(KEY.THREADS) != -1D) {
Gauge.builder("process.threads", //
status, statusRef -> value(KEY.THREADS)) //
.description("The number of process threads") //
.register(registry);
}

if (status.get(KEY.VOLUNTARY_CTXT_SWITCHES) != -1D) {
FunctionCounter.builder("process.threads.context.switches.voluntary", //
status, statusRef -> value(KEY.VOLUNTARY_CTXT_SWITCHES)) //
.description("Voluntary context switches") //
.register(registry);
}

if (status.get(KEY.NONVOLUNTARY_CTXT_SWITCHES) != -1D) {
FunctionCounter.builder("process.threads.context.switches.nonvoluntary", //
status, statusRef -> value(KEY.NONVOLUNTARY_CTXT_SWITCHES))//
.description("Non-voluntary context switches") //
.register(registry);
}
}

private Double value(KEY key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017-2021 Michael Weirauch ([email protected])
* Copyright © 2017-2022 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.
Expand Down Expand Up @@ -55,7 +55,7 @@ public void testInstantiation() {
}

@Test
public void testGetMetrics() throws Exception {
public void testGetMetrics() {
when(status.get(KEY.VSS)).thenReturn(1D);
when(status.get(KEY.RSS)).thenReturn(2D);
when(status.get(KEY.SWAP)).thenReturn(3D);
Expand All @@ -79,10 +79,27 @@ public void testGetMetrics() throws Exception {
assertEquals(3.0, swap.value(), 0.0);
assertEquals(expectedUnit, swap.getId().getBaseUnit());

verify(status, times(3)).get(any(KEY.class));
assertEquals(3, registry.getMeters().size());

verify(status, times(6)).get(any(KEY.class));
verifyNoMoreInteractions(status);
}

assertEquals(3, registry.getMeters().size());
@Test
public void testUnsupported() {
when(status.get(KEY.VSS)).thenReturn(-1D);
when(status.get(KEY.RSS)).thenReturn(-1D);
when(status.get(KEY.SWAP)).thenReturn(-1D);

final SimpleMeterRegistry registry = new SimpleMeterRegistry();
final ProcessMemoryMetrics uut = new ProcessMemoryMetrics(status);

uut.bindTo(registry);

assertEquals(0, registry.getMeters().size());

verify(status, times(3)).get(any(KEY.class));
verifyNoMoreInteractions(status);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testInstantiation() {
}

@Test
public void testGetMetrics() throws Exception {
public void testGetMetrics() {
when(status.get(KEY.THREADS)).thenReturn(7D);
when(status.get(KEY.VOLUNTARY_CTXT_SWITCHES)).thenReturn(4D);
when(status.get(KEY.NONVOLUNTARY_CTXT_SWITCHES)).thenReturn(5D);
Expand All @@ -72,10 +72,27 @@ public void testGetMetrics() throws Exception {
assertEquals(5.0, registry.get("process.threads.context.switches.nonvoluntary")
.functionCounter().count(), 0.0);

verify(status, times(3)).get(any(KEY.class));
assertEquals(3, registry.getMeters().size());

verify(status, times(6)).get(any(KEY.class));
verifyNoMoreInteractions(status);
}

assertEquals(3, registry.getMeters().size());
@Test
public void testUnsupported() {
when(status.get(KEY.THREADS)).thenReturn(-1D);
when(status.get(KEY.VOLUNTARY_CTXT_SWITCHES)).thenReturn(-1D);
when(status.get(KEY.NONVOLUNTARY_CTXT_SWITCHES)).thenReturn(-1D);

final SimpleMeterRegistry registry = new SimpleMeterRegistry();
final ProcessThreadMetrics uut = new ProcessThreadMetrics(status);

uut.bindTo(registry);

assertEquals(0, registry.getMeters().size());

verify(status, times(3)).get(any(KEY.class));
verifyNoMoreInteractions(status);
}

}

0 comments on commit 59ddd59

Please sign in to comment.