-
Notifications
You must be signed in to change notification settings - Fork 6
/
trace_exporter.go
62 lines (54 loc) · 1.92 KB
/
trace_exporter.go
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
package main
import (
"context"
"log"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
octrace "go.opencensus.io/trace"
"go.opentelemetry.io/contrib/detectors/gcp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/bridge/opencensus"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)
// enableTraceExport turns on Open Telemetry tracing with export to Cloud Trace.
func enableTraceExport(ctx context.Context, sampleRate float64) func() {
exporter, err := texporter.New(texporter.WithProjectID(*ProjectName))
if err != nil {
log.Fatalf("texporter.New: %v", err)
}
// Identify your application using resource detection
res, err := resource.New(ctx,
// Use the GCP resource detector to detect information about the GCP platform
resource.WithDetectors(gcp.NewDetector()),
// Keep the default detectors
resource.WithTelemetrySDK(),
// Add your own custom attributes to identify your application
resource.WithAttributes(
semconv.ServiceName(tracerName),
attribute.KeyValue{Key: "transport", Value: attribute.StringValue(*clientProtocol)},
),
)
if err != nil {
log.Fatalf("resource.New: %v", err)
}
// Create trace provider with the exporter.
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(res),
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(sampleRate)),
)
otel.SetTracerProvider(tp)
// Use opencensus bridge to pick up OC traces from the storage library.
// TODO: remove this when migration to OpenTelemetry is complete.
tracer := otel.GetTracerProvider().Tracer(tracerName)
octrace.DefaultTracer = opencensus.NewTracer(tracer)
log.Printf("Cloud trace export enabled")
return func() {
tp.ForceFlush(ctx)
if err := tp.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
}
}