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..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 @@ -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..f9e1e96727b3 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,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");