forked from stackimpact/stackimpact-go
/
agent.go
108 lines (85 loc) · 2.29 KB
/
agent.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
package stackimpact
import (
"github.com/stackimpact/stackimpact-go/internal"
)
const ErrorGroupRecoveredPanics string = "Recovered panics"
const ErrorGroupUnrecoveredPanics string = "Unrecovered panics"
const ErrorGroupHandledExceptions string = "Handled exceptions"
type Options struct {
DashboardAddress string
AgentKey string
AppName string
AppVersion string
AppEnvironment string
HostName string
Debug bool
}
type Agent struct {
internalAgent *internal.Agent
// compatibility < 1.2.0
DashboardAddress string
AgentKey string
AppName string
HostName string
Debug bool
}
func NewAgent() *Agent {
a := &Agent{
internalAgent: internal.NewAgent(),
}
return a
}
func (a *Agent) Start(options Options) {
a.internalAgent.AgentKey = options.AgentKey
a.internalAgent.AppName = options.AppName
if options.AppVersion != "" {
a.internalAgent.AppVersion = options.AppVersion
}
if options.AppEnvironment != "" {
a.internalAgent.AppEnvironment = options.AppEnvironment
}
if options.HostName != "" {
a.internalAgent.HostName = options.HostName
}
if options.DashboardAddress != "" {
a.internalAgent.DashboardAddress = options.DashboardAddress
}
if options.Debug {
a.internalAgent.Debug = options.Debug
}
a.internalAgent.Start()
}
// compatibility < 1.2.0
func (a *Agent) Configure(agentKey string, appName string) {
a.Start(Options{
AgentKey: agentKey,
AppName: appName,
HostName: a.HostName,
DashboardAddress: a.DashboardAddress,
Debug: a.Debug,
})
}
func (a *Agent) MeasureSegment(segmentName string) *Segment {
s := newSegment(a, []string{segmentName})
s.start()
return s
}
func (a *Agent) MeasureSubsegment(segmentName string, subsegmentName string) *Segment {
s := newSegment(a, []string{segmentName, subsegmentName})
s.start()
return s
}
func (a *Agent) RecordError(err interface{}) {
a.internalAgent.RecordError(ErrorGroupHandledExceptions, err, 1)
}
func (a *Agent) RecordPanic() {
if err := recover(); err != nil {
a.internalAgent.RecordError(ErrorGroupUnrecoveredPanics, err, 1)
panic(err)
}
}
func (a *Agent) RecordAndRecoverPanic() {
if err := recover(); err != nil {
a.internalAgent.RecordError(ErrorGroupRecoveredPanics, err, 1)
}
}