|
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 java.time.Duration; |
17 | | -import java.util.Arrays; |
18 | | -import java.util.List; |
19 | | -import java.util.function.BiConsumer; |
20 | | -import java.util.function.Consumer; |
21 | 13 | import java.util.logging.Logger; |
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 | | - InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder(); |
50 | | - |
51 | | - PropertiesApplier applier = new PropertiesApplier(properties); |
52 | | - |
53 | | - applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled); |
54 | | - applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles); |
55 | | - applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode); |
56 | | - applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled); |
57 | | - applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval); |
58 | | - applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration); |
59 | | - applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses); |
60 | | - applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses); |
61 | | - applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval); |
62 | | - applier.applyDuration(DURATION_OPTION, builder::profilingDuration); |
63 | | - applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory); |
64 | | - |
65 | | - String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION); |
66 | | - if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) { |
67 | | - builder.parentOverrideHandler( |
68 | | - constructParentOverrideHandler(parentOverrideHandlerName)); |
69 | | - } |
70 | | - |
71 | | - providerBuilder.addSpanProcessor(builder.build()); |
| 25 | + providerBuilder.addSpanProcessor(InferredSpansConfig.createSpanProcessor(properties)); |
72 | 26 | } else { |
73 | 27 | log.finest( |
74 | 28 | "Not enabling inferred spans processor because " + ENABLED_OPTION + " is not set"); |
75 | 29 | } |
76 | 30 | return providerBuilder; |
77 | 31 | }); |
78 | 32 | } |
79 | | - |
80 | | - @SuppressWarnings("unchecked") |
81 | | - private static BiConsumer<SpanBuilder, SpanContext> constructParentOverrideHandler(String name) { |
82 | | - try { |
83 | | - Class<?> clazz = Class.forName(name); |
84 | | - return (BiConsumer<SpanBuilder, SpanContext>) clazz.getConstructor().newInstance(); |
85 | | - } catch (Exception e) { |
86 | | - throw new IllegalArgumentException("Could not construct parent override handler", e); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - private static class PropertiesApplier { |
91 | | - |
92 | | - private final ConfigProperties properties; |
93 | | - |
94 | | - PropertiesApplier(ConfigProperties properties) { |
95 | | - this.properties = properties; |
96 | | - } |
97 | | - |
98 | | - void applyBool(String configKey, Consumer<Boolean> funcToApply) { |
99 | | - applyValue(properties.getBoolean(configKey), funcToApply); |
100 | | - } |
101 | | - |
102 | | - void applyInt(String configKey, Consumer<Integer> funcToApply) { |
103 | | - applyValue(properties.getInt(configKey), funcToApply); |
104 | | - } |
105 | | - |
106 | | - void applyDuration(String configKey, Consumer<Duration> funcToApply) { |
107 | | - applyValue(properties.getDuration(configKey), funcToApply); |
108 | | - } |
109 | | - |
110 | | - void applyString(String configKey, Consumer<String> funcToApply) { |
111 | | - applyValue(properties.getString(configKey), funcToApply); |
112 | | - } |
113 | | - |
114 | | - void applyWildcards(String configKey, Consumer<? super List<WildcardMatcher>> funcToApply) { |
115 | | - String wildcardListString = properties.getString(configKey); |
116 | | - if (wildcardListString != null && !wildcardListString.isEmpty()) { |
117 | | - List<WildcardMatcher> values = |
118 | | - Arrays.stream(wildcardListString.split(",")) |
119 | | - .filter(str -> !str.isEmpty()) |
120 | | - .map(WildcardMatcher::valueOf) |
121 | | - .collect(toList()); |
122 | | - if (!values.isEmpty()) { |
123 | | - funcToApply.accept(values); |
124 | | - } |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - private static <T> void applyValue(@Nullable T value, Consumer<T> funcToApply) { |
129 | | - if (value != null) { |
130 | | - funcToApply.accept(value); |
131 | | - } |
132 | | - } |
133 | | - } |
134 | 33 | } |
0 commit comments