-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.go
121 lines (101 loc) · 3.38 KB
/
scope.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
// Trace a code block as a scoped span.
// * Use instead of manual instrumentation: `tracer.Start()`/`span.End()`.
// * Must call `InitTracing()` first.
// * Automates start/end of span.
// * Tags file and line number where span started.
// * If function returned error:
// * Span is tagged as error.
// * Sets span attributes `otel.status_code` and `otel.status_description`
// with error details.
// * Logs error details to span.
import (
"context"
"runtime"
"strconv"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type ScopeAction func(ctx context.Context) error
// Start a scope with span named after fully qualified caller function.
func StartScope(ctx context.Context, opts ...trace.SpanStartOption) context.Context {
spanName, fileTag := getCallerSpanName(2)
return startSpan(ctx, spanName, fileTag, opts...)
}
// Start a scope with user-provided span name.
func StartNamedScope(ctx context.Context, spanName string, opts ...trace.SpanStartOption) context.Context {
fileTag := getFileTag(2)
return startSpan(ctx, spanName, fileTag, opts...)
}
// End scope created by `StartScope()`/`StartNamedScope()`.
// Logs error return value and ends span.
func EndScope(ctx context.Context, err error) {
span := trace.SpanFromContext(ctx)
// If scope returns an error, mark span with error.
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
}
// Scope calls action function within a tracing span named after the calling
// function.
// Equivalent to wrapping a code block with `StartScope()`/`EndScope()`.
// Must call `InitTracing()` first.
func Scope(ctx context.Context, action ScopeAction, opts ...trace.SpanStartOption) error {
spanName, fileTag := getCallerSpanName(2)
ctx = startSpan(ctx, spanName, fileTag, opts...)
err := action(ctx)
EndScope(ctx, err)
return err
}
// NamedScope calls action function within a tracing span.
// Equivalent to wrapping a code block with `StartNamedScope()`/`EndScope()`.
// Must call `InitTracing()` first.
func NamedScope(ctx context.Context, spanName string, action ScopeAction, opts ...trace.SpanStartOption) error {
fileTag := getFileTag(2)
ctx = startSpan(ctx, spanName, fileTag, opts...)
err := action(ctx)
EndScope(ctx, err)
return err
}
func startSpan(ctx context.Context, spanName, fileTag string, opts ...trace.SpanStartOption) context.Context {
// Initialize span.
tracer, ok := ctx.Value(tracerKey{}).(trace.Tracer)
if !ok {
// No tracer embedded. Fall back to default tracer.
tracer = GetDefaultTracer()
}
// Else, omit tracing.
if tracer == nil {
return ctx
}
opts = append(opts, trace.WithAttributes(
attribute.String("file", fileTag),
))
ctx, _ = tracer.Start(ctx, spanName, opts...)
return ctx
}
func getCallerSpanName(skip int) (string, string) {
pc, file, line, ok := runtime.Caller(skip)
// Determine source file and line number.
var fileTag, spanName string
if ok {
fileTag = file + ":" + strconv.Itoa(line)
spanName = runtime.FuncForPC(pc).Name()
} else {
// Rare condition. Probably a bug in caller.
fileTag = "unknown"
}
return spanName, fileTag
}
func getFileTag(skip int) string {
_, file, line, ok := runtime.Caller(1)
// Determine source file and line number.
if !ok {
// Rare condition. Probably a bug in caller.
return "unknown"
}
return file + ":" + strconv.Itoa(line)
}