|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package com.example.javaagent; |
| 7 | + |
| 8 | +import com.google.auto.service.AutoService; |
| 9 | +import io.opentelemetry.api.common.AttributeKey; |
| 10 | +import io.opentelemetry.api.common.Attributes; |
| 11 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 12 | +import io.opentelemetry.api.metrics.LongCounter; |
| 13 | +import io.opentelemetry.api.metrics.Meter; |
| 14 | +import io.opentelemetry.context.Context; |
| 15 | +import io.opentelemetry.context.ContextKey; |
| 16 | +import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizer; |
| 17 | +import io.opentelemetry.instrumentation.api.incubator.instrumenter.InstrumenterCustomizerProvider; |
| 18 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 19 | +import io.opentelemetry.instrumentation.api.instrumenter.ContextCustomizer; |
| 20 | +import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; |
| 21 | +import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; |
| 22 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | + |
| 25 | +/** |
| 26 | + * This example demonstrates how to use the InstrumenterCustomizerProvider SPI to customize |
| 27 | + * instrumentation behavior without modifying the core instrumentation code. |
| 28 | + * |
| 29 | + * <p>This customizer adds: |
| 30 | + * |
| 31 | + * <ul> |
| 32 | + * <li>Custom attributes to HTTP server spans |
| 33 | + * <li>Custom metrics for HTTP operations |
| 34 | + * <li>Request correlation IDs via context customization |
| 35 | + * <li>Custom span name transformation |
| 36 | + * </ul> |
| 37 | + * |
| 38 | + * <p>The customizer will be automatically applied to instrumenters that match the specified |
| 39 | + * instrumentation name and span kind. |
| 40 | + * |
| 41 | + * @see InstrumenterCustomizerProvider |
| 42 | + * @see InstrumenterCustomizer |
| 43 | + */ |
| 44 | +@AutoService(InstrumenterCustomizerProvider.class) |
| 45 | +public class DemoInstrumenterCustomizerProvider implements InstrumenterCustomizerProvider { |
| 46 | + |
| 47 | + @Override |
| 48 | + public void customize(InstrumenterCustomizer customizer) { |
| 49 | + String instrumentationName = customizer.getInstrumentationName(); |
| 50 | + if (isHttpServerInstrumentation(instrumentationName)) { |
| 51 | + customizeHttpServer(customizer); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private boolean isHttpServerInstrumentation(String instrumentationName) { |
| 56 | + return instrumentationName.contains("servlet") |
| 57 | + || instrumentationName.contains("jetty") |
| 58 | + || instrumentationName.contains("tomcat") |
| 59 | + || instrumentationName.contains("undertow") |
| 60 | + || instrumentationName.contains("spring-webmvc"); |
| 61 | + } |
| 62 | + |
| 63 | + private void customizeHttpServer(InstrumenterCustomizer customizer) { |
| 64 | + customizer.addAttributesExtractor(new DemoAttributesExtractor()); |
| 65 | + customizer.addOperationMetrics(new DemoMetrics()); |
| 66 | + customizer.addContextCustomizer(new DemoContextCustomizer()); |
| 67 | + customizer.setSpanNameExtractor( |
| 68 | + unused -> (SpanNameExtractor<Object>) object -> "CustomHTTP/" + object.toString()); |
| 69 | + } |
| 70 | + |
| 71 | + /** Custom attributes extractor that adds demo-specific attributes. */ |
| 72 | + private static class DemoAttributesExtractor implements AttributesExtractor<Object, Object> { |
| 73 | + private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("demo.custom"); |
| 74 | + private static final AttributeKey<String> ERROR_ATTR = AttributeKey.stringKey("demo.error"); |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onStart(AttributesBuilder attributes, Context context, Object request) { |
| 78 | + attributes.put(CUSTOM_ATTR, "demo-extension"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onEnd( |
| 83 | + AttributesBuilder attributes, |
| 84 | + Context context, |
| 85 | + Object request, |
| 86 | + Object response, |
| 87 | + Throwable error) { |
| 88 | + if (error != null) { |
| 89 | + attributes.put(ERROR_ATTR, error.getClass().getSimpleName()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** Custom metrics that track request counts. */ |
| 95 | + private static class DemoMetrics implements OperationMetrics { |
| 96 | + @Override |
| 97 | + public OperationListener create(Meter meter) { |
| 98 | + LongCounter requestCounter = |
| 99 | + meter |
| 100 | + .counterBuilder("demo.requests") |
| 101 | + .setDescription("Number of requests") |
| 102 | + .setUnit("requests") |
| 103 | + .build(); |
| 104 | + |
| 105 | + return new OperationListener() { |
| 106 | + @Override |
| 107 | + public Context onStart(Context context, Attributes attributes, long startNanos) { |
| 108 | + requestCounter.add(1, attributes); |
| 109 | + return context; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onEnd(Context context, Attributes attributes, long endNanos) { |
| 114 | + // Could add duration metrics here if needed |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** Context customizer that adds request correlation IDs and custom context data. */ |
| 121 | + private static class DemoContextCustomizer implements ContextCustomizer<Object> { |
| 122 | + private static final AtomicLong requestIdCounter = new AtomicLong(1); |
| 123 | + private static final ContextKey<String> REQUEST_ID_KEY = ContextKey.named("demo.request.id"); |
| 124 | + |
| 125 | + @Override |
| 126 | + public Context onStart(Context context, Object request, Attributes startAttributes) { |
| 127 | + // Generate a unique request ID for correlation |
| 128 | + String requestId = "req-" + requestIdCounter.getAndIncrement(); |
| 129 | + |
| 130 | + // Add custom context data that can be accessed throughout the request lifecycle |
| 131 | + context = context.with(REQUEST_ID_KEY, requestId); |
| 132 | + return context; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments