This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.go
454 lines (406 loc) · 13.1 KB
/
highlight.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package highlight
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/hasura/go-graphql-client"
"github.com/pkg/errors"
)
var (
errorChan chan BackendErrorObjectInput
metricChan chan MetricInput
flushInterval time.Duration
client *graphql.Client
interruptChan chan bool
signalChan chan os.Signal
wg sync.WaitGroup
graphqlClientAddress string
)
// contextKey represents the keys that highlight may store in the users' context
// we append every contextKey with Highlight to avoid collisions
type contextKey string
const (
Highlight contextKey = "highlight"
RequestID = Highlight + "RequestID"
SessionSecureID = Highlight + "SessionSecureID"
)
var (
ContextKeys = struct {
RequestID contextKey
SessionSecureID contextKey
}{
RequestID: RequestID,
SessionSecureID: SessionSecureID,
}
)
// appState is used for keeping track of the current state of the app
// this can determine whether to accept new errors
type appState byte
const (
idle appState = iota
started
stopped
)
var (
state appState // 0 is idle, 1 is started, 2 is stopped
stateMutex sync.RWMutex
)
const backendSetupCooldown = 15
// message channels should be large to avoid blocking request processing
// in case of a surge of metrics or errors.
const messageBufferSize = 1 << 16
const metricCategory = "BACKEND"
var (
lastBackendSetupTimestamp time.Time
)
const (
consumeErrorSessionIDMissing = "context does not contain highlightSessionSecureID; context must have injected values from highlight.InterceptRequest"
consumeErrorRequestIDMissing = "context does not contain highlightRequestID; context must have injected values from highlight.InterceptRequest"
consumeErrorWorkerStopped = "highlight worker stopped"
)
// Logger is an interface that implements Log and Logf
type Logger interface {
Error(v ...interface{})
Errorf(format string, v ...interface{})
}
// log is this packages logger
var logger struct {
Logger
}
// noop default logger
type deadLog struct{}
func (d deadLog) Error(v ...interface{}) {}
func (d deadLog) Errorf(format string, v ...interface{}) {}
// Requester is used for making graphql requests
// in testing, a mock requester with an overwritten trigger function may be used
type Requester interface {
trigger([]*BackendErrorObjectInput, []*MetricInput) error
}
var (
requester Requester
)
type graphqlRequester struct{}
func (g graphqlRequester) trigger(errorsInput []*BackendErrorObjectInput, metricsInputs []*MetricInput) error {
if len(errorsInput) > 0 && len(metricsInputs) > 0 {
var mutation struct {
PushBackendPayload string `graphql:"pushBackendPayload(errors: $errors)"`
PushMetrics string `graphql:"pushMetrics(metrics: $metrics)"`
}
variables := map[string]interface{}{
"errors": errorsInput,
"metrics": metricsInputs,
}
err := client.Mutate(context.Background(), &mutation, variables)
if err != nil {
return err
}
} else if len(errorsInput) > 0 {
var mutation struct {
PushBackendPayload string `graphql:"pushBackendPayload(errors: $errors)"`
}
variables := map[string]interface{}{"errors": errorsInput}
err := client.Mutate(context.Background(), &mutation, variables)
if err != nil {
return err
}
} else if len(metricsInputs) > 0 {
var mutation struct {
PushMetrics string `graphql:"pushMetrics(metrics: $metrics)"`
}
variables := map[string]interface{}{"metrics": metricsInputs}
err := client.Mutate(context.Background(), &mutation, variables)
if err != nil {
return err
}
}
return nil
}
type mockRequester struct{}
func (m mockRequester) trigger(errorsInput []*BackendErrorObjectInput, metricsInput []*MetricInput) error {
// NOOP
_ = errorsInput
_ = metricsInput
return nil
}
type BackendErrorObjectInput struct {
SessionSecureID graphql.String `json:"session_secure_id"`
RequestID graphql.String `json:"request_id"`
Event graphql.String `json:"event"`
Type graphql.String `json:"type"`
URL graphql.String `json:"url"`
Source graphql.String `json:"source"`
StackTrace graphql.String `json:"stackTrace"`
Timestamp time.Time `json:"timestamp"`
Payload *graphql.String `json:"payload"`
}
type MetricInput struct {
SessionSecureID graphql.String `json:"session_secure_id"`
Group *graphql.String `json:"group"`
Name graphql.String `json:"name"`
Value graphql.Float `json:"value"`
Category *graphql.String `json:"category"`
Timestamp time.Time `json:"timestamp"`
}
// init gets called once when you import the package
func init() {
errorChan = make(chan BackendErrorObjectInput, messageBufferSize)
metricChan = make(chan MetricInput, messageBufferSize)
interruptChan = make(chan bool, 1)
signalChan = make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGABRT, syscall.SIGTERM, syscall.SIGINT)
SetGraphqlClientAddress("https://pub.highlight.run")
SetFlushInterval(2 * time.Second)
SetDebugMode(deadLog{})
requester = graphqlRequester{}
}
// Start is used to start the Highlight client's collection service.
func Start() {
StartWithContext(context.Background())
}
// StartWithContext is used to start the Highlight client's collection
// service, but allows the user to pass in their own context.Context.
// This allows the user kill the highlight worker by canceling their context.CancelFunc.
func StartWithContext(ctx context.Context) {
stateMutex.Lock()
defer stateMutex.Unlock()
if state == started {
return
}
var httpClient *http.Client = nil
if graphqlClientAddress == "https://localhost:8082/public" {
httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
client = graphql.NewClient(graphqlClientAddress, httpClient)
state = started
go func() {
for {
select {
case <-time.After(flushInterval):
wg.Add(1)
flushedErrors, flushedMetrics := flush()
wg.Done()
_ = requester.trigger(flushedErrors, flushedMetrics)
case <-interruptChan:
shutdown()
return
case <-signalChan:
shutdown()
return
case <-ctx.Done():
shutdown()
return
}
}
}()
}
// Stop sends an interrupt signal to the main process, closing the channels and returning the goroutines.
func Stop() {
stateMutex.RLock()
defer stateMutex.RUnlock()
if state == stopped || state == idle {
return
}
interruptChan <- true
}
// SetFlushInterval allows you to override the amount of time in which the
// Highlight client will collect errors before sending them to our backend.
// - newFlushInterval is an integer representing seconds
func SetFlushInterval(newFlushInterval time.Duration) {
flushInterval = newFlushInterval
}
// SetGraphqlClientAddress allows you to override the graphql client address,
// in case you are running Highlight on-prem, and need to point to your on-prem instance.
func SetGraphqlClientAddress(newGraphqlClientAddress string) {
graphqlClientAddress = newGraphqlClientAddress
}
func SetDebugMode(l Logger) {
logger.Logger = l
}
// InterceptRequest calls InterceptRequestWithContext using the request object's context
func InterceptRequest(r *http.Request) context.Context {
return InterceptRequestWithContext(r.Context(), r)
}
// InterceptRequestWithContext captures the highlight session and request ID
// for a particular request from the request headers, adding the values to the provided context.
func InterceptRequestWithContext(ctx context.Context, r *http.Request) context.Context {
highlightReqDetails := r.Header.Get("X-Highlight-Request")
ids := strings.Split(highlightReqDetails, "/")
if len(ids) < 2 {
return ctx
}
ctx = context.WithValue(ctx, ContextKeys.SessionSecureID, ids[0])
ctx = context.WithValue(ctx, ContextKeys.RequestID, ids[1])
return ctx
}
func MarkBackendSetup(ctx context.Context) {
if lastBackendSetupTimestamp.IsZero() {
currentTime := time.Now()
if currentTime.Sub(lastBackendSetupTimestamp).Minutes() > backendSetupCooldown {
lastBackendSetupTimestamp = currentTime
var mutation struct {
MarkBackendSetup string `graphql:"markBackendSetup(session_secure_id: $session_secure_id)"`
}
sessionSecureID := ctx.Value(ContextKeys.SessionSecureID)
variables := map[string]interface{}{
"session_secure_id": graphql.String(fmt.Sprintf("%v", sessionSecureID)),
}
err := client.Mutate(context.Background(), &mutation, variables)
if err != nil {
logger.Errorf("[highlight-go] %v", errors.Wrap(err, "error marking backend setup"))
return
}
}
}
}
// ConsumeError adds an error to the queue of errors to be sent to our backend.
// the provided context must have the injected highlight keys from InterceptRequestWithContext.
func ConsumeError(ctx context.Context, errorInput interface{}, tags ...string) {
sessionSecureID, requestID, err := validateRequest(ctx)
if err != nil {
logger.Errorf("[highlight-go] %v", err)
return
}
defer wg.Done()
wg.Add(1)
timestamp := time.Now().UTC()
tagsBytes, err := json.Marshal(tags)
if err != nil {
logger.Errorf("[highlight-go] %v", errors.Wrap(err, "error marshaling tags"))
return
}
tagsString := string(tagsBytes)
convertedError := BackendErrorObjectInput{
SessionSecureID: graphql.String(fmt.Sprintf("%v", sessionSecureID)),
RequestID: graphql.String(fmt.Sprintf("%v", requestID)),
Type: metricCategory,
Timestamp: timestamp,
Payload: (*graphql.String)(&tagsString),
}
switch e := errorInput.(type) {
case stackTracer:
stack := e.StackTrace()
if len(stack) < 1 {
err := errors.New("no stack frames in stack trace for stackTracer errors")
logger.Errorf("[highlight-go] %v", err)
}
var stackFrames []string
for _, frame := range stack {
frameBytes, err := frame.MarshalText()
if err != nil {
logger.Errorf("[highlight-go] %v", errors.Wrap(err, "error marshaling frame text"))
return
}
stackFrames = append(stackFrames, string(frameBytes))
}
convertedError.Event = graphql.String(fmt.Sprintf("%v", e.Error()))
stackFramesBytes, err := json.Marshal(stackFrames)
if err != nil {
logger.Errorf("[highlight-go] %v", errors.Wrap(err, "error marshaling stack frames"))
return
}
convertedError.StackTrace = graphql.String(stackFramesBytes)
case error:
convertedError.Event = graphql.String(e.Error())
convertedError.StackTrace = graphql.String(e.Error())
default:
convertedError.Event = graphql.String(fmt.Sprintf("%v", e))
convertedError.StackTrace = graphql.String(fmt.Sprintf("%v", e))
}
select {
case errorChan <- convertedError:
default:
logger.Errorf("[highlight-go] error channel full. discarding value for %s", sessionSecureID)
}
}
// RecordMetric is used to record arbitrary metrics in your golang backend.
// Highlight will process these metrics in the context of your session and expose them
// through dashboards. For example, you may want to record the latency of a DB query
// as a metric that you would like to graph and monitor. You'll be able to view the metric
// in the context of the session and network request and recorded it.
func RecordMetric(ctx context.Context, name string, value float64) {
sessionSecureID, requestID, err := validateRequest(ctx)
if err != nil {
logger.Errorf("[highlight-go] %v", err)
return
}
// track invocation of this function to ensure shutdown waits
defer wg.Done()
wg.Add(1)
req := graphql.String(requestID)
cat := graphql.String(metricCategory)
metric := MetricInput{
SessionSecureID: graphql.String(sessionSecureID),
Group: &req,
Name: graphql.String(name),
Value: graphql.Float(value),
Category: &cat,
Timestamp: time.Now().UTC(),
}
select {
case metricChan <- metric:
default:
logger.Errorf("[highlight-go] metric channel full. discarding value for %s", sessionSecureID)
}
}
func validateRequest(ctx context.Context) (sessionSecureID string, requestID string, err error) {
stateMutex.RLock()
defer stateMutex.RUnlock()
if state == stopped {
err = errors.New(consumeErrorWorkerStopped)
return
}
if v := ctx.Value(ContextKeys.SessionSecureID); v != nil {
sessionSecureID = v.(string)
} else {
err = errors.New(consumeErrorSessionIDMissing)
return
}
if v := ctx.Value(ContextKeys.RequestID); v != nil {
requestID = v.(string)
} else {
err = errors.New(consumeErrorRequestIDMissing)
return
}
return
}
// stackTracer implements the errors.StackTrace() interface function
type stackTracer interface {
StackTrace() errors.StackTrace
Error() string
}
func flush() ([]*BackendErrorObjectInput, []*MetricInput) {
tempChanSize := len(errorChan)
var flushedErrors []*BackendErrorObjectInput
for i := 0; i < tempChanSize; i++ {
e := <-errorChan
flushedErrors = append(flushedErrors, &e)
}
tempChanSize = len(metricChan)
var flushedMetrics []*MetricInput
for i := 0; i < tempChanSize; i++ {
e := <-metricChan
flushedMetrics = append(flushedMetrics, &e)
}
return flushedErrors, flushedMetrics
}
func shutdown() {
stateMutex.Lock()
defer stateMutex.Unlock()
if state == stopped || state == idle {
return
}
state = stopped
wg.Wait()
}