Skip to content

Commit

Permalink
use double counter instead of gauge in FunctionTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Jan 5, 2022
1 parent 6f47322 commit 4c91065
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,7 @@ <T> void buildGauge(
@Nullable T obj,
ToDoubleFunction<T> objMetric) {

buildGauge(
meterId.getName(), description(meterId), baseUnit(meterId), attributes, obj, objMetric);
}

<T> void buildGauge(
String name,
String description,
String baseUnit,
Attributes attributes,
@Nullable T obj,
ToDoubleFunction<T> objMetric) {

String name = meterId.getName();
synchronized (gauges) {
// use the gauges map as lock for the recorder state - this way all gauge-related mutable
// state will always be accessed in synchronized(gauges)
Expand All @@ -69,8 +58,8 @@ <T> void buildGauge(
new DoubleMeasurementsRecorder(recorderLock);
meter
.gaugeBuilder(name)
.setDescription(description)
.setUnit(baseUnit)
.setDescription(description(meterId))
.setUnit(baseUnit(meterId))
.buildWithCallback(recorderCallback);
return recorderCallback;
});
Expand All @@ -90,6 +79,17 @@ <T> void buildDoubleCounter(
Attributes attributes,
T obj,
ToDoubleFunction<T> objMetric) {
buildDoubleCounter(
meterId.getName(), description(meterId), baseUnit(meterId), attributes, obj, objMetric);
}

<T> void buildDoubleCounter(
String name,
String description,
String baseUnit,
Attributes attributes,
@Nullable T obj,
ToDoubleFunction<T> objMetric) {

synchronized (doubleCounters) {
// use the counters map as lock for the recorder state - this way all double counter-related
Expand All @@ -98,14 +98,14 @@ <T> void buildDoubleCounter(

DoubleMeasurementsRecorder recorder =
doubleCounters.computeIfAbsent(
meterId.getName(),
name,
n -> {
DoubleMeasurementsRecorder recorderCallback =
new DoubleMeasurementsRecorder(recorderLock);
meter
.counterBuilder(meterId.getName())
.setDescription(description(meterId))
.setUnit(baseUnit(meterId))
.counterBuilder(name)
.setDescription(description)
.setUnit(baseUnit)
.ofDoubles()
.buildWithCallback(recorderCallback);
return recorderCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class OpenTelemetryFunctionTimer<T> implements FunctionTimer, RemovableMet
asyncInstrumentRegistry.buildLongCounter(
countMeterName, description(id), /* baseUnit = */ "1", attributes, obj, countFunction);

asyncInstrumentRegistry.buildGauge(
asyncInstrumentRegistry.buildDoubleCounter(
totalTimeMeterName,
description(id),
/* baseUnit = */ "ms",
Expand Down Expand Up @@ -85,7 +85,7 @@ public Id getId() {
@Override
public void onRemove() {
asyncInstrumentRegistry.removeLongCounter(countMeterName, attributes);
asyncInstrumentRegistry.removeGauge(totalTimeMeterName, attributes);
asyncInstrumentRegistry.removeDoubleCounter(totalTimeMeterName, attributes);
}

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void testFunctionTimer() throws Exception {
assertThat(metric)
.hasDescription("This is a test function timer")
.hasUnit("ms")
.hasDoubleGauge()
.hasDoubleSum()
.points()
.satisfiesExactly(
point ->
Expand Down Expand Up @@ -124,7 +124,7 @@ void testNanoPrecision() {
metric ->
assertThat(metric)
.hasUnit("ms")
.hasDoubleGauge()
.hasDoubleSum()
.points()
.satisfiesExactly(
point -> assertThat(point).hasValue(1.234).attributes())));
Expand Down Expand Up @@ -167,7 +167,7 @@ void functionTimersWithSameNameAndDifferentTags() {
metric ->
assertThat(metric)
.hasUnit("ms")
.hasDoubleGauge()
.hasDoubleSum()
.points()
.anySatisfy(
point ->
Expand Down

0 comments on commit 4c91065

Please sign in to comment.