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