forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_boundary.go
178 lines (157 loc) · 4.84 KB
/
error_boundary.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package statsig
import (
"bytes"
"encoding/json"
"net/http"
"runtime"
"strconv"
"sync"
"time"
)
type errorBoundary struct {
api string
endpoint string
sdkKey string
client *http.Client
seen map[string]bool
seenLock sync.RWMutex
diagnostics *diagnostics
options *Options
}
type logExceptionRequestBody struct {
Exception string `json:"exception"`
Info string `json:"info"`
StatsigMetadata statsigMetadata `json:"statsigMetadata"`
Extra map[string]interface{} `json:"extra"`
Tag string `json:"tag"`
}
type logExceptionResponse struct {
Success bool
}
var ErrorBoundaryAPI = "https://statsigapi.net/v1"
var ErrorBoundaryEndpoint = "/sdk_exception"
const (
InvalidSDKKeyError string = "Must provide a valid SDK key."
EmptyUserError string = "A non-empty StatsigUser.UserID or StatsigUser.CustomIDs is required. See https://docs.statsig.com/messages/serverRequiredUserID"
EventBatchSizeError string = "The max number of events supported in one batch is 500. Please reduce the slice size and try again."
)
func newErrorBoundary(sdkKey string, options *Options, diagnostics *diagnostics) *errorBoundary {
errorBoundary := &errorBoundary{
api: ErrorBoundaryAPI,
endpoint: ErrorBoundaryEndpoint,
sdkKey: sdkKey,
client: &http.Client{Timeout: time.Second * 3},
seen: make(map[string]bool),
diagnostics: diagnostics,
options: options,
}
if options.API != "" {
errorBoundary.api = options.API
}
return errorBoundary
}
func (e *errorBoundary) checkSeen(exceptionString string) bool {
e.seenLock.Lock()
defer e.seenLock.Unlock()
if e.seen[exceptionString] {
return true
}
e.seen[exceptionString] = true
return false
}
func (e *errorBoundary) captureCheckGate(task func() FeatureGate) FeatureGate {
defer e.ebRecover(func() {
e.diagnostics.api().checkGate().end().success(false).mark()
})
e.diagnostics.api().checkGate().start().mark()
res := task()
e.diagnostics.api().checkGate().end().success(true).mark()
return res
}
func (e *errorBoundary) captureGetConfig(task func() DynamicConfig) DynamicConfig {
defer e.ebRecover(func() {
e.diagnostics.api().getConfig().end().success(false).mark()
})
e.diagnostics.api().getConfig().start().mark()
res := task()
e.diagnostics.api().getConfig().end().success(true).mark()
return res
}
func (e *errorBoundary) captureGetLayer(task func() Layer) Layer {
defer e.ebRecover(func() {
e.diagnostics.api().getLayer().end().success(false).mark()
})
e.diagnostics.api().getLayer().start().mark()
res := task()
e.diagnostics.api().getLayer().end().success(true).mark()
return res
}
func (e *errorBoundary) captureGetClientInitializeResponse(task func() ClientInitializeResponse) ClientInitializeResponse {
defer e.ebRecover(func() {})
return task()
}
func (e *errorBoundary) captureGetUserPersistedValues(task func() UserPersistedValues) UserPersistedValues {
defer e.ebRecover(func() {})
return task()
}
func (e *errorBoundary) captureVoid(task func()) {
defer e.ebRecover(func() {})
task()
}
func (e *errorBoundary) captureGetExperimentLayer(task func() (string, bool)) (string, bool) {
defer e.ebRecover(func() {})
val, ok := task()
return val, ok
}
func (e *errorBoundary) ebRecover(recoverCallback func()) {
if err := recover(); err != nil {
e.logException(toError(err))
Logger().LogError(err)
recoverCallback()
}
}
func (e *errorBoundary) logExceptionWithContext(exception error, context StatsigContext) {
if e.options.StatsigLoggerOptions.DisableAllLogging || e.options.LocalMode {
return
}
var exceptionString string
if exception == nil {
exceptionString = "Unknown"
} else {
exceptionString = exception.Error()
}
if context.LogToOutput {
Logger().LogError(exception)
}
if !context.BypassDedupe && e.checkSeen(exceptionString) {
return
}
stack := make([]byte, 1024)
runtime.Stack(stack, false)
metadata := getStatsigMetadata()
body := &logExceptionRequestBody{
Exception: exceptionString,
Info: string(stack),
StatsigMetadata: metadata,
Extra: context.getContextForLogging(),
Tag: context.Caller,
}
bodyString, err := json.Marshal(body)
if err != nil {
return
}
req, err := http.NewRequest("POST", e.api+e.endpoint, bytes.NewBuffer(bodyString))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("STATSIG-API-KEY", e.sdkKey)
req.Header.Add("STATSIG-CLIENT-TIME", strconv.FormatInt(getUnixMilli(), 10))
req.Header.Add("STATSIG-SDK-TYPE", metadata.SDKType)
req.Header.Add("STATSIG-SDK-VERSION", metadata.SDKVersion)
req.Header.Add("STATSIG-SERVER-SESSION-ID", metadata.SessionID)
_, _ = e.client.Do(req)
}
func (e *errorBoundary) logException(exception error) {
e.logExceptionWithContext(exception, StatsigContext{})
}