-
Notifications
You must be signed in to change notification settings - Fork 787
/
span.go
70 lines (63 loc) · 2.04 KB
/
span.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
package tracing
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
// StartSpan creates a span from a context with `operationName` name
func StartSpan(
ctx context.Context,
tracerName string,
operationName string,
opts ...trace.SpanStartOption,
) (context.Context, trace.Span) {
return otel.Tracer(tracerName).Start(ctx, operationName, opts...)
}
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
func Span(
ctx context.Context,
tracerName string,
operationName string,
doFn func(context.Context, trace.Span),
opts ...trace.SpanStartOption,
) {
ctx, span := StartSpan(ctx, tracerName, operationName, opts...)
defer span.End()
doFn(ctx, span)
}
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
func Span1[T1 any](
ctx context.Context,
tracerName string,
operationName string,
doFn func(context.Context, trace.Span) T1,
opts ...trace.SpanStartOption,
) T1 {
ctx, span := StartSpan(ctx, tracerName, operationName, opts...)
defer span.End()
return doFn(ctx, span)
}
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
func Span2[T1 any, T2 any](
ctx context.Context,
tracerName string,
operationName string,
doFn func(context.Context, trace.Span) (T1, T2),
opts ...trace.SpanStartOption,
) (T1, T2) {
ctx, span := StartSpan(ctx, tracerName, operationName, opts...)
defer span.End()
return doFn(ctx, span)
}
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
func Span3[T1 any, T2 any, T3 any](
ctx context.Context,
tracerName string,
operationName string,
doFn func(context.Context, trace.Span) (T1, T2, T3),
opts ...trace.SpanStartOption,
) (T1, T2, T3) {
ctx, span := StartSpan(ctx, tracerName, operationName, opts...)
defer span.End()
return doFn(ctx, span)
}