-
Notifications
You must be signed in to change notification settings - Fork 36
/
span.go
52 lines (42 loc) · 890 Bytes
/
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
package stackimpact
import (
"sync/atomic"
"time"
)
type Span struct {
agent *Agent
name string
timestamp time.Time
started bool
active bool
}
func newSpan(agent *Agent, name string) *Span {
s := &Span{
agent: agent,
name: name,
started: false,
active: false,
}
return s
}
func (s *Span) start() {
s.started = atomic.CompareAndSwapInt32(&s.agent.spanStarted, 0, 1)
if s.started {
s.active = s.agent.internalAgent.StartProfiling(s.name)
}
s.timestamp = time.Now()
}
// Stops profiling.
func (s *Span) Stop() {
duration := float64(time.Since(s.timestamp).Nanoseconds()) / 1e6
s.agent.internalAgent.RecordSpan(s.name, duration)
if s.started {
if s.active {
s.agent.internalAgent.StopProfiling()
}
if !s.agent.internalAgent.AutoProfiling {
s.agent.internalAgent.Report()
}
atomic.StoreInt32(&s.agent.spanStarted, 0)
}
}