Skip to content

Commit 69c5c0c

Browse files
committed
Refactor for improved ergonomics
1 parent 66277f5 commit 69c5c0c

File tree

24 files changed

+635
-158
lines changed

24 files changed

+635
-158
lines changed
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
Comparing source compatibility of against
2-
+++* NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.common.ScopeConfig (not serializable)
2+
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.common.ScopeConfigurator (not serializable)
33
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
4+
GENERIC TEMPLATES: +++ T:java.lang.Object
5+
+++ NEW INTERFACE: java.util.function.Function
46
+++ NEW SUPERCLASS: java.lang.Object
5-
+++* 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)
7+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<T> builder()
68
GENERIC TEMPLATES: +++ T:java.lang.Object
7-
+++* 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)
8-
GENERIC TEMPLATES: +++ T:java.lang.Object
9-
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> scopeNameEquals(java.lang.String)
10-
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> scopeNameMatches(java.lang.String)
9+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<T> toBuilder()
10+
+++ NEW ANNOTATION: java.lang.FunctionalInterface
11+
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder (not serializable)
12+
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
13+
GENERIC TEMPLATES: +++ T:java.lang.Object
14+
+++ NEW SUPERCLASS: java.lang.Object
15+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<T> addCondition(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, java.lang.Object)
16+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.ScopeConfigurator<T> build()
17+
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> nameEquals(java.lang.String)
18+
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo> nameMatchesGlob(java.lang.String)
19+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<T> setDefault(java.lang.Object)

docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ Comparing source compatibility of against
22
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.logs.LoggerConfig (not serializable)
33
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
44
+++ NEW SUPERCLASS: java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<io.opentelemetry.sdk.logs.LoggerConfig> configuratorBuilder()
56
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.logs.LoggerConfig defaultConfig()
67
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.logs.LoggerConfig disabled()
8+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.logs.LoggerConfig enabled()
79
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()
810
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder (not serializable)
911
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
10-
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.logs.LoggerConfig)
12+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder addLoggerConfiguratorMatcher(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, io.opentelemetry.sdk.logs.LoggerConfig)
13+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder setLoggerConfigurator(io.opentelemetry.sdk.common.ScopeConfigurator<io.opentelemetry.sdk.logs.LoggerConfig>)

docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ Comparing source compatibility of against
22
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.MeterConfig (not serializable)
33
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
44
+++ NEW SUPERCLASS: java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<io.opentelemetry.sdk.metrics.MeterConfig> configuratorBuilder()
56
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.MeterConfig defaultConfig()
67
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.MeterConfig disabled()
8+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.MeterConfig enabled()
79
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()
810
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder (not serializable)
911
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
10-
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.metrics.MeterConfig)
12+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder addMeterConfiguratorMatcher(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, io.opentelemetry.sdk.metrics.MeterConfig)
13+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder setMeterConfigurator(io.opentelemetry.sdk.common.ScopeConfigurator<io.opentelemetry.sdk.metrics.MeterConfig>)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Comparing source compatibility of against
22
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.SdkTracerProviderBuilder (not serializable)
33
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4-
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder addScopeConfig(io.opentelemetry.sdk.common.ScopeSelector, io.opentelemetry.sdk.trace.TracerConfig)
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder addTracerConfiguratorMatcher(java.util.function.Predicate<io.opentelemetry.sdk.common.InstrumentationScopeInfo>, io.opentelemetry.sdk.trace.TracerConfig)
5+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SdkTracerProviderBuilder setTracerConfigurator(io.opentelemetry.sdk.common.ScopeConfigurator<io.opentelemetry.sdk.trace.TracerConfig>)
56
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.trace.TracerConfig (not serializable)
67
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
78
+++ NEW SUPERCLASS: java.lang.Object
9+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.ScopeConfiguratorBuilder<io.opentelemetry.sdk.trace.TracerConfig> configuratorBuilder()
810
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.TracerConfig defaultConfig()
911
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.TracerConfig disabled()
12+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.TracerConfig enabled()
1013
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean isEnabled()

sdk/all/src/test/java/io/opentelemetry/sdk/ScopeConfigTest.java renamed to sdk/all/src/test/java/io/opentelemetry/sdk/ScopeConfiguratorTest.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
package io.opentelemetry.sdk;
77

8-
import static io.opentelemetry.sdk.common.ScopeConfig.applyToMatching;
9-
import static io.opentelemetry.sdk.common.ScopeConfig.scopeNameEquals;
8+
import static io.opentelemetry.sdk.common.ScopeConfiguratorBuilder.nameEquals;
109
import static org.assertj.core.api.Assertions.assertThat;
1110

1211
import io.opentelemetry.api.OpenTelemetry;
@@ -35,36 +34,31 @@
3534
import java.util.stream.Collectors;
3635
import org.junit.jupiter.api.Test;
3736

38-
class ScopeConfigTest {
37+
class ScopeConfiguratorTest {
3938

4039
private final InMemoryLogRecordExporter logRecordExporter = InMemoryLogRecordExporter.create();
4140
private final InMemoryMetricReader metricReader = InMemoryMetricReader.create();
4241
private final InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
4342

44-
/**
45-
* Disable "scopeB". All other scopes are enabled by default.
46-
*/
43+
/** Disable "scopeB". All other scopes are enabled by default. */
4744
@Test
4845
void disableScopeB() {
4946
OpenTelemetrySdk sdk =
5047
OpenTelemetrySdk.builder()
5148
.setTracerProvider(
5249
SdkTracerProvider.builder()
5350
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
54-
.setTracerConfigProvider(
55-
applyToMatching(scopeNameEquals("scopeB"), TracerConfig.disabled()))
51+
.addTracerConfiguratorMatcher(nameEquals("scopeB"), TracerConfig.disabled())
5652
.build())
5753
.setMeterProvider(
5854
SdkMeterProvider.builder()
5955
.registerMetricReader(metricReader)
60-
.setMeterConfigProvider(
61-
applyToMatching(scopeNameEquals("scopeB"), MeterConfig.disabled()))
56+
.addMeterConfiguratorMatcher(nameEquals("scopeB"), MeterConfig.disabled())
6257
.build())
6358
.setLoggerProvider(
6459
SdkLoggerProvider.builder()
6560
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
66-
.setLoggerConfigProvider(
67-
applyToMatching(scopeNameEquals("scopeB"), LoggerConfig.disabled()))
61+
.addLoggerConfiguratorMatcher(nameEquals("scopeB"), LoggerConfig.disabled())
6862
.build())
6963
.build();
7064

@@ -104,36 +98,44 @@ void disableScopeB() {
10498
});
10599
}
106100

107-
/**
108-
* Disable all scopes by default and enable a single scope.
109-
*/
101+
/** Disable all scopes by default and enable a single scope. */
110102
@Test
111103
void disableAllScopesExceptB() {
112104
OpenTelemetrySdk sdk =
113105
OpenTelemetrySdk.builder()
114106
.setTracerProvider(
115107
SdkTracerProvider.builder()
116108
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
117-
.setTracerConfigProvider(
118-
applyToMatching(scopeNameEquals("scopeB"), TracerConfig.enabled(), TracerConfig.disabled()))
109+
.setTracerConfigurator(
110+
TracerConfig.configuratorBuilder()
111+
.setDefault(TracerConfig.disabled())
112+
.addCondition(nameEquals("scopeB"), TracerConfig.enabled())
113+
.build())
119114
.build())
120115
.setMeterProvider(
121116
SdkMeterProvider.builder()
122117
.registerMetricReader(metricReader)
123-
.setMeterConfigProvider(
124-
applyToMatching(scopeNameEquals("scopeB"), MeterConfig.enabled(), MeterConfig.disabled()))
118+
.setMeterConfigurator(
119+
MeterConfig.configuratorBuilder()
120+
.setDefault(MeterConfig.disabled())
121+
.addCondition(nameEquals("scopeB"), MeterConfig.enabled())
122+
.build())
125123
.build())
126124
.setLoggerProvider(
127125
SdkLoggerProvider.builder()
128126
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
129-
.setLoggerConfigProvider(
130-
applyToMatching(scopeNameEquals("scopeB"), LoggerConfig.enabled(), LoggerConfig.disabled()))
127+
.setLoggerConfigurator(
128+
LoggerConfig.configuratorBuilder()
129+
.setDefault(LoggerConfig.disabled())
130+
.addCondition(nameEquals("scopeB"), LoggerConfig.enabled())
131+
.build())
131132
.build())
132133
.build();
133134

134135
simulateInstrumentation(sdk);
135136

136-
// Collect all the telemetry. Ensure we only see telemetry from scopeB, since other scopes have been disabled by default.
137+
// Collect all the telemetry. Ensure we only see telemetry from scopeB, since other scopes have
138+
// been disabled by default.
137139
assertThat(spanExporter.getFinishedSpanItems())
138140
.satisfies(
139141
spans -> {
@@ -166,7 +168,6 @@ void disableAllScopesExceptB() {
166168
});
167169
}
168170

169-
170171
/**
171172
* Emit spans, metrics and logs in a hierarchy of 3 scopes: scopeA -> scopeB -> scopeC. Exercise
172173
* the scope config which is common across all signals.

sdk/common/src/main/java/io/opentelemetry/sdk/common/ScopeConfig.java

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.common;
7+
8+
import java.util.function.Function;
9+
10+
/**
11+
* A {@link ScopeConfigurator} computes configuration for a given {@link InstrumentationScopeInfo}.
12+
*/
13+
@FunctionalInterface
14+
public interface ScopeConfigurator<T> extends Function<InstrumentationScopeInfo, T> {
15+
16+
/** Create a new builder. */
17+
static <T> ScopeConfiguratorBuilder<T> builder() {
18+
return new ScopeConfiguratorBuilder<>(unused -> null);
19+
}
20+
21+
/**
22+
* Convert this {@link ScopeConfigurator} to a builder. Additional added matchers only apply when
23+
* {@link #apply(Object)} returns {@code null}. If this configurator contains {@link
24+
* ScopeConfiguratorBuilder#setDefault(Object)}, additional matchers are never applied.
25+
*/
26+
default ScopeConfiguratorBuilder<T> toBuilder() {
27+
return new ScopeConfiguratorBuilder<>(this);
28+
}
29+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.common;
7+
8+
import io.opentelemetry.sdk.internal.GlobUtil;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.function.Predicate;
12+
import javax.annotation.Nullable;
13+
14+
/**
15+
* Builder for {@link ScopeConfigurator}.
16+
*
17+
* @param <T> The scope configuration object, e.g. {@code TracerConfig}, {@code LoggerConfig},
18+
* {@code MeterConfig}.
19+
*/
20+
public final class ScopeConfiguratorBuilder<T> {
21+
22+
private final ScopeConfigurator<T> baseScopeConfigurator;
23+
@Nullable private T defaultScopeConfig;
24+
private final List<Condition<T>> conditions = new ArrayList<>();
25+
26+
ScopeConfiguratorBuilder(ScopeConfigurator<T> baseScopeConfigurator) {
27+
this.baseScopeConfigurator = baseScopeConfigurator;
28+
}
29+
30+
/**
31+
* Set the default scope config, which is returned by {@link ScopeConfigurator#apply(Object)} if a
32+
* {@link InstrumentationScopeInfo} does not match any {@link #addCondition(Predicate, Object)
33+
* conditions}. If a default is not set, an SDK defined default is used.
34+
*/
35+
public ScopeConfiguratorBuilder<T> setDefault(T defaultScopeConfig) {
36+
this.defaultScopeConfig = defaultScopeConfig;
37+
return this;
38+
}
39+
40+
/**
41+
* Add a condition. Conditions are evaluated in order. The {@code scopeConfig} for the first match
42+
* is returned by {@link ScopeConfigurator#apply(Object)}.
43+
*
44+
* @param scopePredicate predicate that {@link InstrumentationScopeInfo}s are evaluated against
45+
* @param scopeConfig the scope config to use when this condition is the first matching {@code
46+
* scopePredicate}
47+
* @see #nameMatchesGlob(String)
48+
* @see #nameEquals(String)
49+
*/
50+
public ScopeConfiguratorBuilder<T> addCondition(
51+
Predicate<InstrumentationScopeInfo> scopePredicate, T scopeConfig) {
52+
conditions.add(new Condition<>(scopePredicate, scopeConfig));
53+
return this;
54+
}
55+
56+
/**
57+
* Helper function for pattern matching {@link InstrumentationScopeInfo#getName()} against the
58+
* {@code globPattern}.
59+
*
60+
* <p>{@code globPattern} may contain the wildcard characters {@code *} and {@code ?} with the
61+
* following matching criteria:
62+
*
63+
* <ul>
64+
* <li>{@code *} matches 0 or more instances of any character
65+
* <li>{@code ?} matches exactly one instance of any character
66+
* </ul>
67+
*
68+
* @see #addCondition(Predicate, Object)
69+
*/
70+
public static Predicate<InstrumentationScopeInfo> nameMatchesGlob(String globPattern) {
71+
Predicate<String> globPredicate = GlobUtil.toGlobPatternPredicate(globPattern);
72+
return scopeInfo -> globPredicate.test(scopeInfo.getName());
73+
}
74+
75+
/**
76+
* Helper function for exact matching {@link InstrumentationScopeInfo#getName()} against the
77+
* {@code scopeName}.
78+
*
79+
* @see #addCondition(Predicate, Object)
80+
*/
81+
public static Predicate<InstrumentationScopeInfo> nameEquals(String scopeName) {
82+
return scopeInfo -> scopeInfo.getName().equals(scopeName);
83+
}
84+
85+
/** Build a {@link ScopeConfigurator} with the configuration of this builder. */
86+
public ScopeConfigurator<T> build() {
87+
// TODO: return an instance with toString implementation which self describes rules
88+
return scopeInfo -> {
89+
T scopeConfig = baseScopeConfigurator.apply(scopeInfo);
90+
if (scopeConfig != null) {
91+
return scopeConfig;
92+
}
93+
for (Condition<T> condition : conditions) {
94+
if (condition.scopeMatcher.test(scopeInfo)) {
95+
return condition.scopeConfig;
96+
}
97+
}
98+
return defaultScopeConfig;
99+
};
100+
}
101+
102+
private static final class Condition<T> {
103+
private final Predicate<InstrumentationScopeInfo> scopeMatcher;
104+
private final T scopeConfig;
105+
106+
private Condition(Predicate<InstrumentationScopeInfo> scopeMatcher, T scopeConfig) {
107+
this.scopeMatcher = scopeMatcher;
108+
this.scopeConfig = scopeConfig;
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)