Skip to content

Commit 973616a

Browse files
committed
support inferred spans use case
1 parent 4ee8844 commit 973616a

File tree

4 files changed

+107
-13
lines changed

4 files changed

+107
-13
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# OpenTelemetry Instrumentation API Incubator
2+
3+
Instrumentation API Incubator is a collection of libraries that provide additional functionality
4+
for OpenTelemetry instrumentation and auto-configuration. It is intended to be used by
5+
instrumentation authors and auto-configuration providers to enhance their capabilities and provide a
6+
more consistent experience when working with OpenTelemetry.
7+
8+
## Declarative Config Bridge
9+
10+
Declarative Config Bridge allows instrumentation authors to access configuration in a uniform way,
11+
regardless of the configuration source.
12+
13+
The bridge allows you to read configuration using the system property style when dealing with
14+
declarative configuration.
15+
16+
### Example
17+
18+
As an example, let's look at the inferred spans configuration.
19+
First, there is a configuration method that reads the properties and is unaware of the source of the
20+
configuration:
21+
22+
```java
23+
class InferredSpansConfig {
24+
static SpanProcessor create(ConfigProperties properties) {
25+
// read properties here
26+
boolean backupDiagnosticFiles =
27+
properties.getBoolean("otel.inferred.spans.backup.diagnostic.files", false);
28+
}
29+
}
30+
```
31+
32+
The auto configuration **without declarative config** passes the provided properties directly:
33+
34+
```java
35+
36+
@AutoService(AutoConfigurationCustomizerProvider.class)
37+
public class InferredSpansAutoConfig implements AutoConfigurationCustomizerProvider {
38+
39+
@Override
40+
public void customize(AutoConfigurationCustomizer config) {
41+
config.addTracerProviderCustomizer(
42+
(providerBuilder, properties) -> {
43+
providerBuilder.addSpanProcessor(InferredSpansConfig.create(properties));
44+
return providerBuilder;
45+
});
46+
}
47+
}
48+
```
49+
50+
The auto configuration **with declarative config** uses the Declarative Config Bridge to be able to
51+
use common configuration method:
52+
53+
Let's first look at the yaml file that is used to configure the inferred spans processor:
54+
55+
```yaml
56+
file_format: 1.0-rc.1
57+
tracer_provider:
58+
processors:
59+
- inferred_spans:
60+
backup:
61+
diagnostic:
62+
files: true
63+
```
64+
65+
And now the component provider that uses the Declarative Config Bridge:
66+
67+
```java
68+
69+
@AutoService(ComponentProvider.class)
70+
public class InferredSpansComponentProvider implements ComponentProvider<SpanProcessor> {
71+
72+
@Override
73+
public String getName() {
74+
return "inferred_spans";
75+
}
76+
77+
@Override
78+
public SpanProcessor create(DeclarativeConfigProperties config) {
79+
return InferredSpansConfig.create(
80+
new DeclarativeConfigPropertiesBridgeBuilder()
81+
// crop the prefix, because the properties are under the "inferred_spans" processor
82+
.addMapping("otel.inferred.spans.", "")
83+
.build(config));
84+
}
85+
86+
@Override
87+
public Class<SpanProcessor> getType() {
88+
return SpanProcessor.class;
89+
}
90+
}
91+
```
92+

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/config/bridge/DeclarativeConfigPropertiesBridgeBuilder.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
/**
2222
* A builder for {@link DeclarativeConfigPropertiesBridge} that allows adding translations and fixed
2323
* values for properties.
24-
*
25-
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
26-
* at any time.
2724
*/
2825
public class DeclarativeConfigPropertiesBridgeBuilder {
2926
/**
@@ -82,6 +79,17 @@ public ConfigProperties build(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenT
8279
"AutoConfiguredOpenTelemetrySdk does not have ConfigProperties or DeclarativeConfigProperties. This is likely a programming error in opentelemetry-java");
8380
}
8481

82+
/**
83+
* Build {@link ConfigProperties} from the provided {@link DeclarativeConfigProperties} node.
84+
*
85+
* @param node the declarative config properties to build from
86+
* @return a new instance of {@link ConfigProperties}
87+
*/
88+
public ConfigProperties build(@Nullable DeclarativeConfigProperties node) {
89+
return new DeclarativeConfigPropertiesBridge(
90+
node == null ? empty() : node, mappings, overrideValues);
91+
}
92+
8593
/**
8694
* Build {@link ConfigProperties} from the {@link DeclarativeConfigProperties} provided by the
8795
* instrumentation configuration.
@@ -94,12 +102,7 @@ public ConfigProperties build(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenT
94102
*/
95103
public ConfigProperties buildFromInstrumentationConfig(
96104
@Nullable DeclarativeConfigProperties instrumentationConfig) {
97-
// leave the name "build" for a future method that builds from a DeclarativeConfigProperties
98-
// instance that doesn't come from the top-level instrumentation config
99-
if (instrumentationConfig == null) {
100-
instrumentationConfig = DeclarativeConfigProperties.empty();
101-
}
102-
return new DeclarativeConfigPropertiesBridge(
103-
instrumentationConfig.getStructured("java", empty()), mappings, overrideValues);
105+
return build(
106+
instrumentationConfig == null ? null : instrumentationConfig.getStructured("java"));
104107
}
105108
}

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/config/bridge/DeclarativeConfigPropertiesBridgeBuilderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.instrumentation.api.incubator.config.bridge;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9-
import static org.mockito.ArgumentMatchers.any;
109
import static org.mockito.ArgumentMatchers.eq;
1110
import static org.mockito.Mockito.mock;
1211
import static org.mockito.Mockito.when;
@@ -47,7 +46,7 @@ void shouldUseConfigProviderForDeclarativeConfiguration() {
4746
when(javaNodeMock.getString(propertyName)).thenReturn(expectedValue);
4847

4948
DeclarativeConfigProperties instrumentationConfigMock = mock(DeclarativeConfigProperties.class);
50-
when(instrumentationConfigMock.getStructured(eq("java"), any())).thenReturn(javaNodeMock);
49+
when(instrumentationConfigMock.getStructured(eq("java"))).thenReturn(javaNodeMock);
5150

5251
ConfigProvider configProviderMock = mock(ConfigProvider.class);
5352
when(configProviderMock.getInstrumentationConfig()).thenReturn(instrumentationConfigMock);

instrumentation-api-incubator/src/test/resources/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file_format: 0.4
1+
file_format: 1.0-rc.1
22
instrumentation/development:
33
java:
44
acme:

0 commit comments

Comments
 (0)