-
Notifications
You must be signed in to change notification settings - Fork 270
/
config.go
301 lines (248 loc) · 7.82 KB
/
config.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2022 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package motrace
import (
"encoding/binary"
"sync"
"time"
"github.com/matrixorigin/matrixone/pkg/config"
"github.com/matrixorigin/matrixone/pkg/util"
"github.com/matrixorigin/matrixone/pkg/util/export/table"
ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor"
"github.com/matrixorigin/matrixone/pkg/util/trace"
)
const (
MOStatementType = "statement"
MOSpanType = "span"
MOLogType = "log"
MOErrorType = "error"
MORawLogType = "rawlog"
)
// tracerProviderConfig.
type tracerProviderConfig struct {
// spanProcessors contains collection of SpanProcessors that are processing pipeline
// for spans in the trace signal.
// SpanProcessors registered with a TracerProvider and are called at the start
// and end of a Span's lifecycle, and are called in the order they are
// registered.
spanProcessors []trace.SpanProcessor
enable bool // SetEnable
// idGenerator is used to generate all Span and Trace IDs when needed.
idGenerator trace.IDGenerator
// resource contains attributes representing an entity that produces telemetry.
resource *trace.Resource // withMOVersion, WithNode,
// disableSpan
disableSpan bool
// disableError
disableError bool
// debugMode used in Tracer.Debug
debugMode bool // DebugMode
batchProcessor BatchProcessor // WithBatchProcessor
// writerFactory gen writer for CSV output
writerFactory table.WriterFactory // WithFSWriterFactory, default from export.GetFSWriterFactory4Trace
// disableSqlWriter
disableSqlWriter bool // set by WithSQLWriterDisable
// stmt aggregation
disableStmtAggregation bool // set by WithStmtAggregationDisable
enableStmtMerge bool // set by WithStmtMergeDisable
aggregationWindow time.Duration // WithAggregationWindow
selectAggrThreshold time.Duration // WithSelectThreshold
sqlExecutor func() ie.InternalExecutor // WithSQLExecutor
// needInit control table schema create
needInit bool // WithInitAction
exportInterval time.Duration // WithExportInterval
// longQueryTime unit ns
longQueryTime int64 // WithLongQueryTime
// longSpanTime
longSpanTime time.Duration
// skipRunningStmt
skipRunningStmt bool // set by WithSkipRunningStmt
bufferSizeThreshold int64 // WithBufferSizeThreshold
cuConfig config.OBCUConfig // WithCUConfig
cuConfigV1 config.OBCUConfig // WithCUConfig
tcpPacket bool // WithTCPPacket
labels map[string]string
mux sync.RWMutex
}
func (cfg *tracerProviderConfig) getNodeResource() *trace.MONodeResource {
cfg.mux.RLock()
defer cfg.mux.RUnlock()
if val, has := cfg.resource.Get("Node"); !has {
return &trace.MONodeResource{}
} else {
return val.(*trace.MONodeResource)
}
}
func (cfg *tracerProviderConfig) IsEnable() bool {
cfg.mux.RLock()
defer cfg.mux.RUnlock()
return cfg.enable
}
func (cfg *tracerProviderConfig) SetEnable(enable bool) {
cfg.mux.Lock()
defer cfg.mux.Unlock()
cfg.enable = enable
}
func (cfg *tracerProviderConfig) GetSqlExecutor() func() ie.InternalExecutor {
cfg.mux.RLock()
defer cfg.mux.RUnlock()
return cfg.sqlExecutor
}
// TracerProviderOption configures a TracerProvider.
type TracerProviderOption interface {
apply(*tracerProviderConfig)
}
type tracerProviderOption func(config *tracerProviderConfig)
func (f tracerProviderOption) apply(config *tracerProviderConfig) {
f(config)
}
func withMOVersion(v string) tracerProviderOption {
return func(config *tracerProviderConfig) {
config.resource.Put("version", v)
}
}
// WithNode give id as NodeId, t as NodeType
func WithNode(uuid string, t string) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.resource.Put("Node", &trace.MONodeResource{
NodeUuid: uuid,
NodeType: t,
})
}
}
func EnableTracer(enable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.SetEnable(enable)
}
}
func WithFSWriterFactory(f table.WriterFactory) tracerProviderOption {
return tracerProviderOption(func(cfg *tracerProviderConfig) {
cfg.writerFactory = f
})
}
func WithExportInterval(secs int) tracerProviderOption {
return tracerProviderOption(func(cfg *tracerProviderConfig) {
cfg.exportInterval = time.Second * time.Duration(secs)
})
}
func WithLongQueryTime(secs float64) tracerProviderOption {
return tracerProviderOption(func(cfg *tracerProviderConfig) {
cfg.longQueryTime = int64(float64(time.Second) * secs)
})
}
func WithLongSpanTime(d time.Duration) tracerProviderOption {
return tracerProviderOption(func(cfg *tracerProviderConfig) {
cfg.longSpanTime = d
})
}
func WithSpanDisable(disable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.disableSpan = disable
}
}
func WithErrorDisable(disable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.disableError = disable
}
}
func WithSkipRunningStmt(skip bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.skipRunningStmt = skip
}
}
func WithSQLWriterDisable(disable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.disableSqlWriter = disable
}
}
func WithAggregatorDisable(disable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.disableStmtAggregation = disable
}
}
func WithStmtMergeEnable(enable bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.enableStmtMerge = enable
}
}
func WithCUConfig(cu config.OBCUConfig, cuv1 config.OBCUConfig) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.cuConfig = cu
cfg.cuConfigV1 = cuv1
}
}
func WithTCPPacket(count bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.tcpPacket = count
}
}
func WithLabels(l map[string]string) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.labels = l
}
}
func WithAggregatorWindow(window time.Duration) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.aggregationWindow = window
}
}
func WithSelectThreshold(window time.Duration) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.selectAggrThreshold = window
}
}
func WithBufferSizeThreshold(size int64) tracerProviderOption {
return tracerProviderOption(func(cfg *tracerProviderConfig) {
cfg.bufferSizeThreshold = size
})
}
func DebugMode(debug bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.debugMode = debug
}
}
func WithBatchProcessor(p BatchProcessor) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.batchProcessor = p
}
}
func WithSQLExecutor(f func() ie.InternalExecutor) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.mux.Lock()
defer cfg.mux.Unlock()
cfg.sqlExecutor = f
}
}
func WithInitAction(init bool) tracerProviderOption {
return func(cfg *tracerProviderConfig) {
cfg.mux.Lock()
defer cfg.mux.Unlock()
cfg.needInit = init
}
}
var _ trace.IDGenerator = &moIDGenerator{}
type moIDGenerator struct{}
func (M moIDGenerator) NewIDs() (trace.TraceID, trace.SpanID) {
tid := trace.TraceID{}
binary.BigEndian.PutUint64(tid[:], util.Fastrand64())
binary.BigEndian.PutUint64(tid[8:], util.Fastrand64())
sid := trace.SpanID{}
binary.BigEndian.PutUint64(sid[:], util.Fastrand64())
return tid, sid
}
func (M moIDGenerator) NewSpanID() trace.SpanID {
sid := trace.SpanID{}
binary.BigEndian.PutUint64(sid[:], util.Fastrand64())
return sid
}