Skip to content

Commit 569bf52

Browse files
committed
extract common class
1 parent 2c66efa commit 569bf52

2 files changed

Lines changed: 113 additions & 108 deletions

File tree

inferred-spans/src/main/java/io/opentelemetry/contrib/inferredspans/InferredSpansAutoConfig.java

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,135 +5,29 @@
55

66
package io.opentelemetry.contrib.inferredspans;
77

8-
import static java.util.stream.Collectors.toList;
8+
import static io.opentelemetry.contrib.inferredspans.InferredSpansConfig.ENABLED_OPTION;
99

1010
import com.google.auto.service.AutoService;
11-
import io.opentelemetry.api.trace.SpanBuilder;
12-
import io.opentelemetry.api.trace.SpanContext;
1311
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
1412
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
15-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
16-
import io.opentelemetry.sdk.trace.SpanProcessor;
17-
import java.time.Duration;
18-
import java.util.Arrays;
19-
import java.util.List;
20-
import java.util.function.BiConsumer;
21-
import java.util.function.Consumer;
2213
import java.util.logging.Logger;
23-
import javax.annotation.Nullable;
2414

2515
@AutoService(AutoConfigurationCustomizerProvider.class)
2616
public class InferredSpansAutoConfig implements AutoConfigurationCustomizerProvider {
2717

2818
private static final Logger log = Logger.getLogger(InferredSpansAutoConfig.class.getName());
2919

30-
static final String ENABLED_OPTION = "otel.inferred.spans.enabled";
31-
static final String LOGGING_OPTION = "otel.inferred.spans.logging.enabled";
32-
static final String DIAGNOSTIC_FILES_OPTION = "otel.inferred.spans.backup.diagnostic.files";
33-
static final String SAFEMODE_OPTION = "otel.inferred.spans.safe.mode";
34-
static final String POSTPROCESSING_OPTION = "otel.inferred.spans.post.processing.enabled";
35-
static final String SAMPLING_INTERVAL_OPTION = "otel.inferred.spans.sampling.interval";
36-
static final String MIN_DURATION_OPTION = "otel.inferred.spans.min.duration";
37-
static final String INCLUDED_CLASSES_OPTION = "otel.inferred.spans.included.classes";
38-
static final String EXCLUDED_CLASSES_OPTION = "otel.inferred.spans.excluded.classes";
39-
static final String INTERVAL_OPTION = "otel.inferred.spans.interval";
40-
static final String DURATION_OPTION = "otel.inferred.spans.duration";
41-
static final String LIB_DIRECTORY_OPTION = "otel.inferred.spans.lib.directory";
42-
static final String PARENT_OVERRIDE_HANDLER_OPTION =
43-
"otel.inferred.spans.parent.override.handler";
44-
4520
@Override
4621
public void customize(AutoConfigurationCustomizer config) {
4722
config.addTracerProviderCustomizer(
4823
(providerBuilder, properties) -> {
4924
if (properties.getBoolean(ENABLED_OPTION, false)) {
50-
providerBuilder.addSpanProcessor(create(properties));
25+
providerBuilder.addSpanProcessor(InferredSpansConfig.create(properties));
5126
} else {
5227
log.finest(
5328
"Not enabling inferred spans processor because " + ENABLED_OPTION + " is not set");
5429
}
5530
return providerBuilder;
5631
});
5732
}
58-
59-
static SpanProcessor create(ConfigProperties properties) {
60-
InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder();
61-
62-
PropertiesApplier applier = new PropertiesApplier(properties);
63-
64-
applier.applyBool(ENABLED_OPTION, builder::profilerEnabled);
65-
applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled);
66-
applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles);
67-
applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode);
68-
applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled);
69-
applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval);
70-
applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration);
71-
applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses);
72-
applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses);
73-
applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval);
74-
applier.applyDuration(DURATION_OPTION, builder::profilingDuration);
75-
applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory);
76-
77-
String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION);
78-
if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) {
79-
builder.parentOverrideHandler(constructParentOverrideHandler(parentOverrideHandlerName));
80-
}
81-
82-
return builder.build();
83-
}
84-
85-
@SuppressWarnings("unchecked")
86-
private static BiConsumer<SpanBuilder, SpanContext> constructParentOverrideHandler(String name) {
87-
try {
88-
Class<?> clazz = Class.forName(name);
89-
return (BiConsumer<SpanBuilder, SpanContext>) clazz.getConstructor().newInstance();
90-
} catch (Exception e) {
91-
throw new IllegalArgumentException("Could not construct parent override handler", e);
92-
}
93-
}
94-
95-
private static class PropertiesApplier {
96-
97-
private final ConfigProperties properties;
98-
99-
PropertiesApplier(ConfigProperties properties) {
100-
this.properties = properties;
101-
}
102-
103-
void applyBool(String configKey, Consumer<Boolean> funcToApply) {
104-
applyValue(properties.getBoolean(configKey), funcToApply);
105-
}
106-
107-
void applyInt(String configKey, Consumer<Integer> funcToApply) {
108-
applyValue(properties.getInt(configKey), funcToApply);
109-
}
110-
111-
void applyDuration(String configKey, Consumer<Duration> funcToApply) {
112-
applyValue(properties.getDuration(configKey), funcToApply);
113-
}
114-
115-
void applyString(String configKey, Consumer<String> funcToApply) {
116-
applyValue(properties.getString(configKey), funcToApply);
117-
}
118-
119-
void applyWildcards(String configKey, Consumer<? super List<WildcardMatcher>> funcToApply) {
120-
String wildcardListString = properties.getString(configKey);
121-
if (wildcardListString != null && !wildcardListString.isEmpty()) {
122-
List<WildcardMatcher> values =
123-
Arrays.stream(wildcardListString.split(","))
124-
.filter(str -> !str.isEmpty())
125-
.map(WildcardMatcher::valueOf)
126-
.collect(toList());
127-
if (!values.isEmpty()) {
128-
funcToApply.accept(values);
129-
}
130-
}
131-
}
132-
133-
private static <T> void applyValue(@Nullable T value, Consumer<T> funcToApply) {
134-
if (value != null) {
135-
funcToApply.accept(value);
136-
}
137-
}
138-
}
13933
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.opentelemetry.contrib.inferredspans;
2+
3+
import io.opentelemetry.api.trace.SpanBuilder;
4+
import io.opentelemetry.api.trace.SpanContext;
5+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
6+
import io.opentelemetry.sdk.trace.SpanProcessor;
7+
import java.time.Duration;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.function.BiConsumer;
11+
import java.util.function.Consumer;
12+
import java.util.stream.Collectors;
13+
import javax.annotation.Nullable;
14+
15+
public class InferredSpansConfig {
16+
static final String ENABLED_OPTION = "otel.inferred.spans.enabled";
17+
static final String LOGGING_OPTION = "otel.inferred.spans.logging.enabled";
18+
static final String DIAGNOSTIC_FILES_OPTION = "otel.inferred.spans.backup.diagnostic.files";
19+
static final String SAFEMODE_OPTION = "otel.inferred.spans.safe.mode";
20+
static final String POSTPROCESSING_OPTION = "otel.inferred.spans.post.processing.enabled";
21+
static final String SAMPLING_INTERVAL_OPTION = "otel.inferred.spans.sampling.interval";
22+
static final String MIN_DURATION_OPTION = "otel.inferred.spans.min.duration";
23+
static final String INCLUDED_CLASSES_OPTION = "otel.inferred.spans.included.classes";
24+
static final String EXCLUDED_CLASSES_OPTION = "otel.inferred.spans.excluded.classes";
25+
static final String INTERVAL_OPTION = "otel.inferred.spans.interval";
26+
static final String DURATION_OPTION = "otel.inferred.spans.duration";
27+
static final String LIB_DIRECTORY_OPTION = "otel.inferred.spans.lib.directory";
28+
static final String PARENT_OVERRIDE_HANDLER_OPTION =
29+
"otel.inferred.spans.parent.override.handler";
30+
31+
static SpanProcessor create(ConfigProperties properties) {
32+
InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder();
33+
34+
PropertiesApplier applier = new PropertiesApplier(properties);
35+
36+
applier.applyBool(ENABLED_OPTION, builder::profilerEnabled);
37+
applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled);
38+
applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles);
39+
applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode);
40+
applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled);
41+
applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval);
42+
applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration);
43+
applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses);
44+
applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses);
45+
applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval);
46+
applier.applyDuration(DURATION_OPTION, builder::profilingDuration);
47+
applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory);
48+
49+
String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION);
50+
if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) {
51+
builder.parentOverrideHandler(constructParentOverrideHandler(parentOverrideHandlerName));
52+
}
53+
54+
return builder.build();
55+
}
56+
57+
@SuppressWarnings("unchecked")
58+
private static BiConsumer<SpanBuilder, SpanContext> constructParentOverrideHandler(String name) {
59+
try {
60+
Class<?> clazz = Class.forName(name);
61+
return (BiConsumer<SpanBuilder, SpanContext>) clazz.getConstructor().newInstance();
62+
} catch (Exception e) {
63+
throw new IllegalArgumentException("Could not construct parent override handler", e);
64+
}
65+
}
66+
67+
private static class PropertiesApplier {
68+
69+
private final ConfigProperties properties;
70+
71+
PropertiesApplier(ConfigProperties properties) {
72+
this.properties = properties;
73+
}
74+
75+
void applyBool(String configKey, Consumer<Boolean> funcToApply) {
76+
applyValue(properties.getBoolean(configKey), funcToApply);
77+
}
78+
79+
void applyInt(String configKey, Consumer<Integer> funcToApply) {
80+
applyValue(properties.getInt(configKey), funcToApply);
81+
}
82+
83+
void applyDuration(String configKey, Consumer<Duration> funcToApply) {
84+
applyValue(properties.getDuration(configKey), funcToApply);
85+
}
86+
87+
void applyString(String configKey, Consumer<String> funcToApply) {
88+
applyValue(properties.getString(configKey), funcToApply);
89+
}
90+
91+
void applyWildcards(String configKey, Consumer<? super List<WildcardMatcher>> funcToApply) {
92+
String wildcardListString = properties.getString(configKey);
93+
if (wildcardListString != null && !wildcardListString.isEmpty()) {
94+
List<WildcardMatcher> values =
95+
Arrays.stream(wildcardListString.split(","))
96+
.filter(str -> !str.isEmpty())
97+
.map(WildcardMatcher::valueOf)
98+
.collect(Collectors.toList());
99+
if (!values.isEmpty()) {
100+
funcToApply.accept(values);
101+
}
102+
}
103+
}
104+
105+
private static <T> void applyValue(@Nullable T value, Consumer<T> funcToApply) {
106+
if (value != null) {
107+
funcToApply.accept(value);
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)