-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.go
190 lines (160 loc) · 5.33 KB
/
options.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
package ocsql
import (
"go.opencensus.io/trace"
)
// TraceOption allows for managing ocsql configuration using functional options.
type TraceOption func(o *TraceOptions)
const defaultInstanceName = "default"
// TraceOptions holds configuration of our ocsql tracing middleware.
// By default all options are set to false intentionally when creating a wrapped
// driver and provide the most sensible default with both performance and
// security in mind.
type TraceOptions struct {
// AllowRoot, if set to true, will allow ocsql to create root spans in
// absence of existing spans or even context.
// Default is to not trace ocsql calls if no existing parent span is found
// in context or when using methods not taking context.
AllowRoot bool
// Ping, if set to true, will enable the creation of spans on Ping requests.
Ping bool
// RowsNext, if set to true, will enable the creation of spans on RowsNext
// calls. This can result in many spans.
RowsNext bool
// RowsClose, if set to true, will enable the creation of spans on RowsClose
// calls.
RowsClose bool
// RowsAffected, if set to true, will enable the creation of spans on
// RowsAffected calls.
RowsAffected bool
// LastInsertID, if set to true, will enable the creation of spans on
// LastInsertId calls.
LastInsertID bool
// Query, if set to true, will enable recording of sql queries in spans.
// Only allow this if it is safe to have queries recorded with respect to
// security.
Query bool
// QueryParams, if set to true, will enable recording of parameters used
// with parametrized queries. Only allow this if it is safe to have
// parameters recorded with respect to security.
// This setting is a noop if the Query option is set to false.
QueryParams bool
// DefaultAttributes will be set to each span as default.
DefaultAttributes []trace.Attribute
// InstanceName identifies database.
InstanceName string
// DisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
DisableErrSkip bool
// Sampler to use when creating spans.
Sampler trace.Sampler
}
// WithAllTraceOptions enables all available trace options.
func WithAllTraceOptions() TraceOption {
return func(o *TraceOptions) {
*o = AllTraceOptions
}
}
// AllTraceOptions has all tracing options enabled.
var AllTraceOptions = TraceOptions{
AllowRoot: true,
Ping: true,
RowsNext: true,
RowsClose: true,
RowsAffected: true,
LastInsertID: true,
Query: true,
QueryParams: true,
}
// WithOptions sets our ocsql tracing middleware options through a single
// TraceOptions object.
func WithOptions(options TraceOptions) TraceOption {
return func(o *TraceOptions) {
*o = options
o.DefaultAttributes = append(
[]trace.Attribute(nil), options.DefaultAttributes...,
)
}
}
// WithAllowRoot if set to true, will allow ocsql to create root spans in
// absence of exisiting spans or even context.
// Default is to not trace ocsql calls if no existing parent span is found
// in context or when using methods not taking context.
func WithAllowRoot(b bool) TraceOption {
return func(o *TraceOptions) {
o.AllowRoot = b
}
}
// WithPing if set to true, will enable the creation of spans on Ping requests.
func WithPing(b bool) TraceOption {
return func(o *TraceOptions) {
o.Ping = b
}
}
// WithRowsNext if set to true, will enable the creation of spans on RowsNext
// calls. This can result in many spans.
func WithRowsNext(b bool) TraceOption {
return func(o *TraceOptions) {
o.RowsNext = b
}
}
// WithRowsClose if set to true, will enable the creation of spans on RowsClose
// calls.
func WithRowsClose(b bool) TraceOption {
return func(o *TraceOptions) {
o.RowsClose = b
}
}
// WithRowsAffected if set to true, will enable the creation of spans on
// RowsAffected calls.
func WithRowsAffected(b bool) TraceOption {
return func(o *TraceOptions) {
o.RowsAffected = b
}
}
// WithLastInsertID if set to true, will enable the creation of spans on
// LastInsertId calls.
func WithLastInsertID(b bool) TraceOption {
return func(o *TraceOptions) {
o.LastInsertID = b
}
}
// WithQuery if set to true, will enable recording of sql queries in spans.
// Only allow this if it is safe to have queries recorded with respect to
// security.
func WithQuery(b bool) TraceOption {
return func(o *TraceOptions) {
o.Query = b
}
}
// WithQueryParams if set to true, will enable recording of parameters used
// with parametrized queries. Only allow this if it is safe to have
// parameters recorded with respect to security.
// This setting is a noop if the Query option is set to false.
func WithQueryParams(b bool) TraceOption {
return func(o *TraceOptions) {
o.QueryParams = b
}
}
// WithDefaultAttributes will be set to each span as default.
func WithDefaultAttributes(attrs ...trace.Attribute) TraceOption {
return func(o *TraceOptions) {
o.DefaultAttributes = attrs
}
}
// WithDisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
func WithDisableErrSkip(b bool) TraceOption {
return func(o *TraceOptions) {
o.DisableErrSkip = b
}
}
// WithSampler will be used on span creation.
func WithSampler(sampler trace.Sampler) TraceOption {
return func(o *TraceOptions) {
o.Sampler = sampler
}
}
// WithInstanceName sets database instance name.
func WithInstanceName(instanceName string) TraceOption {
return func(o *TraceOptions) {
o.InstanceName = instanceName
}
}