Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sticky lbels first version #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,18 +33,32 @@ abstract class AbstractSynchronousInstrument<B extends AbstractBoundInstrument>
private final ConcurrentHashMap<Labels, B> boundLabels;
private final ReentrantLock collectLock;

private final Set<String> 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.
Expand Down Expand Up @@ -90,4 +108,24 @@ final List<MetricData> collectAll() {
}

abstract B newBinding(Batcher batcher);

abstract static class Builder<B extends AbstractSynchronousInstrument.Builder<?>> extends
AbstractInstrument.Builder<B> {


private final Set<String> stickyLabelsPrefixes;

public Set<String> getStickyLabelsPrefixes() {
return stickyLabelsPrefixes;
}

Builder(String name, MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState, MeterSdk meterSdk,
Set<String> stickyLabelsPrefixes) {
super(name, meterProviderSharedState, meterSharedState, meterSdk);
this.stickyLabelsPrefixes = stickyLabelsPrefixes;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> stickyLabelsPrefixes) {
return new AutoValue_MeterProviderSharedState(clock, resource, stickyLabelsPrefixes);
}

abstract Clock getClock();

abstract Resource getResource();

abstract Set<String> stickyLabelsPrefixes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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<String> stickyLabelsPrefixes) {
this.registry =
new MeterSdkComponentRegistry(
MeterProviderSharedState.create(clock, resource), new ViewRegistry());
MeterProviderSharedState.create(clock, resource, stickyLabelsPrefixes), new ViewRegistry());
this.metricProducer = new MetricProducerSdk(this.registry);
}

Expand Down Expand Up @@ -92,6 +95,7 @@ public static final class Builder {

private Clock clock = MillisClock.getInstance();
private Resource resource = Resource.getDefault();
private Set<String> stickyLabelsPrefixes = new HashSet<>();

private Builder() {}

Expand Down Expand Up @@ -119,13 +123,18 @@ public Builder setResource(@Nonnull Resource resource) {
return this;
}

public Builder setStickyLabelsPrefixes(Set<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down