0

我有一个应用程序,它由以专有语言构建的多个服务组成。我想收集跟踪并摄取到 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 吗?

在此处输入图像描述

4

2 回答 2

0

基于 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。

于 2021-03-07T17:57:21.240 回答
0

您可以在代码中设置服务名称,如下所示:

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();
于 2021-03-05T13:54:27.717 回答