forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadcrumbs.go
173 lines (142 loc) · 3.78 KB
/
breadcrumbs.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
package sentry
import (
"encoding/json"
"sync"
)
var globalBreadcrumbs = NewBreadcrumbsList(10)
func init() {
AddDefaultOptions(Breadcrumbs(DefaultBreadcrumbs()))
}
// DefaultBreadcrumbs are registered for inclusion in situations where
// you have not specified your own Breadcrumbs collection. You can use
// them to keep track of global events throughout your application.
func DefaultBreadcrumbs() BreadcrumbsList {
return globalBreadcrumbs
}
// Breadcrumbs can be included in your events to help track down
// the sequence of steps that resulted in a failure.
func Breadcrumbs(list BreadcrumbsList) Option {
if opt, ok := list.(Option); ok {
return opt
}
return nil
}
// A BreadcrumbsList is responsible for keeping track of all the
// breadcrumbs that make up a sequence. It will automatically remove
// old breadcrumbs as new ones are added and is both type-safe and
// O(1) execution time for inserts and removals.
type BreadcrumbsList interface {
// Adjusts the maximum number of breadcrumbs which will be maintained
// in this list.
WithSize(length int) BreadcrumbsList
// NewDefault creates a new breadcrumb using the `default` type.
// You can provide any data you wish to include in the breadcrumb,
// or nil if you do not wish to include any extra data.
NewDefault(data map[string]interface{}) Breadcrumb
// NewNavigation creates a new navigation breadcrumb which represents
// a transition from one page to another.
NewNavigation(from, to string) Breadcrumb
// NewHTTPRequest creates a new HTTP request breadcrumb which
// describes the results of an HTTP request.
NewHTTPRequest(method, url string, statusCode int, reason string) Breadcrumb
}
// NewBreadcrumbsList will create a new BreadcrumbsList which can be
// used to track breadcrumbs within a specific context.
func NewBreadcrumbsList(size int) BreadcrumbsList {
return &breadcrumbsList{
MaxLength: size,
Length: 0,
}
}
type breadcrumbsList struct {
MaxLength int
Head *breadcrumbListNode
Tail *breadcrumbListNode
Length int
mutex sync.Mutex
}
func (l *breadcrumbsList) Class() string {
return "breadcrumbs"
}
func (l *breadcrumbsList) WithSize(length int) BreadcrumbsList {
l.mutex.Lock()
defer l.mutex.Unlock()
l.MaxLength = length
for l.Length > l.MaxLength {
if l.Head == nil {
break
}
l.Head = l.Head.Next
l.Length--
}
return l
}
func (l *breadcrumbsList) NewDefault(data map[string]interface{}) Breadcrumb {
if data == nil {
data = map[string]interface{}{}
}
b := newBreadcrumb("default", data)
l.append(b)
return b
}
func (l *breadcrumbsList) NewNavigation(from, to string) Breadcrumb {
b := newBreadcrumb("navigation", map[string]interface{}{
"from": from,
"to": to,
})
l.append(b)
return b
}
func (l *breadcrumbsList) NewHTTPRequest(method, url string, statusCode int, reason string) Breadcrumb {
b := newBreadcrumb("http", map[string]interface{}{
"method": method,
"url": url,
"status_code": statusCode,
"reason": reason,
})
l.append(b)
return b
}
func (l *breadcrumbsList) MarshalJSON() ([]byte, error) {
return json.Marshal(l.list())
}
func (l *breadcrumbsList) append(b Breadcrumb) {
l.mutex.Lock()
defer l.mutex.Unlock()
// If we've disabled the breadcrumbs collector, skip
// any extra work.
if l.MaxLength == 0 {
return
}
n := &breadcrumbListNode{
Value: b,
Next: nil,
}
if l.Tail != nil {
l.Tail.Next = n
} else {
l.Head = n
}
l.Tail = n
l.Length++
for l.Length > l.MaxLength {
if l.Head == nil {
break
}
l.Head = l.Head.Next
l.Length--
}
}
func (l *breadcrumbsList) list() []Breadcrumb {
current := l.Head
out := []Breadcrumb{}
for current != nil {
out = append(out, current.Value)
current = current.Next
}
return out
}
type breadcrumbListNode struct {
Next *breadcrumbListNode
Value Breadcrumb
}