|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/JudgmentLabs/judgeval-go/pkg/tracer" |
| 10 | + "go.opentelemetry.io/otel" |
| 11 | + "go.opentelemetry.io/otel/attribute" |
| 12 | + "go.opentelemetry.io/otel/sdk/resource" |
| 13 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 14 | +) |
| 15 | + |
| 16 | +func processData(ctx context.Context, data string) string { |
| 17 | + _, span := otel.Tracer("data-processor").Start(ctx, "process_data") |
| 18 | + defer span.End() |
| 19 | + |
| 20 | + span.SetAttributes( |
| 21 | + attribute.String("data.input", data), |
| 22 | + attribute.Int("data.length", len(data)), |
| 23 | + ) |
| 24 | + |
| 25 | + time.Sleep(100 * time.Millisecond) |
| 26 | + |
| 27 | + result := fmt.Sprintf("Processed: %s", data) |
| 28 | + span.SetAttributes(attribute.String("data.output", result)) |
| 29 | + |
| 30 | + return result |
| 31 | +} |
| 32 | + |
| 33 | +func initOtel(judgmentTracer *tracer.Tracer) (func(), error) { |
| 34 | + res, err := resource.New(context.Background(), |
| 35 | + resource.WithAttributes( |
| 36 | + attribute.String("service.name", "manual-otel-example"), |
| 37 | + attribute.String("service.version", "1.0.0"), |
| 38 | + attribute.String("telemetry.sdk.name", "opentelemetry"), |
| 39 | + attribute.String("telemetry.sdk.version", "1.0.0"), |
| 40 | + ), |
| 41 | + ) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to create resource: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + var spanExporter sdktrace.SpanExporter |
| 47 | + if judgmentTracer != nil { |
| 48 | + spanExporter = judgmentTracer.GetSpanExporter() |
| 49 | + } |
| 50 | + |
| 51 | + tp := sdktrace.NewTracerProvider( |
| 52 | + sdktrace.WithResource(res), |
| 53 | + sdktrace.WithSpanProcessor(sdktrace.NewBatchSpanProcessor(spanExporter)), |
| 54 | + ) |
| 55 | + |
| 56 | + otel.SetTracerProvider(tp) |
| 57 | + |
| 58 | + return func() { |
| 59 | + time.Sleep(10 * time.Second) |
| 60 | + if err := tp.Shutdown(context.Background()); err != nil { |
| 61 | + log.Printf("Error shutting down tracer provider: %v", err) |
| 62 | + } |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +func main() { |
| 67 | + fmt.Println("Manual OpenTelemetry Instrumentation Example") |
| 68 | + fmt.Println("===========================================") |
| 69 | + |
| 70 | + judgmentTracer, _ := tracer.NewTracer( |
| 71 | + tracer.WithConfiguration(tracer.NewTracerConfiguration( |
| 72 | + tracer.WithProjectName("manual-otel-example"), |
| 73 | + )), |
| 74 | + tracer.WithInitialize(false), |
| 75 | + ) |
| 76 | + |
| 77 | + cleanup, _ := initOtel(judgmentTracer) |
| 78 | + defer cleanup() |
| 79 | + defer judgmentTracer.Shutdown(context.Background()) |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + ctx, rootSpan := otel.Tracer("main").Start(ctx, "manual_instrumentation_example") |
| 84 | + rootSpan.SetAttributes(attribute.String("example.type", "manual_otel")) |
| 85 | + |
| 86 | + result1 := processData(ctx, "Hello, World!") |
| 87 | + fmt.Printf("Result 1: %s\n", result1) |
| 88 | + |
| 89 | + result2 := processData(ctx, "Manual instrumentation") |
| 90 | + fmt.Printf("Result 2: %s\n", result2) |
| 91 | + |
| 92 | + judgmentSpan, _ := judgmentTracer.Span(ctx, "judgment_metrics") |
| 93 | + judgmentTracer.SetGeneralSpan(judgmentSpan) |
| 94 | + judgmentTracer.SetAttribute(judgmentSpan, "service.type", "manual_otel") |
| 95 | + |
| 96 | + requestData := map[string]interface{}{ |
| 97 | + "endpoint": "/api/process", |
| 98 | + "method": "POST", |
| 99 | + "timestamp": time.Now().Unix(), |
| 100 | + } |
| 101 | + judgmentTracer.SetInput(judgmentSpan, requestData) |
| 102 | + |
| 103 | + time.Sleep(50 * time.Millisecond) |
| 104 | + |
| 105 | + responseData := map[string]interface{}{ |
| 106 | + "status_code": 200, |
| 107 | + "response_time_ms": 50, |
| 108 | + "processed_items": 2, |
| 109 | + } |
| 110 | + judgmentTracer.SetOutput(judgmentSpan, responseData) |
| 111 | + judgmentSpan.End() |
| 112 | + |
| 113 | + rootSpan.End() |
| 114 | + |
| 115 | + fmt.Println("\nExample completed!") |
| 116 | +} |
0 commit comments