-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrace.go
More file actions
81 lines (74 loc) · 1.79 KB
/
Copy pathtrace.go
File metadata and controls
81 lines (74 loc) · 1.79 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
81
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/apstndb/spannerotel/tracing"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
const traceServiceName = "execspansql"
func tracingEnabled(o opts) bool {
return o.TraceStdout || o.TraceProject != "" || o.TraceOTLP
}
func traceConfig(o opts) (tracing.Config, error) {
n := 0
if o.TraceOTLP {
n++
}
if o.TraceStdout {
n++
}
if o.TraceProject != "" {
n++
}
if n != 1 {
return tracing.Config{}, fmt.Errorf("exactly one of --experimental-trace-otlp, --experimental-trace-stdout, or --experimental-trace-project must be set")
}
switch {
case o.TraceOTLP:
return tracing.Config{
Exporter: tracing.ExporterOTLP,
ServiceName: traceServiceName,
OTLPEndpoint: o.TraceOTLPEndpoint,
OTLPInsecure: true,
}, nil
case o.TraceStdout:
return tracing.Config{
Exporter: tracing.ExporterStdout,
ServiceName: traceServiceName,
StdoutWriter: os.Stderr,
PrettyStdout: true,
}, nil
case o.TraceProject != "":
return tracing.Config{
Exporter: tracing.ExporterCloudTrace,
ServiceName: traceServiceName,
CloudTraceProject: o.TraceProject,
}, nil
default:
return tracing.Config{}, fmt.Errorf("tracing is not configured")
}
}
func enableTracing(ctx context.Context, o opts) (context.Context, *sdktrace.TracerProvider, error) {
if !tracingEnabled(o) {
return ctx, nil, nil
}
cfg, err := traceConfig(o)
if err != nil {
return ctx, nil, err
}
tp, err := tracing.NewTracerProvider(cfg)
if err != nil {
return ctx, nil, err
}
return ctx, tp, nil
}
func shutdownTracing(ctx context.Context, tp *sdktrace.TracerProvider) error {
if tp == nil {
return nil
}
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return tracing.Shutdown(shutdownCtx, tp)
}