forked from j2gg0s/otsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
184 lines (154 loc) · 5.23 KB
/
options.go
File metadata and controls
184 lines (154 loc) · 5.23 KB
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
package otsql
import (
"context"
"go.opentelemetry.io/otel/label"
)
// TraceOption allows for managing otsql configuration using functional options.
type TraceOption func(*TraceOptions)
// TraceOptions holds configuration of our otsql 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 otsql to create root spans in
// absence of existing spans or even context.
// Default is to not trace otsql 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
// 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
// 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
// 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
// SpanNameFormatter will be called to produce span's name.
// Default use method as span name
SpanNameFormatter func(ctx context.Context, method string, query string) string
// DefaultLabels will be set to each span as default.
DefaultLabels []label.KeyValue
// InstanceName identifies database.
InstanceName string
}
func newTraceOptions(options ...TraceOption) TraceOptions {
o := TraceOptions{}
for _, option := range options {
option(&o)
}
if o.InstanceName == "" {
o.InstanceName = "default"
} else {
o.DefaultLabels = append(o.DefaultLabels, label.String(sqlInstance, o.InstanceName))
}
if o.SpanNameFormatter == nil {
o.SpanNameFormatter = func(_ context.Context, method string, _ string) string { return method }
}
if o.QueryParams && !o.Query {
o.QueryParams = false
}
return o
}
// WithOptions sets our otsql tracing middleware options through a single
// TraceOptions object.
func WithOptions(options TraceOptions) TraceOption {
return func(o *TraceOptions) {
*o = options
o.DefaultLabels = append(
[]label.KeyValue(nil), options.DefaultLabels...,
)
}
}
// WithAllowRoot if set to true, will allow otsql to create root spans in
// absence of exisiting spans or even context.
// Default is to not trace otsql 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
}
}
// WithDefaultLabels will be set to each span as default.
func WithDefaultLabels(attrs ...label.KeyValue) TraceOption {
return func(o *TraceOptions) {
o.DefaultLabels = attrs
}
}
// WithInstanceName sets database instance name.
func WithInstanceName(instanceName string) TraceOption {
return func(o *TraceOptions) {
o.InstanceName = instanceName
}
}
// WithSpanNameFormatter sets name for each span.
func WithSpanNameFormatter(formatter func(context.Context, string, string) string) TraceOption {
return func(o *TraceOptions) {
o.SpanNameFormatter = formatter
}
}