forked from instana/go-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
48 lines (38 loc) · 1.07 KB
/
snapshot.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package instana
import (
"runtime"
"sync"
"time"
"github.com/instana/go-sensor/acceptor"
)
// SnapshotCollector returns a snapshot of Go runtime
type SnapshotCollector struct {
ServiceName string
CollectionInterval time.Duration
mu sync.RWMutex
lastCollectionTime time.Time
}
// Collect returns a snaphot of current runtime state. Any call this
// method made before the next interval elapses will return nil
func (sc *SnapshotCollector) Collect() *acceptor.RuntimeInfo {
sc.mu.RLock()
lastSnapshotCollectionTime := sc.lastCollectionTime
sc.mu.RUnlock()
if time.Since(lastSnapshotCollectionTime) < sc.CollectionInterval {
return nil
}
sc.mu.Lock()
defer sc.mu.Unlock()
sc.lastCollectionTime = time.Now()
return &acceptor.RuntimeInfo{
Name: sc.ServiceName,
Version: runtime.Version(),
Root: runtime.GOROOT(),
MaxProcs: runtime.GOMAXPROCS(0),
Compiler: runtime.Compiler,
NumCPU: runtime.NumCPU(),
SensorVersion: Version,
}
}