Skip to content

Commit 6b63e67

Browse files
committed
inferred spans
1 parent 789bbb4 commit 6b63e67

File tree

4 files changed

+123
-26
lines changed

4 files changed

+123
-26
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,8 @@ public class InferredSpansAutoConfig implements AutoConfigurationCustomizerProvi
4444
public void customize(AutoConfigurationCustomizer config) {
4545
config.addTracerProviderCustomizer(
4646
(providerBuilder, properties) -> {
47-
if (properties.getBoolean(ENABLED_OPTION, false)) {
48-
InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder();
49-
50-
PropertiesApplier applier = new PropertiesApplier(properties);
51-
52-
applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled);
53-
applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles);
54-
applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode);
55-
applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled);
56-
applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval);
57-
applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration);
58-
applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses);
59-
applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses);
60-
applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval);
61-
applier.applyDuration(DURATION_OPTION, builder::profilingDuration);
62-
applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory);
63-
64-
String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION);
65-
if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) {
66-
builder.parentOverrideHandler(
67-
constructParentOverrideHandler(parentOverrideHandlerName));
68-
}
69-
70-
providerBuilder.addSpanProcessor(builder.build());
47+
if (isEnabled(properties)) {
48+
providerBuilder.addSpanProcessor(create(properties));
7149
} else {
7250
log.finest(
7351
"Not enabling inferred spans processor because " + ENABLED_OPTION + " is not set");
@@ -76,6 +54,36 @@ public void customize(AutoConfigurationCustomizer config) {
7654
});
7755
}
7856

57+
static InferredSpansProcessor create(ConfigProperties properties) {
58+
InferredSpansProcessorBuilder builder = InferredSpansProcessor.builder();
59+
60+
PropertiesApplier applier = new PropertiesApplier(properties);
61+
62+
applier.applyBool(LOGGING_OPTION, builder::profilerLoggingEnabled);
63+
applier.applyBool(DIAGNOSTIC_FILES_OPTION, builder::backupDiagnosticFiles);
64+
applier.applyInt(SAFEMODE_OPTION, builder::asyncProfilerSafeMode);
65+
applier.applyBool(POSTPROCESSING_OPTION, builder::postProcessingEnabled);
66+
applier.applyDuration(SAMPLING_INTERVAL_OPTION, builder::samplingInterval);
67+
applier.applyDuration(MIN_DURATION_OPTION, builder::inferredSpansMinDuration);
68+
applier.applyWildcards(INCLUDED_CLASSES_OPTION, builder::includedClasses);
69+
applier.applyWildcards(EXCLUDED_CLASSES_OPTION, builder::excludedClasses);
70+
applier.applyDuration(INTERVAL_OPTION, builder::profilerInterval);
71+
applier.applyDuration(DURATION_OPTION, builder::profilingDuration);
72+
applier.applyString(LIB_DIRECTORY_OPTION, builder::profilerLibDirectory);
73+
74+
String parentOverrideHandlerName = properties.getString(PARENT_OVERRIDE_HANDLER_OPTION);
75+
if (parentOverrideHandlerName != null && !parentOverrideHandlerName.isEmpty()) {
76+
builder.parentOverrideHandler(constructParentOverrideHandler(parentOverrideHandlerName));
77+
}
78+
79+
InferredSpansProcessor spanProcessor = builder.build();
80+
return spanProcessor;
81+
}
82+
83+
static boolean isEnabled(ConfigProperties properties) {
84+
return properties.getBoolean(ENABLED_OPTION, false);
85+
}
86+
7987
@SuppressWarnings("unchecked")
8088
private static BiConsumer<SpanBuilder, SpanContext> constructParentOverrideHandler(String name) {
8189
try {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.inferredspans;
7+
8+
import com.google.auto.service.AutoService;
9+
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
10+
import io.opentelemetry.contrib.sdk.autoconfigure.ConfigPropertiesUtil;
11+
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
12+
import io.opentelemetry.sdk.trace.SpanProcessor;
13+
14+
@SuppressWarnings("rawtypes")
15+
@AutoService(ComponentProvider.class)
16+
public class InferredSpansComponentProvider implements ComponentProvider<SpanProcessor> {
17+
@Override
18+
public String getName() {
19+
return "inferred_spans";
20+
}
21+
22+
@Override
23+
public SpanProcessor create(DeclarativeConfigProperties config) {
24+
return InferredSpansAutoConfig.create(
25+
ConfigPropertiesUtil.resolveInstrumentationConfig(
26+
config, InferredSpansCustomizerProvider.TRANSLATION_MAP));
27+
}
28+
29+
@Override
30+
public Class<SpanProcessor> getType() {
31+
return SpanProcessor.class;
32+
}
33+
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,35 @@
1010
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1111
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizer;
1212
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizerProvider;
13+
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanProcessorModel;
14+
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProviderModel;
1315
import java.util.Collections;
16+
import java.util.Map;
1417

1518
@AutoService(DeclarativeConfigurationCustomizerProvider.class)
1619
public class InferredSpansCustomizerProvider implements DeclarativeConfigurationCustomizerProvider {
1720

21+
static final Map<String, String> TRANSLATION_MAP =
22+
Collections.singletonMap("otel.inferred.spans", "inferred_spans");
23+
1824
@Override
1925
public void customize(DeclarativeConfigurationCustomizer customizer) {
2026
customizer.addModelCustomizer(
2127
model -> {
22-
ConfigProperties configProperties = ConfigPropertiesUtil.resolveModel(model,
23-
Collections.singletonMap("otel.inferred.spans", "inferred_spans"));
28+
ConfigProperties configProperties =
29+
ConfigPropertiesUtil.resolveModel(model, TRANSLATION_MAP);
30+
31+
TracerProviderModel tracerProvider = model.getTracerProvider();
32+
if (tracerProvider != null && InferredSpansAutoConfig.isEnabled(configProperties)) {
33+
tracerProvider.getProcessors().add(create());
34+
}
2435

2536
return model;
2637
});
2738
}
2839

40+
@SuppressWarnings("NullAway")
41+
private static SpanProcessorModel create() {
42+
return new SpanProcessorModel().withAdditionalProperty("inferred_spans", null);
43+
}
2944
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.inferredspans;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
11+
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
12+
import java.io.ByteArrayInputStream;
13+
import java.nio.charset.StandardCharsets;
14+
import org.junit.jupiter.api.Test;
15+
16+
class InferredSpansCustomizerProviderTest {
17+
18+
@Test
19+
void declarativeConfig() {
20+
String yaml =
21+
"file_format: 0.4\n"
22+
+ "tracer_provider:\n"
23+
+ "instrumentation/development:\n"
24+
+ " java:\n"
25+
+ " inferred_spans:\n"
26+
+ " enabled: true\n";
27+
28+
OpenTelemetryConfigurationModel model =
29+
DeclarativeConfiguration.parse(
30+
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
31+
32+
new InferredSpansCustomizerProvider()
33+
.customize(
34+
c -> {
35+
OpenTelemetryConfigurationModel configurationModel = c.apply(model);
36+
assertThat(configurationModel.toString())
37+
.matches(
38+
".*SpanProcessorModel@.{8}\\[batch=<null>,simple=<null>,additionalProperties=\\{inferred_spans=null}.*");
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)