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

import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.io.IOException;
import java.time.Duration;

public class TraceExporterExample {
  private TraceExporter traceExporter;

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

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

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

      // Register the TraceExporter with OpenTelemetry
      OpenTelemetrySdk.getGlobalTracerManagement()
          .addSpanProcessor(SimpleSpanProcessor.builder(this.traceExporter).build());
    } catch (IOException e) {
      System.out.println("Uncaught Exception");
    }
  }

  private void myUseCase() {
    // Generate a span
    Span span = this.tracer.spanBuilder("Start my use case").startSpan();
    span.addEvent("Event 0");
    // Simulate work: this could be simulating a network request or an expensive disk operation
    doWork();

    span.addEvent("Event 1");
    span.end();
  }

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

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

@@ -292,24 +292,63 @@
 ace.Tracer;%0A
+import io.opentelemetry.context.Scope;%0A
 import io.op
@@ -422,22 +422,21 @@
 .export.
-Simple
+Batch
 SpanProc
@@ -496,16 +496,41 @@
 uration;
+%0Aimport java.util.Random;
 %0A%0Apublic
@@ -573,47 +573,20 @@
 ate 
-TraceExporter traceExporter;%0A%0A  private
+static final
  Tra
@@ -597,22 +597,16 @@
 tracer =
-%0A     
  OpenTel
@@ -677,16 +677,69 @@
 ample%22);
+%0A  private static final Random random = new Random();
 %0A%0A  priv
@@ -738,24 +738,31 @@
 %0A%0A  private 
+static 
 void setupTr
@@ -965,21 +965,30 @@
 %7B%0A      
-this.
+TraceExporter 
 traceExp
@@ -1188,14 +1188,13 @@
 sor(
-Simple
+Batch
 Span
@@ -1211,21 +1211,16 @@
 builder(
-this.
 traceExp
@@ -1331,24 +1331,31 @@
 %0A%0A  private 
+static 
 void myUseCa
@@ -1357,16 +1357,34 @@
 UseCase(
+String description
 ) %7B%0A    
@@ -1422,13 +1422,8 @@
 n = 
-this.
 trac
@@ -1441,42 +1441,81 @@
 der(
-%22Start my use case%22).startSpan();%0A
+description).startSpan();%0A    try (Scope scope = span.makeCurrent()) %7B%0A  
     
@@ -1539,124 +1539,191 @@
 ent 
-0
+A
 %22);%0A
+  
     // 
-Simulate work: this could be simulating a network request or an expensive disk operation%0A
+Do some work for the use case%0A      for (int i=0; i%3C3; i++) %7B%0A        String work = String.format(%22%25s - Work #%25d%22, description, (i+1));%0A 
     
+   
 doWork(
-);%0A%0A
+work);%0A      %7D%0A%0A  
     
@@ -1747,13 +1747,31 @@
 ent 
-1
+B
 %22);%0A
+    %7D finally %7B%0A  
     
@@ -1778,24 +1778,30 @@
 span.end();%0A
+    %7D%0A
   %7D%0A%0A  priva
@@ -1803,16 +1803,23 @@
 private 
+static 
 void doW
@@ -1826,74 +1826,298 @@
 ork(
-) %7B%0A    try %7B%0A      Thread.sleep((int) (Math.random() * 
+String description) %7B%0A    // Child span%0A    Span span = tracer.spanBuilder(description).startSpan();%0A    try (Scope scope = span.makeCurrent()) %7B%0A      // Simulate work: this could be simulating a network request or an expensive disk operation%0A      Thread.sleep(
 100
-0)
  + 
+random.nextInt(5) * 
 1
-0
 00);
@@ -2193,16 +2193,50 @@
 ion(e);%0A
+    %7D finally %7B%0A      span.end();%0A
     %7D%0A  
@@ -2289,79 +2289,77 @@
     
-TraceExporterExample example = new 
+// Configure the OpenTelemetry pipeline with Cloud
 Trace
-E
+ e
 xporter
-Example();%0A    example.
+%0A    
 setu
@@ -2380,29 +2380,178 @@
 ();%0A
+%0A
     
-example.myUseCase
+// Application-specific logic%0A    myUseCase(%22One%22);%0A    myUseCase(%22Two%22);%0A%0A    // Flush all buffered traces%0A    OpenTelemetrySdk.getGlobalTracerManagement().shutdown
 ();%0A

Fix and improve the trace example code (#60)