forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventID.go
81 lines (67 loc) · 1.48 KB
/
eventID.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
package sentry
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
)
func init() {
AddDefaultOptionProvider(func() Option {
id, err := NewEventID()
if err != nil {
return nil
}
return EventID(id)
})
}
// NewEventID attempts to generate a new random UUIDv4 event
// ID which can be passed to the EventID() option.
func NewEventID() (string, error) {
id := make([]byte, 16)
_, err := io.ReadFull(rand.Reader, id)
if err != nil {
return "", err
}
id[6] &= 0x0F // clear version
id[6] |= 0x40 // set version to 4 (random uuid)
id[8] &= 0x3F // clear variant
id[8] |= 0x80 // set to IETF variant
return hex.EncodeToString(id), nil
}
// EventID is an option which controls the UUID used to represent
// an event. The ID should be exactly 32 hexadecimal characters long
// and include no dashes.
// If an invalid ID is passed to this option, it will return nil and
// be ignored by the packet builder.
func EventID(id string) Option {
if len(id) != 32 {
return nil
}
for _, r := range id {
if r <= 'f' && r >= 'a' {
continue
}
if r <= '9' && r >= '0' {
continue
}
return nil
}
return &eventIDOption{id}
}
func (p packet) getEventID() string {
if idOpt, ok := p["event_id"]; ok {
if id, ok := idOpt.(*eventIDOption); ok {
return id.ID
}
}
return ""
}
type eventIDOption struct {
ID string
}
func (o *eventIDOption) Class() string {
return "event_id"
}
func (o *eventIDOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.ID)
}