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

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

import java.io.IOException;

public class TraceExample {
  private TraceExporter traceExporter;

  // OTel API
  private Tracer tracer = OpenTelemetry.getTracer("io.opentelemetry.example.TraceExample");

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

    try {
      this.traceExporter = TraceExporter.createWithConfiguration(configuration);
    } catch(IOException e) {
      System.out.println("Uncaught Excetption");
    }

    OpenTelemetrySdk.getTracerProvider()
            .addSpanProcessor(SimpleSpanProcessor.newBuilder(this.traceExporter).build());
    System.out.println("Set up");
  }

  private void myUseCase() {
    // Generate a span
    Span span = this.tracer.spanBuilder("Start my wonderful use case").startSpan();
    span.addEvent("Event 0");
    // execute my use case - here we simulate a wait
    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) {
    TraceExample example = new TraceExample();
    example.setupTraceExporter();

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    System.out.println("Done");

  }
}

@@ -175,61 +175,8 @@
 er;%0A
-import com.google.cloud.trace.v2.TraceServiceClient;%0A
 impo
@@ -521,22 +521,8 @@
 r;%0A%0A
-  // OTel API%0A
   pr
@@ -652,13 +652,8 @@
 ) %7B%0A
-    %0A
     
@@ -891,93 +891,11 @@
 n);%0A
+%0A
   
-  %7D catch(IOException e) %7B%0A      System.out.println(%22Uncaught Excetption%22);%0A    %7D%0A%0A
     
@@ -931,16 +931,18 @@
 vider()%0A
+  
         
@@ -1032,37 +1032,88 @@
     
-System.out.println(%22Set up%22);
+%7D catch(IOException e) %7B%0A      System.out.println(%22Uncaught Excetption%22);%0A    %7D%0A
 %0A  %7D
@@ -1154,9 +1154,9 @@
  // 
-G
+g
 ener
@@ -1220,18 +1220,8 @@
  my 
-wonderful 
 use 
@@ -1280,38 +1280,8 @@
   //
- execute my use case - here we
  sim
@@ -1290,14 +1290,12 @@
 ate 
-a wait
+work
 %0A   
@@ -1624,30 +1624,99 @@
 ter();%0A%0A    
-try %7B%0A
+while(true) %7B%0A      example.myUseCase();%0A      try %7B%0A        // one hour%0A  
       Thread
@@ -1722,27 +1722,39 @@
 d.sleep(1000
+ * 60 * 60
 );%0A
+  
     %7D catch 
@@ -1788,41 +1788,17 @@
     
+  
 %7D%0A    
-System.out.println(%22Done%22);
+%7D
 %0A%0A  

Add span code to TraceExample