-
Notifications
You must be signed in to change notification settings - Fork 2
/
contexts.go
115 lines (98 loc) · 2.87 KB
/
contexts.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
109
110
111
112
113
114
115
package sentry
import (
"encoding/json"
"runtime"
"strings"
"github.com/matishsiao/goInfo"
)
func init() {
gi := goInfo.GetInfo()
AddDefaultOptions(
RuntimeContext("go", strings.TrimPrefix(runtime.Version(), "go")),
OSContext(&OSContextInfo{
Name: gi.GoOS,
Version: gi.OS,
KernelVersion: gi.Core,
}),
DeviceContext(&DeviceContextInfo{
Architecture: gi.Platform,
Family: gi.Kernel,
Model: "Unknown",
}),
)
}
// OSContextInfo describes the operating system that your application
// is running on.
type OSContextInfo struct {
Type string `json:"type,omitempty"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
Build string `json:"build,omitempty"`
KernelVersion string `json:"kernel_version,omitempty"`
Rooted bool `json:"rooted,omitempty"`
}
// OSContext allows you to set the context describing
// the operating system that your application is running on.
func OSContext(info *OSContextInfo) Option {
return Context("os", info)
}
// DeviceContextInfo describes the device that your application
// is running on.
type DeviceContextInfo struct {
Type string `json:"type,omitempty"`
Name string `json:"name"`
Family string `json:"family,omitempty"`
Model string `json:"model,omitempty"`
ModelID string `json:"model_id,omitempty"`
Architecture string `json:"arch,omitempty"`
BatteryLevel int `json:"battery_level,omitempty"`
Orientation string `json:"orientation,omitempty"`
}
// DeviceContext allows you to set the context describing the
// device that your application is being executed on.
func DeviceContext(info *DeviceContextInfo) Option {
return Context("device", info)
}
// RuntimeContext allows you to set the information
// pertaining to the runtime that your program is
// executing on.
func RuntimeContext(name, version string) Option {
return Context("runtime", map[string]string{
"name": name,
"version": version,
})
}
// Context allows you to manually set a context entry
// by providing its key and the data to accompany it.
// This is a low-level method and you should be familiar
// with the correct usage of contexts before using it.
// https://docs.sentry.io/clientdev/interfaces/contexts/
func Context(key string, data interface{}) Option {
return &contextOption{
contexts: map[string]interface{}{
key: data,
},
}
}
type contextOption struct {
contexts map[string]interface{}
}
func (o *contextOption) Class() string {
return "contexts"
}
func (o *contextOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.contexts)
}
func (o *contextOption) Merge(old Option) Option {
if old, ok := old.(*contextOption); ok {
ctx := map[string]interface{}{}
for k, v := range old.contexts {
ctx[k] = v
}
for k, v := range o.contexts {
ctx[k] = v
}
return &contextOption{ctx}
}
return o
}