-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtracing.go
72 lines (57 loc) · 1.37 KB
/
tracing.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
package metrics
import (
"context"
"fmt"
"github.com/newrelic/go-agent/v3/newrelic"
)
// TraceMethodCall traces a method call with a given struct/package and method names
func TraceMethodCall(ctx context.Context, structOrPackageName, methodName string) *MethodTracer {
txn := newrelic.FromContext(ctx)
if txn == nil {
return nil
}
seg := txn.StartSegment(fmt.Sprintf("%s %s", structOrPackageName, methodName))
return &MethodTracer{
txn: txn,
seg: seg,
}
}
// MethodTracer collects analytics for a given method call within an existing
// trace.
type MethodTracer struct {
txn *newrelic.Transaction
seg *newrelic.Segment
}
// AddAttribute adds a key-value pair metadata to the method trace
func (t *MethodTracer) AddAttribute(key string, value interface{}) {
if t == nil {
return
}
t.seg.AddAttribute(key, value)
}
// AddAttributes adds a set of key-value pair metadata to the method trace
func (t *MethodTracer) AddAttributes(attributes map[string]interface{}) {
if t == nil {
return
}
for key, value := range attributes {
t.seg.AddAttribute(key, value)
}
}
// OnError observes an error within a method trace
func (t *MethodTracer) OnError(err error) {
if t == nil {
return
}
if err == nil {
return
}
t.txn.NoticeError(err)
}
// End completes the trace for the method call.
func (t *MethodTracer) End() {
if t == nil {
return
}
t.seg.End()
}