-
Notifications
You must be signed in to change notification settings - Fork 9
/
collect.go
57 lines (43 loc) · 1.35 KB
/
collect.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tracing
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type Collected struct {
name string
attributes []attribute.KeyValue
}
func Name(name string) Collected {
return Collected{name: name}
}
func (c Collected) Attribute(kv ...attribute.KeyValue) Collected {
return Collected{name: c.name, attributes: append(c.attributes, kv...)}
}
func Collect0(ctx context.Context, tracer trace.Tracer, name Collected, callback func(context.Context) error) error {
if tracer == nil {
return callback(ctx)
}
ctx, span := tracer.Start(ctx, name.name, trace.WithAttributes(name.attributes...))
defer span.End(trace.WithStackTrace(true))
err := callback(ctx)
if err != nil {
span.RecordError(err)
}
return err
}
func Collect1[T any](ctx context.Context, tracer trace.Tracer, name Collected, callback func(context.Context) (T, error)) (T, error) {
if tracer == nil {
return callback(ctx)
}
ctx, span := tracer.Start(ctx, name.name, trace.WithAttributes(name.attributes...))
defer span.End(trace.WithStackTrace(true))
value, err := callback(ctx)
if err != nil {
span.RecordError(err)
}
return value, err
}