Skip to content

Commit 2e024c5

Browse files
committed
extract common class
1 parent de96376 commit 2e024c5

File tree

2 files changed

+114
-108
lines changed

2 files changed

+114
-108
lines changed

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

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

66
package io.opentelemetry.contrib.inferredspans;
77

8+
import static io.opentelemetry.contrib.inferredspans.InferredSpansConfig.ENABLED_OPTION;
9+
810
import com.google.auto.service.AutoService;
9-
import io.opentelemetry.api.trace.SpanBuilder;
10-
import io.opentelemetry.api.trace.SpanContext;
1111
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
1212
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
13-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
14-
import io.opentelemetry.sdk.trace.SpanProcessor;
15-
import java.time.Duration;
16-
import java.util.Arrays;
17-
import java.util.List;
18-
import java.util.function.BiConsumer;
19-
import java.util.function.Consumer;
2013
import java.util.logging.Logger;
21-
import java.util.stream.Collectors;
22-
import javax.annotation.Nullable;
2314

2415
@AutoService(AutoConfigurationCustomizerProvider.class)
2516
public class InferredSpansAutoConfig implements AutoConfigurationCustomizerProvider {
2617

2718
private static final Logger log = Logger.getLogger(InferredSpansAutoConfig.class.getName());
2819

29-
static final String ENABLED_OPTION = "otel.inferred.spans.enabled";
30-
static final String LOGGING_OPTION = "otel.inferred.spans.logging.enabled";
31-
static final String DIAGNOSTIC_FILES_OPTION = "otel.inferred.spans.backup.diagnostic.files";
32-
static final String SAFEMODE_OPTION = "otel.inferred.spans.safe.mode";
33-
static final String POSTPROCESSING_OPTION = "otel.inferred.spans.post.processing.enabled";
34-
static final String SAMPLING_INTERVAL_OPTION = "otel.inferred.spans.sampling.interval";
35-
static final String MIN_DURATION_OPTION = "otel.inferred.spans.min.duration";
36-
static final String INCLUDED_CLASSES_OPTION = "otel.inferred.spans.included.classes";
37-
static final String EXCLUDED_CLASSES_OPTION = "otel.inferred.spans.excluded.classes";
38-
static final String INTERVAL_OPTION = "otel.inferred.spans.interval";
39-
static final String DURATION_OPTION = "otel.inferred.spans.duration";
40-
static final String LIB_DIRECTORY_OPTION = "otel.inferred.spans.lib.directory";
41-
static final String PARENT_OVERRIDE_HANDLER_OPTION =
42-
"otel.inferred.spans.parent.override.handler";
43-
4420
@Override
4521
public void customize(AutoConfigurationCustomizer config) {
4622
config.addTracerProviderCustomizer(
4723
(providerBuilder, properties) -> {
4824
if (properties.getBoolean(ENABLED_OPTION, false)) {
49-
providerBuilder.addSpanProcessor(create(properties));
25+
providerBuilder.addSpanProcessor(InferredSpansConfig.create(properties));
5026
} else {
5127
log.finest(
5228
"Not enabling inferred spans processor because " + ENABLED_OPTION + " is not set");
5329
}
5430
return providerBuilder;
5531
});
5632
}
57-
58-
static SpanProcessor create(ConfigProperties properties) {
59-
InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder();
60-
61-
PropertiesApplier applier = new PropertiesApplier(properties);
62-
63-
applier.applyBool(ENABLED_OPTION, builder::profilerEnabled);
64-
applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled);
65-
applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles);
66-
applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode);
67-
applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled);
68-
applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval);
69-
applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration);
70-
applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses);
71-
applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses);
72-
applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval);
73-
applier.applyDuration(DURATION_OPTION, builder::profilingDuration);
74-
applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory);
75-
76-
String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION);
77-
if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) {
78-
builder.parentOverrideHandler(constructParentOverrideHandler(parentOverrideHandlerName));
79-
}
80-
81-
return builder.build();
82-
}
83-
84-
@SuppressWarnings("unchecked")
85-
private static BiConsumer<SpanBuilder, SpanContext> constructParentOverrideHandler(String name) {
86-
try {
87-
Class<?> clazz = Class.forName(name);
88-
return (BiConsumer<SpanBuilder, SpanContext>) clazz.getConstructor().newInstance();
89-
} catch (Exception e) {
90-
throw new IllegalArgumentException("Could not construct parent override handler", e);
91-
}
92-
}
93-
94-
private static class PropertiesApplier {
95-
96-
private final ConfigProperties properties;
97-
98-
PropertiesApplier(ConfigProperties properties) {
99-
this.properties = properties;
100-
}
101-
102-
void applyBool(String configKey, Consumer<Boolean> funcToApply) {
103-
applyValue(properties.getBoolean(configKey), funcToApply);
104-
}
105-
106-
void applyInt(String configKey, Consumer<Integer> funcToApply) {
107-
applyValue(properties.getInt(configKey), funcToApply);
108-
}
109-
110-
void applyDuration(String configKey, Consumer<Duration> funcToApply) {
111-
applyValue(properties.getDuration(configKey), funcToApply);
112-
}
113-
114-
void applyString(String configKey, Consumer<String> funcToApply) {
115-
applyValue(properties.getString(configKey), funcToApply);
116-
}
117-
118-
void applyWildcards(String configKey, Consumer<? super List<WildcardMatcher>> funcToApply) {
119-
String wildcardListString = properties.getString(configKey);
120-
if (wildcardListString != null && !wildcardListString.isEmpty()) {
121-
List<WildcardMatcher> values =
122-
Arrays.stream(wildcardListString.split(","))
123-
.filter(str -> !str.isEmpty())
124-
.map(WildcardMatcher::valueOf)
125-
.collect(Collectors.toList());
126-
if (!values.isEmpty()) {
127-
funcToApply.accept(values);
128-
}
129-
}
130-
}
131-
132-
private static <T> void applyValue(@Nullable T value, Consumer<T> funcToApply) {
133-
if (value != null) {
134-
funcToApply.accept(value);
135-
}
136-
}
137-
}
13833
}
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)