-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap.go
48 lines (39 loc) · 1.88 KB
/
wrap.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
package uctrace
import "context"
// Wrap0 takes a function that returns 0 values + potentially an error, and
// wraps its execution in a span. This is useful for capturing any returned
// errors and recording them in the span, since you'd otherwise need to find
// all the potential error return paths in your function and ensure you're
// recording those. (If you want to trace a function that doesn't return an
// error, just call tracer.Start() directly.)
func Wrap0(ctx context.Context, t Tracer, spanName string, requireParentSpan bool, f func(context.Context) error) error {
ctx, span := t.StartSpan(ctx, spanName, requireParentSpan)
defer span.End()
err := f(ctx)
span.RecordError(err)
return err // lint: ucerr-ignore
}
// Wrap1 wraps a function execution in a span. See comments above.
func Wrap1[T1 any](ctx context.Context, t Tracer, spanName string, requireParentSpan bool, f func(context.Context) (T1, error)) (T1, error) {
ctx, span := t.StartSpan(ctx, spanName, requireParentSpan)
defer span.End()
r1, err := f(ctx)
span.RecordError(err)
return r1, err // lint: ucerr-ignore
}
// Wrap2 wraps a function execution in a span. See comments above.
func Wrap2[T1 any, T2 any](ctx context.Context, t Tracer, spanName string, requireParentSpan bool, f func(context.Context) (T1, T2, error)) (T1, T2, error) {
ctx, span := t.StartSpan(ctx, spanName, requireParentSpan)
defer span.End()
r1, r2, err := f(ctx)
span.RecordError(err)
return r1, r2, err // lint: ucerr-ignore
}
// Wrap3 wraps a function execution in a span. See comments above.
func Wrap3[T1 any, T2 any, T3 any](ctx context.Context, t Tracer, spanName string, requireParentSpan bool, f func(context.Context) (T1, T2, T3, error)) (T1, T2, T3, error) {
ctx, span := t.StartSpan(ctx, spanName, requireParentSpan)
defer span.End()
r1, r2, r3, err := f(ctx)
span.RecordError(err)
return r1, r2, r3, err // lint: ucerr-ignore
}