我已经使用下面的自定义 spanProcessor 和customizerProvider 创建了一个自定义扩展。我在我的 TracerProvider 中提供自定义跨度处理器。
我正在使用以下 cmd 运行我的扩展和 java-agent:
java -javaagent:C:/Users/abc/Desktop/auto-instrumentation/src/opentelemetry-javaagent.jar \
-Dotel.javaagent.extensions=C:/Users/abc/Desktop/auto-instrumentation/src/extension_example-1.0-SNAPSHOT.jar \
-Dotel.exporter.jaeger.endpoint=http://localhost:4317 \
-Dotel.traces.exporter=otlp \
-Dotel.metrics.exporter=none \
-Dotel.resource.attributes="service.name=MyApp" \
-jar MyApp-0.0.1-SNAPSHOT.jar \
我的 jaeger 后端没有显示我的 spanProcessor 属性。它仅显示默认的跨度属性。我什至没有看到任何迹象表明我的扩展已加载。有人可以帮助我吗?
SpanProcessor 类:
package org.example.javaagent;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
public class ExampleSpanProcessor implements SpanProcessor {
@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
span.setAttribute("customAttribute", "customValue");
}
@Override
public boolean isStartRequired() {
return true;
}
@Override
public void onEnd(ReadableSpan span) {}
@Override
public boolean isEndRequired() {
return false;
}
@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}
@Override
public CompletableResultCode forceFlush() {
return CompletableResultCode.ofSuccess();
}
}
CustomizerProvider 类:
package org.example.javaagent;
import com.google.auto.service.AutoService;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.SpanLimits;
import java.util.HashMap;
import java.util.Map;
@AutoService(AutoConfigurationCustomizerProvider.class)
public class ExampleAutoConfigurationCustomizerProvider implements AutoConfigurationCustomizerProvider {
@Override
public void customize(AutoConfigurationCustomizer autoConfiguration) {
System.out.printf("Called customize extension method");
autoConfiguration
.addTracerProviderCustomizer(this::configureSdkTracerProvider)
.addPropertiesSupplier(this::getDefaultProperties);
}
private SdkTracerProviderBuilder configureSdkTracerProvider(
SdkTracerProviderBuilder tracerProvider, ConfigProperties config) {
return tracerProvider
.setSpanLimits(SpanLimits.builder().setMaxNumberOfAttributes(1024).build())
.addSpanProcessor(new ExampleSpanProcessor());
}
private Map<String, String> getDefaultProperties() {
Map<String, String> properties = new HashMap<>();
properties.put("otel.exporter.otlp.endpoint", "http://backend:8080");
properties.put("otel.exporter.otlp.insecure", "true");
properties.put("otel.config.max.attrs", "16");
return properties;
}
}
我点击了
http://localhost:8080/myapp
,但它显示的是由javaagent收集的跨度,而不是我的客户跨度,即customeAttribute: customeValue
。它没有在控制台中显示我的扩展被调用的打印语句。那么我如何知道我的扩展是否被执行
正如评论中已经说过的,https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/examples/extension是创建扩展的正确起点。
@AutoService
使用注释处理器创建 查找文件 - 也许缺少这个 - 但手动创建查找文件完全没问题