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

Prototype disabling scope across traces, metrics, and logs #6201

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
Comparing source compatibility of against
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
=== UNCHANGED METHOD: PUBLIC io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setEncoder(zipkin2.codec.BytesEncoder<zipkin2.Span><zipkin2.Span>)
+++ NEW ANNOTATION: java.lang.Deprecated
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setEncoder(zipkin2.reporter.BytesEncoder<zipkin2.Span>)
=== UNCHANGED METHOD: PUBLIC io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setSender(zipkin2.reporter.Sender)
+++ NEW ANNOTATION: java.lang.Deprecated
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder setSender(zipkin2.reporter.BytesMessageSender)
No changes.
10 changes: 9 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-common.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
+++* NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.common.ScopeConfig (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++* NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Function<io.opentelemetry.sdk.common.InstrumentationScopeInfo,T> applyToMatching(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, java.lang.Object)
GENERIC TEMPLATES: +++ T:java.lang.Object
+++* NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Function<io.opentelemetry.sdk.common.InstrumentationScopeInfo,T> applyToMatching(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, java.lang.Object, java.lang.Object)
GENERIC TEMPLATES: +++ T:java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> scopeNameEquals(java.lang.String)
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> scopeNameMatches(java.lang.String)
10 changes: 9 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.logs.LoggerConfig (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.logs.LoggerConfig defaultConfig()
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.logs.LoggerConfig disabled()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.logs.LoggerConfig)
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.MeterConfig (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.MeterConfig defaultConfig()
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.MeterConfig disabled()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.metrics.MeterConfig)
10 changes: 9 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.SdkTracerProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.trace.TracerConfig)
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.trace.TracerConfig (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.TracerConfig defaultConfig()
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.TracerConfig disabled()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()
216 changes: 216 additions & 0 deletions sdk/all/src/test/java/io/opentelemetry/sdk/ScopeConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk;

import static io.opentelemetry.sdk.common.ScopeConfig.applyToMatching;
import static io.opentelemetry.sdk.common.ScopeConfig.scopeNameEquals;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.LoggerConfig;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.metrics.MeterConfig;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.TracerConfig;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

class ScopeConfigTest {

private final InMemoryLogRecordExporter logRecordExporter = InMemoryLogRecordExporter.create();
private final InMemoryMetricReader metricReader = InMemoryMetricReader.create();
private final InMemorySpanExporter spanExporter = InMemorySpanExporter.create();

/**
* Disable "scopeB". All other scopes are enabled by default.
*/
@Test
void disableScopeB() {
OpenTelemetrySdk sdk =
OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
.setTracerConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), TracerConfig.disabled()))
.build())
.setMeterProvider(
SdkMeterProvider.builder()
.registerMetricReader(metricReader)
.setMeterConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), MeterConfig.disabled()))
.build())
.setLoggerProvider(
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
.setLoggerConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), LoggerConfig.disabled()))
.build())
.build();

simulateInstrumentation(sdk);

// Collect all the telemetry. Ensure we don't see any from scopeB, and that the telemetry from
// scopeA and scopeC is valid.
assertThat(spanExporter.getFinishedSpanItems())
.satisfies(
spans -> {
Map<InstrumentationScopeInfo, List<SpanData>> spansByScope =
spans.stream()
.collect(Collectors.groupingBy(SpanData::getInstrumentationScopeInfo));
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeA"))).hasSize(1);
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeB"))).isNull();
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeC"))).hasSize(1);
});
assertThat(metricReader.collectAllMetrics())
.satisfies(
metrics -> {
Map<InstrumentationScopeInfo, List<MetricData>> metricsByScope =
metrics.stream()
.collect(Collectors.groupingBy(MetricData::getInstrumentationScopeInfo));
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeA"))).hasSize(1);
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeB"))).isNull();
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeC"))).hasSize(1);
});
assertThat(logRecordExporter.getFinishedLogRecordItems())
.satisfies(
logs -> {
Map<InstrumentationScopeInfo, List<LogRecordData>> logsByScope =
logs.stream()
.collect(Collectors.groupingBy(LogRecordData::getInstrumentationScopeInfo));
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeA"))).hasSize(1);
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeB"))).isNull();
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeC"))).hasSize(1);
});
}

/**
* Disable all scopes by default and enable a single scope.
*/
@Test
void disableAllScopesExceptB() {
OpenTelemetrySdk sdk =
OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
.setTracerConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), TracerConfig.enabled(), TracerConfig.disabled()))
.build())
.setMeterProvider(
SdkMeterProvider.builder()
.registerMetricReader(metricReader)
.setMeterConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), MeterConfig.enabled(), MeterConfig.disabled()))
.build())
.setLoggerProvider(
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
.setLoggerConfigProvider(
applyToMatching(scopeNameEquals("scopeB"), LoggerConfig.enabled(), LoggerConfig.disabled()))
.build())
.build();

simulateInstrumentation(sdk);

// Collect all the telemetry. Ensure we only see telemetry from scopeB, since other scopes have been disabled by default.
assertThat(spanExporter.getFinishedSpanItems())
.satisfies(
spans -> {
Map<InstrumentationScopeInfo, List<SpanData>> spansByScope =
spans.stream()
.collect(Collectors.groupingBy(SpanData::getInstrumentationScopeInfo));
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeA"))).isNull();
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeB"))).hasSize(1);
assertThat(spansByScope.get(InstrumentationScopeInfo.create("scopeC"))).isNull();
});
assertThat(metricReader.collectAllMetrics())
.satisfies(
metrics -> {
Map<InstrumentationScopeInfo, List<MetricData>> metricsByScope =
metrics.stream()
.collect(Collectors.groupingBy(MetricData::getInstrumentationScopeInfo));
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeA"))).isNull();
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeB"))).hasSize(1);
assertThat(metricsByScope.get(InstrumentationScopeInfo.create("scopeC"))).isNull();
});
assertThat(logRecordExporter.getFinishedLogRecordItems())
.satisfies(
logs -> {
Map<InstrumentationScopeInfo, List<LogRecordData>> logsByScope =
logs.stream()
.collect(Collectors.groupingBy(LogRecordData::getInstrumentationScopeInfo));
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeA"))).isNull();
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeB"))).hasSize(1);
assertThat(logsByScope.get(InstrumentationScopeInfo.create("scopeC"))).isNull();
});
}


/**
* Emit spans, metrics and logs in a hierarchy of 3 scopes: scopeA -> scopeB -> scopeC. Exercise
* the scope config which is common across all signals.
*/
private static void simulateInstrumentation(OpenTelemetry openTelemetry) {
// Start scopeA
Tracer scopeATracer = openTelemetry.getTracer("scopeA");
Meter scopeAMeter = openTelemetry.getMeter("scopeA");
Logger scopeALogger = openTelemetry.getLogsBridge().get("scopeA");
Span spanA = scopeATracer.spanBuilder("spanA").startSpan();
try (Scope spanAScope = spanA.makeCurrent()) {
scopeALogger.logRecordBuilder().setBody("scopeA log message").emit();

// Start scopeB
Tracer scopeBTracer = openTelemetry.getTracer("scopeB");
Meter scopeBMeter = openTelemetry.getMeter("scopeB");
Logger scopeBLogger = openTelemetry.getLogsBridge().get("scopeB");
Span spanB = scopeBTracer.spanBuilder("spanB").startSpan();
try (Scope spanBScope = spanB.makeCurrent()) {
scopeBLogger.logRecordBuilder().setBody("scopeB log message").emit();

// Start scopeC
Tracer scopeCTracer = openTelemetry.getTracer("scopeC");
Meter scopeCMeter = openTelemetry.getMeter("scopeC");
Logger scopeCLogger = openTelemetry.getLogsBridge().get("scopeC");
Span spanC = scopeCTracer.spanBuilder("spanC").startSpan();
try (Scope spanCScope = spanB.makeCurrent()) {
scopeCLogger.logRecordBuilder().setBody("scopeC log message").emit();
} finally {
spanC.end();
scopeCMeter.counterBuilder("scopeCCounter").build().add(1);
}
// End scopeC

} finally {
spanB.end();
scopeBMeter.counterBuilder("scopeBCounter").build().add(1);
}
// End scopeB

} finally {
spanA.end();
scopeAMeter.counterBuilder("scopeACounter").build().add(1);
}
// End scopeA
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.common;

import io.opentelemetry.sdk.internal.GlobUtil;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Utilities for configuring scopes.
*/
public final class ScopeConfig {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the various Provider interfaces, we never call it Scope independently, but always instrumentationScope. Consider following that same convention here to avoid confusion from other types of scope.


/**
* Returns a function which returns {@code matchingConfig} to scopes which match the {@code scopeMatcher}. If a scope does match, returns null, which triggers the default behavior.
*
* <p>See {@link #scopeNameEquals(String)}, {@link #scopeNameMatches(String)} for helper functions for {@code scopeMatcher}.
*/
public static <T> Function<InstrumentationScopeInfo, T> applyToMatching(
Predicate<InstrumentationScopeInfo> scopeMatcher, T matchingConfig) {
return scopeInfo -> scopeMatcher.test(scopeInfo) ? matchingConfig : null;
}

/**
* Returns a function which returns {@code matchingConfig} to scopes which match the {@code scopeMatcher}, else returns {@code defaultConfig}. This is useful for overriding the default behavior. For example, you can disable by default and selectively enable select scopes.
*
* <p>See {@link #scopeNameEquals(String)}, {@link #scopeNameMatches(String)} for helper functions for {@code scopeMatcher}.
*/
public static <T> Function<InstrumentationScopeInfo, T> applyToMatching(
Predicate<InstrumentationScopeInfo> scopeMatcher, T matchingConfig, T defaultConfig) {
return scopeInfo -> scopeMatcher.test(scopeInfo) ? matchingConfig : defaultConfig;
}

/**
* Returns a predicate which returns {@code true} if the {@link InstrumentationScopeInfo#getName()} is an exact match of {@code targetScopeName}.
*/
public static Predicate<InstrumentationScopeInfo> scopeNameEquals(String scopeName) {
return scopeInfo -> scopeInfo.getName().equals(scopeName);
}

/**
* Returns a predicate which returns {@code true} if the {@link InstrumentationScopeInfo#getName()} is a wildcard match of the {@code scopeNameGlobPattern}.
*
* <p>{@code scopeNameGlobPattern} name may contain the wildcard characters {@code *} and {@code ?} with the following matching criteria:
*
* <ul>
* <li>{@code *} matches 0 or more instances of any character
* <li>{@code ?} matches exactly one instance of any character
* </ul>
*/
public static Predicate<InstrumentationScopeInfo> scopeNameMatches(
String scopeNameGlobPattern) {
Predicate<String> globPredicate = GlobUtil.toGlobPatternPredicate(scopeNameGlobPattern);
return scopeInfo -> globPredicate.test(scopeInfo.getName());
}

private ScopeConfig() {}
}
Loading
Loading