package com.google.cloud.opentelemetry.example.trace;

import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;

import java.io.IOException;
import java.time.Duration;

public class TraceExporterExample {
  private TraceExporter traceExporter;

  private Tracer tracer = OpenTelemetry.getTracer("io.opentelemetry.example.TraceExporterExample");

  private void setupTraceExporter() {
    // using default project ID
    TraceConfiguration configuration =
        TraceConfiguration.builder().setDeadline(Duration.ofMillis(30000)).build();

    try {
      this.traceExporter = TraceExporter.createWithConfiguration(configuration);

      OpenTelemetrySdk.getTracerProvider()
          .addSpanProcessor(SimpleSpanProcessor.newBuilder(this.traceExporter).build());
    } catch (IOException e) {
      System.out.println("Uncaught Excetption");
    }
  }

  private void myUseCase() {
    // Generate a span
    Span span = this.tracer.spanBuilder("Start my use case").startSpan();
    span.addEvent("Event 0");
    // Simulate work
    doWork();
    span.addEvent("Event 1");
    span.end();
  }

  private void doWork() {
    try {
      Thread.sleep((int) (Math.random() * 1000) + 1000);
    } catch (InterruptedException e) {
    }
  }

  public static void main(String[] args) {
    TraceExporterExample example = new TraceExporterExample();
    example.setupTraceExporter();
    example.myUseCase();
  }
}

@@ -671,17 +671,17 @@
 %0A    // 
-u
+U
 sing def
@@ -695,16 +695,32 @@
 oject ID
+ and Credentials
 %0A    Tra
@@ -924,24 +924,79 @@
 guration);%0A%0A
+      // Register the TraceExporter with OpenTelemetry%0A
       OpenTe
@@ -1184,17 +1184,16 @@
 ght Exce
-t
 ption%22);
@@ -1380,16 +1380,91 @@
 ate work
+: this could be simulating a network request or an expensive disk operation
 %0A    doW
@@ -1466,24 +1466,25 @@
   doWork();%0A
+%0A
     span.add
@@ -1615,16 +1615,16 @@
  1000);%0A
-
     %7D ca
@@ -1650,24 +1650,61 @@
 eption e) %7B%0A
+      throw new RuntimeException(e);%0A
     %7D%0A  %7D%0A%0A 

Fix typos in TraceExporterExample Add a runtime error to the sleep try-catch