-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (64 loc) · 2.34 KB
/
main.go
File metadata and controls
80 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package main demonstrates distributed tracing using W3C baggage propagation.
//
// This example shows how trace context propagates across service boundaries
// via W3C baggage. A parent span encodes context to headers, and a child span
// extracts it (simulated without an actual HTTP server).
//
// To run this example:
//
// export BRAINTRUST_API_KEY="your-api-key"
// go run main.go
package main
import (
"context"
"fmt"
"log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"github.com/braintrustdata/braintrust-sdk-go"
)
func main() {
// Setup tracing
tp := sdktrace.NewTracerProvider()
defer tp.Shutdown(context.Background()) //nolint:errcheck
// Set global tracer provider
otel.SetTracerProvider(tp)
// Enable W3C baggage propagation globally (required for distributed tracing)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
))
// Create Braintrust client with default project
bt, err := braintrust.New(tp,
braintrust.WithProject("go-sdk-examples"),
braintrust.WithBlockingLogin(true),
)
if err != nil {
log.Fatalf("Failed to initialize Braintrust: %v", err)
}
tracer := otel.Tracer("examples/distributed-tracing")
ctx := context.Background()
// Create parent span
ctx, parentSpan := tracer.Start(ctx, "examples/distributed-tracing/main.go")
defer parentSpan.End()
// Encode context to headers (simulates HTTP request)
headers := make(map[string]string)
otel.GetTextMapPropagator().Inject(ctx, propagation.MapCarrier(headers))
// Call remote service (simulates crossing service boundary)
simulateHTTPRequest(headers)
// Flush all spans
if err := tp.ForceFlush(context.Background()); err != nil {
log.Printf("Failed to flush spans: %v", err)
}
fmt.Printf("\nView span: %s\n", bt.Permalink(parentSpan))
}
// simulateHTTPRequest simulates a remote service receiving an HTTP request
func simulateHTTPRequest(headers map[string]string) {
tracer := otel.Tracer("examples/distributed-tracing")
// Extract context from headers (simulates HTTP handler)
ctx := otel.GetTextMapPropagator().Extract(context.Background(), propagation.MapCarrier(headers))
// Create child span - inherits trace context from parent
_, span := tracer.Start(ctx, "remote-service.handle-request")
defer span.End()
}