2

我按照 Google Cloud 的 Java 和 OpenTelemetry 站点 ( https://cloud.google.com/trace/docs/setup/java-ot ) 上的步骤,在本地制作了一个简单的 hello world Java 应用程序,并试图让我的踪迹显示出来使用他们的跟踪导出器在 Google Cloud Trace 上运行。

所有设置代码都是一样的,程序编译运行成功。但是,我在 Trace 仪表板上看不到任何内容。我知道这不是 IAM 或我的服务帐户密钥的问题,因为我运行了 Python 示例,它很好地显示在 Cloud Trace 仪表板中。

任何人都对为什么 Java 版本可能会默默失败有任何指导吗?

谢谢

 package hello;
 import org.joda.time.LocalTime;
 import io.opentelemetry.api.GlobalOpenTelemetry;
 import io.opentelemetry.api.trace.Span;
 import io.opentelemetry.api.trace.Tracer;
 import io.opentelemetry.context.Scope;
 import io.opentelemetry.api.OpenTelemetry;
 import io.opentelemetry.api.common.AttributeKey;
 import io.opentelemetry.api.common.Attributes;
 import io.opentelemetry.api.trace.Span;
 import io.opentelemetry.api.trace.StatusCode;
 import io.opentelemetry.api.trace.Tracer;
 import io.opentelemetry.context.Scope;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.SdkTracerProvider;
 import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
 import io.opentelemetry.exporter.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
 import io.opentelemetry.context.Context;
 import io.opentelemetry.context.propagation.ContextPropagators;
 import io.opentelemetry.api.metrics.LongCounter;
 import io.opentelemetry.api.metrics.Meter;
 import io.opentelemetry.sdk.metrics.SdkMeterProvider;
 import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
 import java.io.IOException;
 import java.util.Random;
 import com.google.cloud.opentelemetry.trace.TraceConfiguration;
 import com.google.cloud.opentelemetry.trace.TraceExporter;
 import java.util.Collections;
 import static java.util.Collections.singleton;
 import java.time.Duration;

 public class HelloWorld {
   private static final Random random = new Random();
   private static OpenTelemetry setupTraceExporter() {
     try {
       
       TraceExporter traceExporter = TraceExporter.createWithConfiguration(
               TraceConfiguration.builder().setProjectId("my-test-id").build());
       // Register the TraceExporter with OpenTelemetry
       return OpenTelemetrySdk.builder()
               .setTracerProvider(
                       SdkTracerProvider.builder()
                               .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())
                               .build())
               .buildAndRegisterGlobal();
     } catch (IOException e) {
       System.out.println("Uncaught Exception");
       System.out.println(e);
       return null;
     }
   }
   public static void main(String[] args) {
     System.out.println("Starting the example application");
     /* SET UP */
     OpenTelemetry otel = setupTraceExporter();
     /* Creating tracer */
     Tracer tracer =
             otel.getTracer("java foo");
     Span span = tracer.spanBuilder("my span").startSpan();
     // put the span into the current Context
     try (Scope scope = span.makeCurrent()) {
       System.out.println("Hello");
       Thread.sleep(4000);
     } catch (Throwable t) {
       span.setStatus(StatusCode.ERROR, "error");
       System.out.println(t);
     } finally {
       span.end(); 
     }
     System.out.println("Closing");
     //otel.getSdkTracerProvider().shutdown();
   }
 }
4

1 回答 1

1

经过一番调试,我想出了答案。

似乎对于这个简单的示例,这BatchSpanProcessor不是一个好主意,因为只有一个跨度被跟踪。

SimpleSpanProcessor无论如何都直接将 span 转发到 Cloud Trace,而BatchSpanProcessor在推送到 Cloud Trace 之前等到有足够的数据。因此,为什么我没有在 Cloud Trace 中看到任何内容,因为BatchSpanProcessor没有注册足够的 span 以将其实际上传到 Google Cloud。

跨度处理器文档

更改以下行

      return OpenTelemetrySdk.builder()
          .setTracerProvider(
                  SdkTracerProvider.builder()
                          .addSpanProcessor(SimpleSpanProcessor.create(traceExporter))
                          .build())
          .buildAndRegisterGlobal();

希望这对其他人有帮助!

于 2021-08-26T20:03:33.020 回答