-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsystem_context.go
More file actions
57 lines (51 loc) · 1.38 KB
/
system_context.go
File metadata and controls
57 lines (51 loc) · 1.38 KB
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
package posthog
import (
"runtime"
"sync"
)
type osInfo struct {
Name string `json:"$os"`
Version string `json:"$os_version,omitempty"`
Distro string `json:"$os_distro,omitempty"`
}
type sysContext struct {
osInfo
GoVersion string `json:"$go_version"`
}
// cachedSystemContext holds the cached system context, computed once per process.
// OS info and Go version don't change during runtime, so there's no need to
// re-compute (and re-exec sw_vers/uname) on every event.
var (
cachedSystemContext sysContext
cachedSystemContextOnce sync.Once
// cachedSysContextProps holds the pre-built properties from the cached system context.
// This avoids creating a new Properties map on every ToProperties() call.
cachedSysContextProps Properties
cachedSysContextPropsOnce sync.Once
)
func getSystemContext() sysContext {
cachedSystemContextOnce.Do(func() {
cachedSystemContext = sysContext{
osInfo: getOSInfo(),
GoVersion: runtime.Version(),
}
})
return cachedSystemContext
}
func (ctx sysContext) ToProperties() Properties {
cachedSysContextPropsOnce.Do(func() {
sc := getSystemContext()
props := Properties{
"$os": sc.Name,
"$go_version": sc.GoVersion,
}
if sc.Version != "" {
props["$os_version"] = sc.Version
}
if sc.Distro != "" {
props["$os_distro"] = sc.Distro
}
cachedSysContextProps = props
})
return cachedSysContextProps
}