forked from TheZeroSlave/zapsentry
/
scope.go
74 lines (59 loc) · 1.5 KB
/
scope.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
package zapsentry
import (
"net/http"
"github.com/getsentry/sentry-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
zapSentryScopeKey = "_zapsentry_scope_"
)
// ScopeFunc is a function that can be used to apply changes to the underlying scope
type ScopeFunc func(scope *sentry.Scope)
// Scope is abstraction over sentry.Scope
type Scope struct {
scope *sentry.Scope
}
// NewScope creates a new Scope object
func NewScope() *Scope {
return &Scope{sentry.NewScope()}
}
// FromScope adds scope provided to field
func FromScope(scope *sentry.Scope) zapcore.Field {
f := zap.Skip()
f.Interface = scope
f.Key = zapSentryScopeKey
return f
}
// SetRequest sets the request on the underlying scope
func (s *Scope) SetRequest(r *http.Request) *Scope {
s.scope.SetRequest(r)
return s
}
// SetUser sets the user on the underlying scope
func (s *Scope) SetUser(user sentry.User) *Scope {
s.scope.SetUser(user)
return s
}
// SetTag sets a tag on the underlying scope
func (s *Scope) SetTag(key, value string) *Scope {
s.scope.SetTag(key, value)
return s
}
// SetTags sets the tags on the underlying scope
func (s *Scope) SetTags(tags map[string]string) *Scope {
s.scope.SetTags(tags)
return s
}
// Apply applies direct changes to the underlying scope
func (s *Scope) Apply(sf ScopeFunc) *Scope {
sf(s.scope)
return s
}
// Build constructs a zapcore.Field object from the current scope
func (s *Scope) Build() zapcore.Field {
f := zap.Skip()
f.Interface = s.scope
f.Key = zapSentryScopeKey
return f
}