-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument.go
57 lines (45 loc) · 1.27 KB
/
instrument.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
package go_otel_auto_instrument
import (
"context"
"os"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
func init() {
serviceName := os.Getenv("OTEL_SERVICE_NAME")
err := setupTracerProvider(serviceName)
if err != nil {
panic(err)
}
}
func SetContext(ctx context.Context) {
setContext(ctx)
}
func StartSpan(name string) (context.Context, context.Context, trace.Span) {
ctx := getContext()
parentCtx := getParentContext()
return startSpan(ctx, parentCtx, name)
}
func StartSpanCtx(ctx context.Context, name string) (context.Context, context.Context, trace.Span) {
parentCtx := getParentContext()
return startSpan(ctx, parentCtx, name)
}
func startSpan(ctx context.Context, _ context.Context, name string) (context.Context, context.Context, trace.Span) {
t := otel.Tracer(name)
gCtx := getContext()
ctxSpan := trace.SpanFromContext(ctx)
gCtxSpan := trace.SpanFromContext(gCtx)
if ctxSpan != nil && ctxSpan.SpanContext().IsValid() {
newCtx, span := t.Start(ctx, name)
setContext(newCtx)
return newCtx, ctx, span
}
if gCtxSpan != nil && gCtxSpan.SpanContext().IsValid() {
newCtx, span := t.Start(gCtx, name)
setContext(newCtx)
return newCtx, gCtx, span
}
newCtx, span := t.Start(ctx, name)
setContext(newCtx)
return newCtx, ctx, span
}