Skip to content

Commit 6cfaaa1

Browse files
committed
Update examples for InstrumenterCustomizerProvider extension
1 parent 2d498b9 commit 6cfaaa1

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 31 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,30 @@ 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 implements AttributesExtractor<Object, Object> {
83+
private static final AttributeKey<String> CLIENT_ATTR = AttributeKey.stringKey("demo.client.type");
84+
85+
@Override
86+
public void onStart(AttributesBuilder attributes, Context context, Object request) {
87+
attributes.put(CLIENT_ATTR, "demo-http-client");
88+
}
89+
90+
@Override
91+
public void onEnd(
92+
AttributesBuilder attributes,
93+
Context context,
94+
Object request,
95+
Object response,
96+
Throwable error) {
97+
}
98+
}
99+
71100
/** Custom attributes extractor that adds demo-specific attributes. */
72101
private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> {
73102
private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom");

0 commit comments

Comments
 (0)