From 6cfaaa1b7a3dae754a52d18c9923784cf0e58593 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Fri, 7 Nov 2025 10:48:37 +0800 Subject: [PATCH 1/3] Update examples for InstrumenterCustomizerProvider extension --- examples/distro/README.md | 2 +- .../DemoInstrumenterCustomizerProvider.java | 29 ++++++++++++++++ examples/extension/README.md | 2 +- .../DemoInstrumenterCustomizerProvider.java | 33 +++++++++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/examples/distro/README.md b/examples/distro/README.md index c35d49d88468..7ad6355590e4 100644 --- a/examples/distro/README.md +++ b/examples/distro/README.md @@ -44,7 +44,7 @@ The `InstrumenterCustomizerProvider` extension point allows you to customize ins - Add custom attributes and metrics to existing instrumentations - Customize context - Transform span names to match your naming conventions -- Apply customizations conditionally based on instrumentation name +- Apply customizations conditionally based on instrumentation name or type (HTTP client, HTTP server, DB client, etc.) ### I don't want this span at all diff --git a/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java b/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java index f2669d9041b2..003ab9186206 100644 --- a/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java +++ b/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java @@ -48,6 +48,10 @@ public void customize(InstrumenterCustomizer customizer) { if (isHttpServerInstrumentation(instrumentationName)) { customizeHttpServer(customizer); } + + if (customizer.hasType(InstrumenterCustomizer.InstrumentationType.HTTP_CLIENT)) { + customizeHttpClient(customizer); + } } private boolean isHttpServerInstrumentation(String instrumentationName) { @@ -66,6 +70,31 @@ private void customizeHttpServer(InstrumenterCustomizer customizer) { unused -> (SpanNameExtractor) object -> "CustomHTTP/" + object.toString()); } + private void customizeHttpClient(InstrumenterCustomizer customizer) { + // Simple customization for HTTP client instrumentations + customizer.addAttributesExtractor(new DemoHttpClientAttributesExtractor()); + } + + /** Custom attributes extractor for HTTP client instrumentations. */ + private static class DemoHttpClientAttributesExtractor + implements AttributesExtractor { + private static final AttributeKey CLIENT_ATTR = + AttributeKey.stringKey("demo.client.type"); + + @Override + public void onStart(AttributesBuilder attributes, Context context, Object request) { + attributes.put(CLIENT_ATTR, "demo-http-client"); + } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + Object request, + Object response, + Throwable error) {} + } + /** Custom attributes extractor that adds demo-specific attributes. */ private static class DemoAttributesExtractor implements AttributesExtractor { private static final AttributeKey CUSTOM_ATTR = AttributeKey.stringKey("demo.custom"); diff --git a/examples/extension/README.md b/examples/extension/README.md index fdd99a1fec16..ad16a0801a9a 100644 --- a/examples/extension/README.md +++ b/examples/extension/README.md @@ -74,7 +74,7 @@ The `InstrumenterCustomizerProvider` extension point allows you to customize ins - Add custom attributes and metrics to existing instrumentations - Customize context - Transform span names to match your naming conventions -- Apply customizations conditionally based on instrumentation name +- Apply customizations conditionally based on instrumentation name or type (HTTP client, HTTP server, DB client, etc.) ### "I don't want this span at all" diff --git a/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java b/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java index 411558ecce1e..0815396ac3b7 100644 --- a/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java +++ b/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java @@ -29,14 +29,15 @@ *

This customizer adds: * *

    - *
  • Custom attributes to HTTP server spans + *
  • Custom attributes to HTTP server spans (based on instrumentation name) + *
  • Custom attributes to HTTP client spans (based on instrumentation type) *
  • Custom metrics for HTTP operations *
  • Request correlation IDs via context customization *
  • Custom span name transformation *
* *

The customizer will be automatically applied to instrumenters that match the specified - * instrumentation name and span kind. + * instrumentation name or type. * * @see InstrumenterCustomizerProvider * @see InstrumenterCustomizer @@ -50,6 +51,10 @@ public void customize(InstrumenterCustomizer customizer) { if (isHttpServerInstrumentation(instrumentationName)) { customizeHttpServer(customizer); } + + if (customizer.hasType(InstrumenterCustomizer.InstrumentationType.HTTP_CLIENT)) { + customizeHttpClient(customizer); + } } private boolean isHttpServerInstrumentation(String instrumentationName) { @@ -68,6 +73,30 @@ private void customizeHttpServer(InstrumenterCustomizer customizer) { unused -> (SpanNameExtractor) object -> "CustomHTTP/" + object.toString()); } + private void customizeHttpClient(InstrumenterCustomizer customizer) { + // Simple customization for HTTP client instrumentations + customizer.addAttributesExtractor(new DemoHttpClientAttributesExtractor()); + } + + /** Custom attributes extractor for HTTP client instrumentations. */ + private static class DemoHttpClientAttributesExtractor implements AttributesExtractor { + private static final AttributeKey CLIENT_ATTR = AttributeKey.stringKey("demo.client.type"); + + @Override + public void onStart(AttributesBuilder attributes, Context context, Object request) { + attributes.put(CLIENT_ATTR, "demo-http-client"); + } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + Object request, + Object response, + Throwable error) { + } + } + /** Custom attributes extractor that adds demo-specific attributes. */ private static class DemoAttributesExtractor implements AttributesExtractor { private static final AttributeKey CUSTOM_ATTR = AttributeKey.stringKey("demo.custom"); From 2c5b5a5074eed5b1fcaa7f6234705e10f0322b33 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Fri, 7 Nov 2025 10:55:22 +0800 Subject: [PATCH 2/3] Update examples for InstrumenterCustomizerProvider extension --- .../example/javaagent/DemoInstrumenterCustomizerProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java b/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java index 003ab9186206..e57e8d5e146a 100644 --- a/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java +++ b/examples/distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java @@ -35,7 +35,7 @@ * * *

The customizer will be automatically applied to instrumenters that match the specified - * instrumentation name and span kind. + * instrumentation name or type. * * @see InstrumenterCustomizerProvider * @see InstrumenterCustomizer From c1b616ade6600ccc952183ad12cdaff086547e52 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Fri, 7 Nov 2025 11:26:52 +0800 Subject: [PATCH 3/3] Fix CI failing --- .../javaagent/DemoInstrumenterCustomizerProvider.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java b/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java index 0815396ac3b7..f9e1e96727b3 100644 --- a/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java +++ b/examples/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java @@ -51,7 +51,7 @@ public void customize(InstrumenterCustomizer customizer) { if (isHttpServerInstrumentation(instrumentationName)) { customizeHttpServer(customizer); } - + if (customizer.hasType(InstrumenterCustomizer.InstrumentationType.HTTP_CLIENT)) { customizeHttpClient(customizer); } @@ -79,8 +79,10 @@ private void customizeHttpClient(InstrumenterCustomizer customizer) { } /** Custom attributes extractor for HTTP client instrumentations. */ - private static class DemoHttpClientAttributesExtractor implements AttributesExtractor { - private static final AttributeKey CLIENT_ATTR = AttributeKey.stringKey("demo.client.type"); + private static class DemoHttpClientAttributesExtractor + implements AttributesExtractor { + private static final AttributeKey CLIENT_ATTR = + AttributeKey.stringKey("demo.client.type"); @Override public void onStart(AttributesBuilder attributes, Context context, Object request) { @@ -93,8 +95,7 @@ public void onEnd( Context context, Object request, Object response, - Throwable error) { - } + Throwable error) {} } /** Custom attributes extractor that adds demo-specific attributes. */