github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/impl/motrace/config.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package motrace 16 17 import ( 18 "encoding/binary" 19 "sync" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/util" 23 "github.com/matrixorigin/matrixone/pkg/util/export/table" 24 ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor" 25 "github.com/matrixorigin/matrixone/pkg/util/trace" 26 ) 27 28 const ( 29 InternalExecutor = "InternalExecutor" 30 FileService = "FileService" 31 ) 32 33 const ( 34 MOStatementType = "statement" 35 MOSpanType = "span" 36 MOLogType = "log" 37 MOErrorType = "error" 38 MORawLogType = "rawlog" 39 ) 40 41 // tracerProviderConfig. 42 type tracerProviderConfig struct { 43 // spanProcessors contains collection of SpanProcessors that are processing pipeline 44 // for spans in the trace signal. 45 // SpanProcessors registered with a TracerProvider and are called at the start 46 // and end of a Span's lifecycle, and are called in the order they are 47 // registered. 48 spanProcessors []trace.SpanProcessor 49 50 enable bool // SetEnable 51 52 // idGenerator is used to generate all Span and Trace IDs when needed. 53 idGenerator trace.IDGenerator 54 55 // resource contains attributes representing an entity that produces telemetry. 56 resource *trace.Resource // withMOVersion, WithNode, 57 58 // debugMode used in Tracer.Debug 59 debugMode bool // DebugMode 60 61 batchProcessMode string // WithBatchProcessMode 62 batchProcessor BatchProcessor // WithBatchProcessor 63 64 // writerFactory gen writer for CSV output 65 writerFactory table.WriterFactory // WithFSWriterFactory, default from export.GetFSWriterFactory4Trace 66 67 sqlExecutor func() ie.InternalExecutor // WithSQLExecutor 68 // needInit control table schema create 69 needInit bool // WithInitAction 70 71 exportInterval time.Duration // WithExportInterval 72 // longQueryTime unit ns 73 longQueryTime int64 // WithLongQueryTime 74 75 mux sync.RWMutex 76 } 77 78 func (cfg *tracerProviderConfig) getNodeResource() *trace.MONodeResource { 79 cfg.mux.RLock() 80 defer cfg.mux.RUnlock() 81 if val, has := cfg.resource.Get("Node"); !has { 82 return &trace.MONodeResource{} 83 } else { 84 return val.(*trace.MONodeResource) 85 } 86 } 87 88 func (cfg *tracerProviderConfig) IsEnable() bool { 89 cfg.mux.RLock() 90 defer cfg.mux.RUnlock() 91 return cfg.enable 92 } 93 94 func (cfg *tracerProviderConfig) SetEnable(enable bool) { 95 cfg.mux.Lock() 96 defer cfg.mux.Unlock() 97 cfg.enable = enable 98 } 99 100 func (cfg *tracerProviderConfig) GetSqlExecutor() func() ie.InternalExecutor { 101 cfg.mux.RLock() 102 defer cfg.mux.RUnlock() 103 return cfg.sqlExecutor 104 } 105 106 // TracerProviderOption configures a TracerProvider. 107 type TracerProviderOption interface { 108 apply(*tracerProviderConfig) 109 } 110 111 type tracerProviderOption func(config *tracerProviderConfig) 112 113 func (f tracerProviderOption) apply(config *tracerProviderConfig) { 114 f(config) 115 } 116 117 func withMOVersion(v string) tracerProviderOption { 118 return func(config *tracerProviderConfig) { 119 config.resource.Put("version", v) 120 } 121 } 122 123 // WithNode give id as NodeId, t as NodeType 124 func WithNode(uuid string, t string) tracerProviderOption { 125 return func(cfg *tracerProviderConfig) { 126 cfg.resource.Put("Node", &trace.MONodeResource{ 127 NodeUuid: uuid, 128 NodeType: t, 129 }) 130 } 131 } 132 133 func EnableTracer(enable bool) tracerProviderOption { 134 return func(cfg *tracerProviderConfig) { 135 cfg.SetEnable(enable) 136 } 137 } 138 139 func WithFSWriterFactory(f table.WriterFactory) tracerProviderOption { 140 return tracerProviderOption(func(cfg *tracerProviderConfig) { 141 cfg.writerFactory = f 142 }) 143 } 144 145 func WithExportInterval(secs int) tracerProviderOption { 146 return tracerProviderOption(func(cfg *tracerProviderConfig) { 147 cfg.exportInterval = time.Second * time.Duration(secs) 148 }) 149 } 150 151 func WithLongQueryTime(secs float64) tracerProviderOption { 152 return tracerProviderOption(func(cfg *tracerProviderConfig) { 153 cfg.longQueryTime = int64(float64(time.Second) * secs) 154 }) 155 } 156 157 func DebugMode(debug bool) tracerProviderOption { 158 return func(cfg *tracerProviderConfig) { 159 cfg.debugMode = debug 160 } 161 } 162 163 func WithBatchProcessMode(mode string) tracerProviderOption { 164 return func(cfg *tracerProviderConfig) { 165 cfg.batchProcessMode = mode 166 } 167 } 168 func WithBatchProcessor(p BatchProcessor) tracerProviderOption { 169 return func(cfg *tracerProviderConfig) { 170 cfg.batchProcessor = p 171 } 172 } 173 174 func WithSQLExecutor(f func() ie.InternalExecutor) tracerProviderOption { 175 return func(cfg *tracerProviderConfig) { 176 cfg.mux.Lock() 177 defer cfg.mux.Unlock() 178 cfg.sqlExecutor = f 179 } 180 } 181 182 func WithInitAction(init bool) tracerProviderOption { 183 return func(cfg *tracerProviderConfig) { 184 cfg.mux.Lock() 185 defer cfg.mux.Unlock() 186 cfg.needInit = init 187 } 188 } 189 190 var _ trace.IDGenerator = &moIDGenerator{} 191 192 type moIDGenerator struct{} 193 194 func (M moIDGenerator) NewIDs() (trace.TraceID, trace.SpanID) { 195 tid := trace.TraceID{} 196 binary.BigEndian.PutUint64(tid[:], util.Fastrand64()) 197 binary.BigEndian.PutUint64(tid[8:], util.Fastrand64()) 198 sid := trace.SpanID{} 199 binary.BigEndian.PutUint64(sid[:], util.Fastrand64()) 200 return tid, sid 201 } 202 203 func (M moIDGenerator) NewSpanID() trace.SpanID { 204 sid := trace.SpanID{} 205 binary.BigEndian.PutUint64(sid[:], util.Fastrand64()) 206 return sid 207 }