如何使用 opentelemetry java sdk 创建多个服务?
How to create multiple services using opentelemetry java sdk?
我有一个应用程序,它由多个使用专有语言构建的服务组成。我想收集痕迹并摄取到 Jaeger 或 APM 解决方案中。没有仪器库。但是,这些服务以专有格式生成跟踪。我想将这些跟踪转换为 OpenTelemetry 跟踪并摄取到 Jaeger 或 APM 解决方案中。
我开始使用 OpenTelemetry-Java SDK。
// This is a just converter service, trying to convert proprietary traces to OLTP
// Not interested in instrumenting this converter service. Following code does not show the proprietary trace reading and conversion for brevity.
// Converter service - reading first service
private static void convert() {
OpenTelemetry openTelemetry = ExampleConfiguration.initOpenTelemetry();
Tracer tracer = openTelemetry.getTracer("io.opentelemetry.example");
Span exampleSpan = tracer.spanBuilder("exampleSpan").startSpan();
childOne(exampleSpan, openTelemetry);
...
}
// Converter service - reading second service
private static void childOne(Span parentSpan, OpenTelemetry openTelemetry) {
Tracer tracer = openTelemetry.getTracer("service2");
Span childSpan = tracer.spanBuilder("child").setParent(Context.current().with(parentSpan)).startSpan();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
childSpan.end();
}
配置了合适的出口商。正如预期的那样,我可以在 Jeager 中看到痕迹。
但它只显示一项服务。
而我想描述两种不同的服务。可以使用 OpenTelemetry 吗?
您可以在代码中设置服务名称如下:
Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));
// Set to process the spans by the Jaeger Exporter
SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(jaegerExporter))
.setResource(Resource.getDefault().merge(serviceNameResource))
.build();
OpenTelemetrySdk openTelemetry =
OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build();
根据 Opentelemetry Java 社区提供的一些提示,创建了两个提供商,它们确实创建了两个服务。重复使用同一个导出器,这样就可以避免到后端的多个连接。未来几天,我将了解更多有关重用导出器在同一应用程序中创建两个或更多供应的信息。
OpenTelemetry openTelemetryParent = ExampleConfiguration.initOpenTelemetry(jaegerHostName, jaegerPort,
"FirstService");
Tracer tracerParent = openTelemetryParent.getTracer("FirstServiceInstrumentation");
OpenTelemetry openTelemetryChild = ExampleConfiguration.initOpenTelemetry(jaegerHostName, jaegerPort,
"SecondService");
Tracer tracerChild = openTelemetryChild.getTracer("SecondServiceInstrumentation");
Random randomNum = new Random();
for (int i = 0; i < 10; i++) {
Span parentSpan = tracerParent.spanBuilder("MyService1Span").startSpan();
try (Scope scope = parentSpan.makeCurrent()) {
Thread.sleep(1 + randomNum.nextInt(100));
secondService(parentSpan, tracerChild, randomNum.nextInt(300));
Thread.sleep(1 + randomNum.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
parentSpan.end();
}
}
}
private static void secondService(Span parentSpan, Tracer tracer, int delay) {
Span childSpan = tracer.spanBuilder("MyService2Span").setParent(Context.current().with(parentSpan)).startSpan();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
childSpan.end();
}
感谢出色的 Opentelemetry java 社区 - @John Watson (jkwatson)、Bogdan Drutu、Rupinder Singh、Anuraag Agrawal。
我有一个应用程序,它由多个使用专有语言构建的服务组成。我想收集痕迹并摄取到 Jaeger 或 APM 解决方案中。没有仪器库。但是,这些服务以专有格式生成跟踪。我想将这些跟踪转换为 OpenTelemetry 跟踪并摄取到 Jaeger 或 APM 解决方案中。
我开始使用 OpenTelemetry-Java SDK。
// This is a just converter service, trying to convert proprietary traces to OLTP
// Not interested in instrumenting this converter service. Following code does not show the proprietary trace reading and conversion for brevity.
// Converter service - reading first service
private static void convert() {
OpenTelemetry openTelemetry = ExampleConfiguration.initOpenTelemetry();
Tracer tracer = openTelemetry.getTracer("io.opentelemetry.example");
Span exampleSpan = tracer.spanBuilder("exampleSpan").startSpan();
childOne(exampleSpan, openTelemetry);
...
}
// Converter service - reading second service
private static void childOne(Span parentSpan, OpenTelemetry openTelemetry) {
Tracer tracer = openTelemetry.getTracer("service2");
Span childSpan = tracer.spanBuilder("child").setParent(Context.current().with(parentSpan)).startSpan();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
childSpan.end();
}
配置了合适的出口商。正如预期的那样,我可以在 Jeager 中看到痕迹。
但它只显示一项服务。
而我想描述两种不同的服务。可以使用 OpenTelemetry 吗?
您可以在代码中设置服务名称如下:
Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));
// Set to process the spans by the Jaeger Exporter
SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(jaegerExporter))
.setResource(Resource.getDefault().merge(serviceNameResource))
.build();
OpenTelemetrySdk openTelemetry =
OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build();
根据 Opentelemetry Java 社区提供的一些提示,创建了两个提供商,它们确实创建了两个服务。重复使用同一个导出器,这样就可以避免到后端的多个连接。未来几天,我将了解更多有关重用导出器在同一应用程序中创建两个或更多供应的信息。
OpenTelemetry openTelemetryParent = ExampleConfiguration.initOpenTelemetry(jaegerHostName, jaegerPort,
"FirstService");
Tracer tracerParent = openTelemetryParent.getTracer("FirstServiceInstrumentation");
OpenTelemetry openTelemetryChild = ExampleConfiguration.initOpenTelemetry(jaegerHostName, jaegerPort,
"SecondService");
Tracer tracerChild = openTelemetryChild.getTracer("SecondServiceInstrumentation");
Random randomNum = new Random();
for (int i = 0; i < 10; i++) {
Span parentSpan = tracerParent.spanBuilder("MyService1Span").startSpan();
try (Scope scope = parentSpan.makeCurrent()) {
Thread.sleep(1 + randomNum.nextInt(100));
secondService(parentSpan, tracerChild, randomNum.nextInt(300));
Thread.sleep(1 + randomNum.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
parentSpan.end();
}
}
}
private static void secondService(Span parentSpan, Tracer tracer, int delay) {
Span childSpan = tracer.spanBuilder("MyService2Span").setParent(Context.current().with(parentSpan)).startSpan();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
childSpan.end();
}
感谢出色的 Opentelemetry java 社区 - @John Watson (jkwatson)、Bogdan Drutu、Rupinder Singh、Anuraag Agrawal。