javaopen-telemetrygoogle-cloud-trace

Google Cloud Trace Opentelemetry Java example code not showing up on Google Cloud Trace Dashboard


I followed the steps on Google Cloud's Java and OpenTelemetry site (https://cloud.google.com/trace/docs/setup/java-ot) and made a simple hello world Java application locally and am trying to get my traces to show up on Google Cloud Trace using their trace exporter.

All the setup code is the same, and the program compiles and runs successfully. However, I don't see anything on the Trace dashboard. I know it is not an issue with IAM or my service account key because I ran the Python example and it shows up in Cloud Trace dashboard just fine.

Anyone have any guidance on why the Java version could be silently failing?

Thanks

 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();
   }
 }

Solution

  • After some debugging, I figured out the answer.

    Seems like with this simple example, the BatchSpanProcessor is not a good idea because there is only one span that is getting traced.

    SimpleSpanProcessor directly forwards the spans to Cloud Trace no matter what whereas BatchSpanProcessor waits until there is enough data before pushing to Cloud Trace. Hence why I was not seeing anything in Cloud Trace because BatchSpanProcessor hadn't registered enough spans for it to actually upload it to Google Cloud.

    Span Processors Documentation

    Change the following lines

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

    Hope this helps others!