From 954502a17b3df0eb48651e46f1ec55d4083ba621 Mon Sep 17 00:00:00 2001 From: Anton Polyakov Date: Tue, 29 Sep 2020 10:10:28 -0700 Subject: [PATCH] sticky lbels first version --- build.gradle | 2 +- .../AbstractSynchronousInstrument.java | 38 +++++++++++++++++++ .../sdk/metrics/MeterProviderSharedState.java | 10 ++++- .../sdk/metrics/MeterSdkProvider.java | 15 ++++++-- .../sdk/metrics/BatchRecorderSdkTest.java | 2 +- .../sdk/metrics/DoubleCounterSdkTest.java | 2 +- .../sdk/metrics/DoubleSumObserverSdkTest.java | 2 +- .../metrics/DoubleUpDownCounterSdkTest.java | 2 +- .../DoubleUpDownSumObserverSdkTest.java | 2 +- .../metrics/DoubleValueObserverSdkTest.java | 2 +- .../metrics/DoubleValueRecorderSdkTest.java | 2 +- .../sdk/metrics/LongCounterSdkTest.java | 2 +- .../sdk/metrics/LongSumObserverSdkTest.java | 2 +- .../sdk/metrics/LongUpDownCounterSdkTest.java | 2 +- .../metrics/LongUpDownSumObserverSdkTest.java | 2 +- .../sdk/metrics/LongValueObserverSdkTest.java | 2 +- .../sdk/metrics/LongValueRecorderSdkTest.java | 2 +- .../sdk/metrics/MeterSdkTest.java | 2 +- 18 files changed, 74 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index 8b33321cc1c..1df83dbb7ca 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ ext { subprojects { group = "io.opentelemetry" - version = "0.9.0-SNAPSHOT" // CURRENT_OPEN_TELEMETRY_VERSION + version = "0.9.0-modified-SNAPSHOT" // CURRENT_OPEN_TELEMETRY_VERSION plugins.withId("maven-publish") { // Always include the artifactory/bintray plugins to do the deployment. diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractSynchronousInstrument.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractSynchronousInstrument.java index fa8acef38ee..9b82ec43c46 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractSynchronousInstrument.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractSynchronousInstrument.java @@ -16,11 +16,15 @@ package io.opentelemetry.sdk.metrics; +import io.grpc.Context; import io.opentelemetry.common.Labels; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.sdk.metrics.data.MetricData; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; @@ -29,18 +33,32 @@ abstract class AbstractSynchronousInstrument private final ConcurrentHashMap boundLabels; private final ReentrantLock collectLock; + private final Set stickyLabelsPrefixes; + AbstractSynchronousInstrument( InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, MeterSharedState meterSharedState, ActiveBatcher activeBatcher) { super(descriptor, meterProviderSharedState, meterSharedState, activeBatcher); + this.stickyLabelsPrefixes = this.getMeterProviderSharedState().stickyLabelsPrefixes(); boundLabels = new ConcurrentHashMap<>(); collectLock = new ReentrantLock(); } + private Labels combineWithSticky(Labels labels) { + if (stickyLabelsPrefixes.isEmpty()) + return labels; + Labels.Builder builder = labels.toBuilder(); + CorrelationContext ctx = CorrelationsContextUtils.getCorrelationContext(Context.current()); + ctx.getEntries().stream().filter(e -> stickyLabelsPrefixes.contains(e.getKey().substring(0, e.getKey().indexOf('.')))) + .forEach(e -> builder.setLabel(e.getKey(), e.getValue())); + return builder.build(); + } + public B bind(Labels labels) { Objects.requireNonNull(labels, "labels"); + labels = combineWithSticky(labels); B binding = boundLabels.get(labels); if (binding != null && binding.bind()) { // At this moment it is guaranteed that the Bound is in the map and will not be removed. @@ -90,4 +108,24 @@ final List collectAll() { } abstract B newBinding(Batcher batcher); + + abstract static class Builder> extends + AbstractInstrument.Builder { + + + private final Set stickyLabelsPrefixes; + + public Set getStickyLabelsPrefixes() { + return stickyLabelsPrefixes; + } + + Builder(String name, MeterProviderSharedState meterProviderSharedState, + MeterSharedState meterSharedState, MeterSdk meterSdk, + Set stickyLabelsPrefixes) { + super(name, meterProviderSharedState, meterSharedState, meterSdk); + this.stickyLabelsPrefixes = stickyLabelsPrefixes; + } + + + } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterProviderSharedState.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterProviderSharedState.java index 7aebe9b9518..cffa42d239f 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterProviderSharedState.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterProviderSharedState.java @@ -20,15 +20,23 @@ import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.resources.Resource; import javax.annotation.concurrent.Immutable; +import java.util.HashSet; +import java.util.Set; @AutoValue @Immutable abstract class MeterProviderSharedState { static MeterProviderSharedState create(Clock clock, Resource resource) { - return new AutoValue_MeterProviderSharedState(clock, resource); + return new AutoValue_MeterProviderSharedState(clock, resource, new HashSet<>()); + } + + static MeterProviderSharedState create(Clock clock, Resource resource, Set stickyLabelsPrefixes) { + return new AutoValue_MeterProviderSharedState(clock, resource, stickyLabelsPrefixes); } abstract Clock getClock(); abstract Resource getResource(); + + abstract Set stickyLabelsPrefixes(); } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterSdkProvider.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterSdkProvider.java index 28c32888449..aed4e75e964 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterSdkProvider.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/MeterSdkProvider.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.metrics; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.metrics.MeterProvider; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -27,8 +28,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import javax.annotation.Nonnull; /** @@ -42,10 +45,10 @@ public final class MeterSdkProvider implements MeterProvider { private final MeterSdkComponentRegistry registry; private final MetricProducer metricProducer; - private MeterSdkProvider(Clock clock, Resource resource) { + private MeterSdkProvider(Clock clock, Resource resource, Set stickyLabelsPrefixes) { this.registry = new MeterSdkComponentRegistry( - MeterProviderSharedState.create(clock, resource), new ViewRegistry()); + MeterProviderSharedState.create(clock, resource, stickyLabelsPrefixes), new ViewRegistry()); this.metricProducer = new MetricProducerSdk(this.registry); } @@ -92,6 +95,7 @@ public static final class Builder { private Clock clock = MillisClock.getInstance(); private Resource resource = Resource.getDefault(); + private Set stickyLabelsPrefixes = new HashSet<>(); private Builder() {} @@ -119,13 +123,18 @@ public Builder setResource(@Nonnull Resource resource) { return this; } + public Builder setStickyLabelsPrefixes(Set stickyLabelsPrefixes) { + this.stickyLabelsPrefixes = ImmutableSet.copyOf(stickyLabelsPrefixes); + return this; + } + /** * Create a new TracerSdkFactory instance. * * @return An initialized TracerSdkFactory. */ public MeterSdkProvider build() { - return new MeterSdkProvider(clock, resource); + return new MeterSdkProvider(clock, resource, stickyLabelsPrefixes); } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java index 7d05b77c3c8..8f24b183819 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java @@ -42,7 +42,7 @@ class BatchRecorderSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void batchRecorder_badLabelSet() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java index 3d0631f11de..9bb8aebfba3 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java @@ -45,7 +45,7 @@ class DoubleCounterSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void add_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java index 9be84178587..b9596030e09 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java @@ -42,7 +42,7 @@ class DoubleSumObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java index 85d3266ff04..ed0475fabea 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java @@ -46,7 +46,7 @@ class DoubleUpDownCounterSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void add_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java index 64108ab3f15..e6ae43db36a 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java @@ -42,7 +42,7 @@ class DoubleUpDownSumObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java index cf7b74bac19..0a33a49d904 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java @@ -45,7 +45,7 @@ class DoubleValueObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java index e48be0d036b..3bd28cb8738 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java @@ -49,7 +49,7 @@ class DoubleValueRecorderSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void record_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java index a9afd15f98b..8b151dcc920 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java @@ -46,7 +46,7 @@ class LongCounterSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void add_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java index d51ecc6e505..8dfccd4be31 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java @@ -42,7 +42,7 @@ class LongSumObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java index 5d0de322c82..397e3e87b82 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java @@ -46,7 +46,7 @@ class LongUpDownCounterSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void add_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java index ac6049e0f21..35502800ae6 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java @@ -42,7 +42,7 @@ class LongUpDownSumObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java index 5ef0d0d3d16..92d695608e2 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java @@ -45,7 +45,7 @@ class LongValueObserverSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void collectMetrics_NoCallback() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java index e36a8563f9b..d6d84b9cf16 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java @@ -49,7 +49,7 @@ class LongValueRecorderSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void record_PreventNullLabels() { diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java index 033ff965382..5df4fa4a30a 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java @@ -48,7 +48,7 @@ class MeterSdkTest { private final MeterProviderSharedState meterProviderSharedState = MeterProviderSharedState.create(testClock, RESOURCE); private final MeterSdk testSdk = - new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry()); + new MeterSdk(meterProviderSharedState, INSTRUMENTATION_LIBRARY_INFO, new ViewRegistry(), stickyLabelsPrefixes); @Test void testLongCounter() {