Change to use labels for async instruments (#1349)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2020-06-17 17:22:32 -07:00 committed by GitHub
parent 589adc0531
commit b734e366ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 29 additions and 31 deletions

View File

@ -16,6 +16,7 @@
package io.opentelemetry.metrics; package io.opentelemetry.metrics;
import io.opentelemetry.common.Labels;
import io.opentelemetry.metrics.AsynchronousInstrument.Result; import io.opentelemetry.metrics.AsynchronousInstrument.Result;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
@ -63,11 +64,11 @@ public interface AsynchronousInstrument<R extends Result> extends Instrument {
/** The result for the {@link Callback}. */ /** The result for the {@link Callback}. */
interface LongResult extends Result { interface LongResult extends Result {
void observe(long value, String... keyValueLabelPairs); void observe(long value, Labels labels);
} }
/** The result for the {@link Callback}. */ /** The result for the {@link Callback}. */
interface DoubleResult extends Result { interface DoubleResult extends Result {
void observe(double value, String... keyValueLabelPairs); void observe(double value, Labels labels);
} }
} }

View File

@ -17,6 +17,7 @@
package io.opentelemetry.extensions.metrics.runtime; package io.opentelemetry.extensions.metrics.runtime;
import io.opentelemetry.OpenTelemetry; import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.common.Labels;
import io.opentelemetry.metrics.AsynchronousInstrument.Callback; import io.opentelemetry.metrics.AsynchronousInstrument.Callback;
import io.opentelemetry.metrics.AsynchronousInstrument.LongResult; import io.opentelemetry.metrics.AsynchronousInstrument.LongResult;
import io.opentelemetry.metrics.LongSumObserver; import io.opentelemetry.metrics.LongSumObserver;
@ -61,10 +62,9 @@ public final class GarbageCollector {
.setDescription("Time spent in a given JVM garbage collector in milliseconds.") .setDescription("Time spent in a given JVM garbage collector in milliseconds.")
.setUnit("ms") .setUnit("ms")
.build(); .build();
final List<String[]> labelSets = new ArrayList<>(garbageCollectors.size()); final List<Labels> labelSets = new ArrayList<>(garbageCollectors.size());
for (final GarbageCollectorMXBean gc : garbageCollectors) { for (final GarbageCollectorMXBean gc : garbageCollectors) {
String[] label = {GC_LABEL_KEY, gc.getName()}; labelSets.add(Labels.of(GC_LABEL_KEY, gc.getName()));
labelSets.add(label);
} }
gcMetric.setCallback( gcMetric.setCallback(

View File

@ -17,6 +17,7 @@
package io.opentelemetry.extensions.metrics.runtime; package io.opentelemetry.extensions.metrics.runtime;
import io.opentelemetry.OpenTelemetry; import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.common.Labels;
import io.opentelemetry.metrics.AsynchronousInstrument.Callback; import io.opentelemetry.metrics.AsynchronousInstrument.Callback;
import io.opentelemetry.metrics.AsynchronousInstrument.LongResult; import io.opentelemetry.metrics.AsynchronousInstrument.LongResult;
import io.opentelemetry.metrics.LongSumObserver; import io.opentelemetry.metrics.LongSumObserver;
@ -75,14 +76,13 @@ public final class MemoryPools {
.setDescription("Bytes of a given JVM memory area.") .setDescription("Bytes of a given JVM memory area.")
.setUnit("By") .setUnit("By")
.build(); .build();
final String[] usedHeap = new String[] {TYPE_LABEL_KEY, USED, AREA_LABEL_KEY, HEAP}; final Labels usedHeap = Labels.of(TYPE_LABEL_KEY, USED, AREA_LABEL_KEY, HEAP);
final String[] usedNonHeap = new String[] {TYPE_LABEL_KEY, USED, AREA_LABEL_KEY, NON_HEAP}; final Labels usedNonHeap = Labels.of(TYPE_LABEL_KEY, USED, AREA_LABEL_KEY, NON_HEAP);
final String[] committedHeap = new String[] {TYPE_LABEL_KEY, COMMITTED, AREA_LABEL_KEY, HEAP}; final Labels committedHeap = Labels.of(TYPE_LABEL_KEY, COMMITTED, AREA_LABEL_KEY, HEAP);
final String[] committedNonHeap = final Labels committedNonHeap = Labels.of(TYPE_LABEL_KEY, COMMITTED, AREA_LABEL_KEY, NON_HEAP);
new String[] {TYPE_LABEL_KEY, COMMITTED, AREA_LABEL_KEY, NON_HEAP};
// TODO: Decide if max is needed or not. May be derived with some approximation from max(used). // TODO: Decide if max is needed or not. May be derived with some approximation from max(used).
final String[] maxHeap = new String[] {TYPE_LABEL_KEY, MAX, AREA_LABEL_KEY, HEAP}; final Labels maxHeap = Labels.of(TYPE_LABEL_KEY, MAX, AREA_LABEL_KEY, HEAP);
final String[] maxNonHeap = new String[] {TYPE_LABEL_KEY, MAX, AREA_LABEL_KEY, NON_HEAP}; final Labels maxNonHeap = Labels.of(TYPE_LABEL_KEY, MAX, AREA_LABEL_KEY, NON_HEAP);
areaMetric.setCallback( areaMetric.setCallback(
new LongSumObserver.Callback<LongResult>() { new LongSumObserver.Callback<LongResult>() {
@Override @Override
@ -107,14 +107,13 @@ public final class MemoryPools {
.setDescription("Bytes of a given JVM memory pool.") .setDescription("Bytes of a given JVM memory pool.")
.setUnit("By") .setUnit("By")
.build(); .build();
final List<String[]> usedLabelSets = new ArrayList<>(poolBeans.size()); final List<Labels> usedLabelSets = new ArrayList<>(poolBeans.size());
final List<String[]> committedLabelSets = new ArrayList<>(poolBeans.size()); final List<Labels> committedLabelSets = new ArrayList<>(poolBeans.size());
final List<String[]> maxLabelSets = new ArrayList<>(poolBeans.size()); final List<Labels> maxLabelSets = new ArrayList<>(poolBeans.size());
for (final MemoryPoolMXBean pool : poolBeans) { for (final MemoryPoolMXBean pool : poolBeans) {
usedLabelSets.add(new String[] {TYPE_LABEL_KEY, USED, POOL_LABEL_KEY, pool.getName()}); usedLabelSets.add(Labels.of(TYPE_LABEL_KEY, USED, POOL_LABEL_KEY, pool.getName()));
committedLabelSets.add( committedLabelSets.add(Labels.of(TYPE_LABEL_KEY, COMMITTED, POOL_LABEL_KEY, pool.getName()));
new String[] {TYPE_LABEL_KEY, COMMITTED, POOL_LABEL_KEY, pool.getName()}); maxLabelSets.add(Labels.of(TYPE_LABEL_KEY, MAX, POOL_LABEL_KEY, pool.getName()));
maxLabelSets.add(new String[] {TYPE_LABEL_KEY, MAX, POOL_LABEL_KEY, pool.getName()});
} }
poolMetric.setCallback( poolMetric.setCallback(
new Callback<LongResult>() { new Callback<LongResult>() {

View File

@ -96,11 +96,10 @@ abstract class AbstractAsynchronousInstrument<T extends AsynchronousInstrument.R
} }
@Override @Override
public void observe(long sum, String... keyValueLabelPairs) { public void observe(long sum, Labels labels) {
Aggregator aggregator = activeBatcher.getAggregator(); Aggregator aggregator = activeBatcher.getAggregator();
aggregator.recordLong(sum); aggregator.recordLong(sum);
activeBatcher.batch( activeBatcher.batch(labels, aggregator, /* mappedAggregator= */ false);
Labels.of(keyValueLabelPairs), aggregator, /* mappedAggregator= */ false);
} }
} }
} }
@ -129,11 +128,10 @@ abstract class AbstractAsynchronousInstrument<T extends AsynchronousInstrument.R
} }
@Override @Override
public void observe(double sum, String... keyValueLabelPairs) { public void observe(double sum, Labels labels) {
Aggregator aggregator = activeBatcher.getAggregator(); Aggregator aggregator = activeBatcher.getAggregator();
aggregator.recordDouble(sum); aggregator.recordDouble(sum);
activeBatcher.batch( activeBatcher.batch(labels, aggregator, /* mappedAggregator= */ false);
Labels.of(keyValueLabelPairs), aggregator, /* mappedAggregator= */ false);
} }
} }
} }

View File

@ -106,7 +106,7 @@ public class DoubleSumObserverSdkTest {
new Callback<DoubleResult>() { new Callback<DoubleResult>() {
@Override @Override
public void update(DoubleResult result) { public void update(DoubleResult result) {
result.observe(12.1d, "k", "v"); result.observe(12.1d, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);

View File

@ -106,7 +106,7 @@ public class DoubleUpDownSumObserverSdkTest {
new Callback<DoubleResult>() { new Callback<DoubleResult>() {
@Override @Override
public void update(DoubleResult result) { public void update(DoubleResult result) {
result.observe(12.1d, "k", "v"); result.observe(12.1d, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);

View File

@ -109,7 +109,7 @@ public class DoubleValueObserverSdkTest {
new Callback<DoubleResult>() { new Callback<DoubleResult>() {
@Override @Override
public void update(DoubleResult result) { public void update(DoubleResult result) {
result.observe(12.1d, "k", "v"); result.observe(12.1d, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);

View File

@ -105,7 +105,7 @@ public class LongSumObserverSdkTest {
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult result) { public void update(LongResult result) {
result.observe(12, "k", "v"); result.observe(12, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);

View File

@ -106,7 +106,7 @@ public class LongUpDownSumObserverSdkTest {
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult result) { public void update(LongResult result) {
result.observe(12, "k", "v"); result.observe(12, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);

View File

@ -109,7 +109,7 @@ public class LongValueObserverSdkTest {
new Callback<LongResult>() { new Callback<LongResult>() {
@Override @Override
public void update(LongResult result) { public void update(LongResult result) {
result.observe(12, "k", "v"); result.observe(12, Labels.of("k", "v"));
} }
}); });
testClock.advanceNanos(SECOND_NANOS); testClock.advanceNanos(SECOND_NANOS);