Skip to content

Commit 54b5e92

Browse files
authored
Update examples for InstrumenterCustomizerProvider extension (#15239)
1 parent b94cf7d commit 54b5e92

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

examples/distro/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The `InstrumenterCustomizerProvider` extension point allows you to customize ins
4444
- Add custom attributes and metrics to existing instrumentations
4545
- Customize context
4646
- Transform span names to match your naming conventions
47-
- Apply customizations conditionally based on instrumentation name
47+
- Apply customizations conditionally based on instrumentation name or type (HTTP client, HTTP server, DB client, etc.)
4848

4949
### I don't want this span at all
5050

examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* </ul>
3636
*
3737
* <p>The customizer will be automatically applied to instrumenters that match the specified
38-
* instrumentation name and span kind.
38+
* instrumentation name or type.
3939
*
4040
* @see InstrumenterCustomizerProvider
4141
* @see InstrumenterCustomizer
@@ -48,6 +48,10 @@ public void customize(InstrumenterCustomizer customizer) {
4848
if (isHttpServerInstrumentation(instrumentationName)) {
4949
customizeHttpServer(customizer);
5050
}
51+
52+
if (customizer.hasType(InstrumenterCustomizer.InstrumentationType.HTTP_CLIENT)) {
53+
customizeHttpClient(customizer);
54+
}
5155
}
5256

5357
private boolean isHttpServerInstrumentation(String instrumentationName) {
@@ -66,6 +70,31 @@ private void customizeHttpServer(InstrumenterCustomizer customizer) {
6670
unused -> (SpanNameExtractor<Object>) object -> "CustomHTTP/" + object.toString());
6771
}
6872

73+
private void customizeHttpClient(InstrumenterCustomizer customizer) {
74+
// Simple customization for HTTP client instrumentations
75+
customizer.addAttributesExtractor(new DemoHttpClientAttributesExtractor());
76+
}
77+
78+
/** Custom attributes extractor for HTTP client instrumentations. */
79+
private static class DemoHttpClientAttributesExtractor
80+
implements AttributesExtractor<Object, Object> {
81+
private static final AttributeKey<String> CLIENT_ATTR =
82+
AttributeKey.stringKey("demo.client.type");
83+
84+
@Override
85+
public void onStart(AttributesBuilder attributes, Context context, Object request) {
86+
attributes.put(CLIENT_ATTR, "demo-http-client");
87+
}
88+
89+
@Override
90+
public void onEnd(
91+
AttributesBuilder attributes,
92+
Context context,
93+
Object request,
94+
Object response,
95+
Throwable error) {}
96+
}
97+
6998
/** Custom attributes extractor that adds demo-specific attributes. */
7099
private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> {
71100
private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom");

examples/extension/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The `InstrumenterCustomizerProvider` extension point allows you to customize ins
7474
- Add custom attributes and metrics to existing instrumentations
7575
- Customize context
7676
- Transform span names to match your naming conventions
77-
- Apply customizations conditionally based on instrumentation name
77+
- Apply customizations conditionally based on instrumentation name or type (HTTP client, HTTP server, DB client, etc.)
7878

7979
### "I don't want this span at all"
8080

examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
* <p>This customizer adds:
3030
*
3131
* <ul>
32-
* <li>Custom attributes to HTTP server spans
32+
* <li>Custom attributes to HTTP server spans (based on instrumentation name)
33+
* <li>Custom attributes to HTTP client spans (based on instrumentation type)
3334
* <li>Custom metrics for HTTP operations
3435
* <li>Request correlation IDs via context customization
3536
* <li>Custom span name transformation
3637
* </ul>
3738
*
3839
* <p>The customizer will be automatically applied to instrumenters that match the specified
39-
* instrumentation name and span kind.
40+
* instrumentation name or type.
4041
*
4142
* @see InstrumenterCustomizerProvider
4243
* @see InstrumenterCustomizer
@@ -50,6 +51,10 @@ public void customize(InstrumenterCustomizer customizer) {
5051
if (isHttpServerInstrumentation(instrumentationName)) {
5152
customizeHttpServer(customizer);
5253
}
54+
55+
if (customizer.hasType(InstrumenterCustomizer.InstrumentationType.HTTP_CLIENT)) {
56+
customizeHttpClient(customizer);
57+
}
5358
}
5459

5560
private boolean isHttpServerInstrumentation(String instrumentationName) {
@@ -68,6 +73,31 @@ private void customizeHttpServer(InstrumenterCustomizer customizer) {
6873
unused -> (SpanNameExtractor<Object>) object -> "CustomHTTP/" + object.toString());
6974
}
7075

76+
private void customizeHttpClient(InstrumenterCustomizer customizer) {
77+
// Simple customization for HTTP client instrumentations
78+
customizer.addAttributesExtractor(new DemoHttpClientAttributesExtractor());
79+
}
80+
81+
/** Custom attributes extractor for HTTP client instrumentations. */
82+
private static class DemoHttpClientAttributesExtractor
83+
implements AttributesExtractor<Object, Object> {
84+
private static final AttributeKey<String> CLIENT_ATTR =
85+
AttributeKey.stringKey("demo.client.type");
86+
87+
@Override
88+
public void onStart(AttributesBuilder attributes, Context context, Object request) {
89+
attributes.put(CLIENT_ATTR, "demo-http-client");
90+
}
91+
92+
@Override
93+
public void onEnd(
94+
AttributesBuilder attributes,
95+
Context context,
96+
Object request,
97+
Object response,
98+
Throwable error) {}
99+
}
100+
71101
/** Custom attributes extractor that adds demo-specific attributes. */
72102
private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> {
73103
private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom");

0 commit comments

Comments
 (0)