1010import io .opentelemetry .api .common .AttributesBuilder ;
1111import io .opentelemetry .api .metrics .LongCounter ;
1212import io .opentelemetry .api .metrics .Meter ;
13- import io .opentelemetry .api .trace .SpanKind ;
1413import io .opentelemetry .context .Context ;
14+ import io .opentelemetry .context .ContextKey ;
1515import io .opentelemetry .instrumentation .api .incubator .instrumenter .InstrumenterCustomizer ;
1616import io .opentelemetry .instrumentation .api .incubator .instrumenter .InstrumenterCustomizerProvider ;
1717import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
1818import io .opentelemetry .instrumentation .api .instrumenter .ContextCustomizer ;
1919import io .opentelemetry .instrumentation .api .instrumenter .OperationListener ;
2020import io .opentelemetry .instrumentation .api .instrumenter .OperationMetrics ;
2121import io .opentelemetry .instrumentation .api .instrumenter .SpanNameExtractor ;
22- import io .opentelemetry .context .ContextKey ;
2322import java .util .concurrent .atomic .AtomicLong ;
2423
2524/**
2625 * This example demonstrates how to use the InstrumenterCustomizerProvider SPI to customize
2726 * instrumentation behavior in a custom distribution.
2827 *
2928 * <p>This customizer adds:
29+ *
3030 * <ul>
31- * <li>Custom attributes to HTTP server spans</li>
32- * <li>Custom metrics for HTTP operations</li>
33- * <li>Request correlation IDs via context customization</li>
34- * <li>Custom span name transformation</li>
31+ * <li>Custom attributes to HTTP server spans
32+ * <li>Custom metrics for HTTP operations
33+ * <li>Request correlation IDs via context customization
34+ * <li>Custom span name transformation
3535 * </ul>
3636 *
3737 * <p>The customizer will be automatically applied to instrumenters that match the specified
@@ -77,7 +77,12 @@ public void onStart(AttributesBuilder attributes, Context context, Object reques
7777 }
7878
7979 @ Override
80- public void onEnd (AttributesBuilder attributes , Context context , Object request , Object response , Throwable error ) {
80+ public void onEnd (
81+ AttributesBuilder attributes ,
82+ Context context ,
83+ Object request ,
84+ Object response ,
85+ Throwable error ) {
8186 if (error != null ) {
8287 attributes .put (ERROR_ATTR , error .getClass ().getSimpleName ());
8388 }
@@ -88,10 +93,12 @@ public void onEnd(AttributesBuilder attributes, Context context, Object request,
8893 private static class DemoMetrics implements OperationMetrics {
8994 @ Override
9095 public OperationListener create (Meter meter ) {
91- LongCounter requestCounter = meter .counterBuilder ("demo.requests" )
92- .setDescription ("Number of requests" )
93- .setUnit ("requests" )
94- .build ();
96+ LongCounter requestCounter =
97+ meter
98+ .counterBuilder ("demo.requests" )
99+ .setDescription ("Number of requests" )
100+ .setUnit ("requests" )
101+ .build ();
95102
96103 return new OperationListener () {
97104 @ Override
@@ -117,11 +124,11 @@ private static class DemoContextCustomizer implements ContextCustomizer<Object>
117124 public Context onStart (Context context , Object request , Attributes startAttributes ) {
118125 // Generate a unique request ID for correlation
119126 String requestId = "req-" + requestIdCounter .getAndIncrement ();
120-
127+
121128 // Add custom context data that can be accessed throughout the request lifecycle
122129 // This follows the pattern used in real implementations like UndertowSingletons
123130 context = context .with (REQUEST_ID_KEY , requestId );
124131 return context ;
125132 }
126133 }
127- }
134+ }
0 commit comments