|
5 | 5 |
|
6 | 6 | package io.opentelemetry.contrib.inferredspans;
|
7 | 7 |
|
| 8 | +import static io.opentelemetry.contrib.inferredspans.InferredSpansConfig.ENABLED_OPTION; |
| 9 | + |
8 | 10 | import com.google.auto.service.AutoService;
|
9 |
| -import io.opentelemetry.api.trace.SpanBuilder; |
10 |
| -import io.opentelemetry.api.trace.SpanContext; |
11 | 11 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
|
12 | 12 | 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; |
20 | 13 | import java.util.logging.Logger;
|
21 |
| -import java.util.stream.Collectors; |
22 |
| -import javax.annotation.Nullable; |
23 | 14 |
|
24 | 15 | @AutoService(AutoConfigurationCustomizerProvider.class)
|
25 | 16 | public class InferredSpansAutoConfig implements AutoConfigurationCustomizerProvider {
|
26 | 17 |
|
27 | 18 | private static final Logger log = Logger.getLogger(InferredSpansAutoConfig.class.getName());
|
28 | 19 |
|
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 |
| - |
44 | 20 | @Override
|
45 | 21 | public void customize(AutoConfigurationCustomizer config) {
|
46 | 22 | config.addTracerProviderCustomizer(
|
47 | 23 | (providerBuilder, properties) -> {
|
48 | 24 | if (properties.getBoolean(ENABLED_OPTION, false)) {
|
49 |
| - providerBuilder.addSpanProcessor(create(properties)); |
| 25 | + providerBuilder.addSpanProcessor(InferredSpansConfig.create(properties)); |
50 | 26 | } else {
|
51 | 27 | log.finest(
|
52 | 28 | "Not enabling inferred spans processor because " + ENABLED_OPTION + " is not set");
|
53 | 29 | }
|
54 | 30 | return providerBuilder;
|
55 | 31 | });
|
56 | 32 | }
|
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 |
| - } |
138 | 33 | }
|
0 commit comments