-
Notifications
You must be signed in to change notification settings - Fork 1k
Support extensions for attributesExtractors, contextCustomizers, operationListeners and spanNameExtractor #13917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
trask
merged 23 commits into
open-telemetry:main
from
steverao:support-custom-extention
Aug 14, 2025
Merged
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7b4c56d
Support extensions for attributesExtractors, contextCustomizers and o…
steverao 8ba2bf0
Fix failing test
steverao 84b1c70
Merge branch 'main' into support-custom-extention
steverao 5531d16
Fix failing CI
steverao 35da705
Add necessary description of classes
steverao 30182a3
Address review comments
steverao 6972f96
Address review comments
steverao 59662da
Moved API to internal directory
steverao 798ba56
Moved API to internal directory
steverao 611aa90
Moved API to internal directory
steverao 891822a
Fix CI failing
steverao 7ed6e43
Add support for spanNameExtractor
steverao da46f57
Support extensions for spanNameExtractor
steverao 7109962
Optimize codes
steverao 159e53d
Refactor extension to be consistent with existence.
steverao e7d8de3
Move customizer api to incubator module (#9)
laurit fb9d6c6
Optimize codes
steverao 5c0489c
Add example
steverao f11181d
Removed unexpected part in example
steverao 47bf9da
Fix formatting in DemoInstrumenterCustomizerProvider
steverao 49a2e90
Address review comments
steverao 26e1675
Add missing imports in InstrumenterCustomizer
steverao 5ca4e61
Update README to clarify customization conditions based on instrument…
steverao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...distro/custom/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.example.javaagent; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextKey; | ||
| import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizer; | ||
| import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizerProvider; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.ContextCustomizer; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * This example demonstrates how to use the InstrumenterCustomizerProvider SPI to customize | ||
| * instrumentation behavior in a custom distribution. | ||
| * | ||
| * <p>This customizer adds: | ||
| * | ||
| * <ul> | ||
| * <li>Custom attributes to HTTP server spans | ||
| * <li>Custom metrics for HTTP operations | ||
| * <li>Request correlation IDs via context customization | ||
| * <li>Custom span name transformation | ||
| * </ul> | ||
| * | ||
| * <p>The customizer will be automatically applied to instrumenters that match the specified | ||
| * instrumentation name and span kind. | ||
| * | ||
| * @see InstrumenterCustomizerProvider | ||
| * @see InstrumenterCustomizer | ||
| */ | ||
| public class DemoInstrumenterCustomizerProvider implements InstrumenterCustomizerProvider { | ||
|
|
||
| @Override | ||
| public void customize(InstrumenterCustomizer customizer) { | ||
| String instrumentationName = customizer.getInstrumentationName(); | ||
| if (isHttpServerInstrumentation(instrumentationName)) { | ||
| customizeHttpServer(customizer); | ||
| } | ||
| } | ||
|
|
||
| private boolean isHttpServerInstrumentation(String instrumentationName) { | ||
| return instrumentationName.contains("servlet") | ||
| || instrumentationName.contains("jetty") | ||
| || instrumentationName.contains("tomcat") | ||
| || instrumentationName.contains("undertow") | ||
| || instrumentationName.contains("spring-webmvc"); | ||
| } | ||
|
|
||
| private void customizeHttpServer(InstrumenterCustomizer customizer) { | ||
| customizer.addAttributesExtractor(new DemoAttributesExtractor()); | ||
| customizer.addOperationMetrics(new DemoMetrics()); | ||
| customizer.addContextCustomizer(new DemoContextCustomizer()); | ||
| customizer.setSpanNameExtractor( | ||
| unused -> (SpanNameExtractor<Object>) object -> "CustomHTTP/" + object.toString()); | ||
| } | ||
|
|
||
| /** Custom attributes extractor that adds demo-specific attributes. */ | ||
| private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> { | ||
| private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom"); | ||
| private static final AttributeKey<String> ERROR_ATTR = AttributeKey.stringKey("demo.error"); | ||
|
|
||
| @Override | ||
| public void onStart(AttributesBuilder attributes, Context context, Object request) { | ||
| attributes.put(CUSTOM_ATTR, "demo-distro"); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnd( | ||
| AttributesBuilder attributes, | ||
| Context context, | ||
| Object request, | ||
| Object response, | ||
| Throwable error) { | ||
| if (error != null) { | ||
| attributes.put(ERROR_ATTR, error.getClass().getSimpleName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Custom metrics that track request counts. */ | ||
| private static class DemoMetrics implements OperationMetrics { | ||
| @Override | ||
| public OperationListener create(Meter meter) { | ||
| LongCounter requestCounter = | ||
| meter | ||
| .counterBuilder("demo.requests") | ||
| .setDescription("Number of requests") | ||
| .setUnit("requests") | ||
| .build(); | ||
|
|
||
| return new OperationListener() { | ||
| @Override | ||
| public Context onStart(Context context, Attributes attributes, long startNanos) { | ||
| requestCounter.add(1, attributes); | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnd(Context context, Attributes attributes, long endNanos) { | ||
| // Could add duration metrics here if needed | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** Context customizer that adds request correlation IDs and custom context data. */ | ||
| private static class DemoContextCustomizer implements ContextCustomizer<Object> { | ||
| private static final AtomicLong requestIdCounter = new AtomicLong(1); | ||
| private static final ContextKey<String> REQUEST_ID_KEY = ContextKey.named("demo.request.id"); | ||
|
|
||
| @Override | ||
| public Context onStart(Context context, Object request, Attributes startAttributes) { | ||
| // Generate a unique request ID for correlation | ||
| String requestId = "req-" + requestIdCounter.getAndIncrement(); | ||
|
|
||
| // Add custom context data that can be accessed throughout the request lifecycle | ||
| // This follows the pattern used in real implementations like UndertowSingletons | ||
steverao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| context = context.with(REQUEST_ID_KEY, requestId); | ||
| return context; | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...o.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizerProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.example.javaagent.DemoInstrumenterCustomizerProvider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...les/extension/src/main/java/com/example/javaagent/DemoInstrumenterCustomizerProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.example.javaagent; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextKey; | ||
| import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizer; | ||
| import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizerProvider; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.ContextCustomizer; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * This example demonstrates how to use the InstrumenterCustomizerProvider SPI to customize | ||
| * instrumentation behavior without modifying the core instrumentation code. | ||
| * | ||
| * <p>This customizer adds: | ||
| * | ||
| * <ul> | ||
| * <li>Custom attributes to HTTP server spans | ||
| * <li>Custom metrics for HTTP operations | ||
| * <li>Request correlation IDs via context customization | ||
| * <li>Custom span name transformation | ||
| * </ul> | ||
| * | ||
| * <p>The customizer will be automatically applied to instrumenters that match the specified | ||
| * instrumentation name and span kind. | ||
| * | ||
| * @see InstrumenterCustomizerProvider | ||
| * @see InstrumenterCustomizer | ||
| */ | ||
| @AutoService(InstrumenterCustomizerProvider.class) | ||
| public class DemoInstrumenterCustomizerProvider implements InstrumenterCustomizerProvider { | ||
|
|
||
| @Override | ||
| public void customize(InstrumenterCustomizer customizer) { | ||
| String instrumentationName = customizer.getInstrumentationName(); | ||
| if (isHttpServerInstrumentation(instrumentationName)) { | ||
| customizeHttpServer(customizer); | ||
| } | ||
| } | ||
|
|
||
| private boolean isHttpServerInstrumentation(String instrumentationName) { | ||
| return instrumentationName.contains("servlet") | ||
| || instrumentationName.contains("jetty") | ||
| || instrumentationName.contains("tomcat") | ||
| || instrumentationName.contains("undertow") | ||
| || instrumentationName.contains("spring-webmvc"); | ||
| } | ||
|
|
||
| private void customizeHttpServer(InstrumenterCustomizer customizer) { | ||
| customizer.addAttributesExtractor(new DemoAttributesExtractor()); | ||
| customizer.addOperationMetrics(new DemoMetrics()); | ||
| customizer.addContextCustomizer(new DemoContextCustomizer()); | ||
| customizer.setSpanNameExtractor( | ||
| unused -> (SpanNameExtractor<Object>) object -> "CustomHTTP/" + object.toString()); | ||
| } | ||
|
|
||
| /** Custom attributes extractor that adds demo-specific attributes. */ | ||
| private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> { | ||
| private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom"); | ||
| private static final AttributeKey<String> ERROR_ATTR = AttributeKey.stringKey("demo.error"); | ||
|
|
||
| @Override | ||
| public void onStart(AttributesBuilder attributes, Context context, Object request) { | ||
| attributes.put(CUSTOM_ATTR, "demo-extension"); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnd( | ||
| AttributesBuilder attributes, | ||
| Context context, | ||
| Object request, | ||
| Object response, | ||
| Throwable error) { | ||
| if (error != null) { | ||
| attributes.put(ERROR_ATTR, error.getClass().getSimpleName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Custom metrics that track request counts. */ | ||
| private static class DemoMetrics implements OperationMetrics { | ||
| @Override | ||
| public OperationListener create(Meter meter) { | ||
| LongCounter requestCounter = | ||
| meter | ||
| .counterBuilder("demo.requests") | ||
| .setDescription("Number of requests") | ||
| .setUnit("requests") | ||
| .build(); | ||
|
|
||
| return new OperationListener() { | ||
| @Override | ||
| public Context onStart(Context context, Attributes attributes, long startNanos) { | ||
| requestCounter.add(1, attributes); | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnd(Context context, Attributes attributes, long endNanos) { | ||
| // Could add duration metrics here if needed | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** Context customizer that adds request correlation IDs and custom context data. */ | ||
| private static class DemoContextCustomizer implements ContextCustomizer<Object> { | ||
| private static final AtomicLong requestIdCounter = new AtomicLong(1); | ||
| private static final ContextKey<String> REQUEST_ID_KEY = ContextKey.named("demo.request.id"); | ||
|
|
||
| @Override | ||
| public Context onStart(Context context, Object request, Attributes startAttributes) { | ||
| // Generate a unique request ID for correlation | ||
| String requestId = "req-" + requestIdCounter.getAndIncrement(); | ||
|
|
||
| // Add custom context data that can be accessed throughout the request lifecycle | ||
| // This follows the pattern used in real implementations like UndertowSingletons | ||
| context = context.with(REQUEST_ID_KEY, requestId); | ||
| return context; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.