github.com/matrixorigin/matrixone@v1.2.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/config"
    23  	"github.com/matrixorigin/matrixone/pkg/util"
    24  	"github.com/matrixorigin/matrixone/pkg/util/export/table"
    25  	ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor"
    26  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    27  )
    28  
    29  const (
    30  	MOStatementType = "statement"
    31  	MOSpanType      = "span"
    32  	MOLogType       = "log"
    33  	MOErrorType     = "error"
    34  	MORawLogType    = "rawlog"
    35  )
    36  
    37  // tracerProviderConfig.
    38  type tracerProviderConfig struct {
    39  	// spanProcessors contains collection of SpanProcessors that are processing pipeline
    40  	// for spans in the trace signal.
    41  	// SpanProcessors registered with a TracerProvider and are called at the start
    42  	// and end of a Span's lifecycle, and are called in the order they are
    43  	// registered.
    44  	spanProcessors []trace.SpanProcessor
    45  
    46  	enable bool // SetEnable
    47  
    48  	// idGenerator is used to generate all Span and Trace IDs when needed.
    49  	idGenerator trace.IDGenerator
    50  
    51  	// resource contains attributes representing an entity that produces telemetry.
    52  	resource *trace.Resource // withMOVersion, WithNode,
    53  
    54  	// disableSpan
    55  	disableSpan bool
    56  	// disableError
    57  	disableError bool
    58  	// debugMode used in Tracer.Debug
    59  	debugMode bool // DebugMode
    60  
    61  	batchProcessor BatchProcessor // WithBatchProcessor
    62  
    63  	// writerFactory gen writer for CSV output
    64  	writerFactory table.WriterFactory // WithFSWriterFactory, default from export.GetFSWriterFactory4Trace
    65  	// disableSqlWriter
    66  	disableSqlWriter bool // set by WithSQLWriterDisable
    67  
    68  	// stmt aggregation
    69  	disableStmtAggregation bool          // set by WithStmtAggregationDisable
    70  	enableStmtMerge        bool          // set by WithStmtMergeDisable
    71  	aggregationWindow      time.Duration // WithAggregationWindow
    72  	selectAggrThreshold    time.Duration // WithSelectThreshold
    73  
    74  	sqlExecutor func() ie.InternalExecutor // WithSQLExecutor
    75  	// needInit control table schema create
    76  	needInit bool // WithInitAction
    77  
    78  	exportInterval time.Duration //  WithExportInterval
    79  	// longQueryTime unit ns
    80  	longQueryTime int64 //  WithLongQueryTime
    81  	// longSpanTime
    82  	longSpanTime time.Duration
    83  	// skipRunningStmt
    84  	skipRunningStmt bool // set by WithSkipRunningStmt
    85  
    86  	bufferSizeThreshold int64 // WithBufferSizeThreshold
    87  
    88  	cuConfig   config.OBCUConfig // WithCUConfig
    89  	cuConfigV1 config.OBCUConfig // WithCUConfig
    90  
    91  	tcpPacket bool // WithTCPPacket
    92  
    93  	labels map[string]string
    94  
    95  	mux sync.RWMutex
    96  }
    97  
    98  func (cfg *tracerProviderConfig) getNodeResource() *trace.MONodeResource {
    99  	cfg.mux.RLock()
   100  	defer cfg.mux.RUnlock()
   101  	if val, has := cfg.resource.Get("Node"); !has {
   102  		return &trace.MONodeResource{}
   103  	} else {
   104  		return val.(*trace.MONodeResource)
   105  	}
   106  }
   107  
   108  func (cfg *tracerProviderConfig) IsEnable() bool {
   109  	cfg.mux.RLock()
   110  	defer cfg.mux.RUnlock()
   111  	return cfg.enable
   112  }
   113  
   114  func (cfg *tracerProviderConfig) SetEnable(enable bool) {
   115  	cfg.mux.Lock()
   116  	defer cfg.mux.Unlock()
   117  	cfg.enable = enable
   118  }
   119  
   120  func (cfg *tracerProviderConfig) GetSqlExecutor() func() ie.InternalExecutor {
   121  	cfg.mux.RLock()
   122  	defer cfg.mux.RUnlock()
   123  	return cfg.sqlExecutor
   124  }
   125  
   126  // TracerProviderOption configures a TracerProvider.
   127  type TracerProviderOption interface {
   128  	apply(*tracerProviderConfig)
   129  }
   130  
   131  type tracerProviderOption func(config *tracerProviderConfig)
   132  
   133  func (f tracerProviderOption) apply(config *tracerProviderConfig) {
   134  	f(config)
   135  }
   136  
   137  func withMOVersion(v string) tracerProviderOption {
   138  	return func(config *tracerProviderConfig) {
   139  		config.resource.Put("version", v)
   140  	}
   141  }
   142  
   143  // WithNode give id as NodeId, t as NodeType
   144  func WithNode(uuid string, t string) tracerProviderOption {
   145  	return func(cfg *tracerProviderConfig) {
   146  		cfg.resource.Put("Node", &trace.MONodeResource{
   147  			NodeUuid: uuid,
   148  			NodeType: t,
   149  		})
   150  	}
   151  }
   152  
   153  func EnableTracer(enable bool) tracerProviderOption {
   154  	return func(cfg *tracerProviderConfig) {
   155  		cfg.SetEnable(enable)
   156  	}
   157  }
   158  
   159  func WithFSWriterFactory(f table.WriterFactory) tracerProviderOption {
   160  	return tracerProviderOption(func(cfg *tracerProviderConfig) {
   161  		cfg.writerFactory = f
   162  	})
   163  }
   164  
   165  func WithExportInterval(secs int) tracerProviderOption {
   166  	return tracerProviderOption(func(cfg *tracerProviderConfig) {
   167  		cfg.exportInterval = time.Second * time.Duration(secs)
   168  	})
   169  }
   170  
   171  func WithLongQueryTime(secs float64) tracerProviderOption {
   172  	return tracerProviderOption(func(cfg *tracerProviderConfig) {
   173  		cfg.longQueryTime = int64(float64(time.Second) * secs)
   174  	})
   175  }
   176  
   177  func WithLongSpanTime(d time.Duration) tracerProviderOption {
   178  	return tracerProviderOption(func(cfg *tracerProviderConfig) {
   179  		cfg.longSpanTime = d
   180  	})
   181  }
   182  
   183  func WithSpanDisable(disable bool) tracerProviderOption {
   184  	return func(cfg *tracerProviderConfig) {
   185  		cfg.disableSpan = disable
   186  	}
   187  }
   188  
   189  func WithErrorDisable(disable bool) tracerProviderOption {
   190  	return func(cfg *tracerProviderConfig) {
   191  		cfg.disableError = disable
   192  	}
   193  }
   194  
   195  func WithSkipRunningStmt(skip bool) tracerProviderOption {
   196  	return func(cfg *tracerProviderConfig) {
   197  		cfg.skipRunningStmt = skip
   198  	}
   199  }
   200  
   201  func WithSQLWriterDisable(disable bool) tracerProviderOption {
   202  	return func(cfg *tracerProviderConfig) {
   203  		cfg.disableSqlWriter = disable
   204  	}
   205  }
   206  
   207  func WithAggregatorDisable(disable bool) tracerProviderOption {
   208  	return func(cfg *tracerProviderConfig) {
   209  		cfg.disableStmtAggregation = disable
   210  	}
   211  }
   212  
   213  func WithStmtMergeEnable(enable bool) tracerProviderOption {
   214  	return func(cfg *tracerProviderConfig) {
   215  		cfg.enableStmtMerge = enable
   216  	}
   217  }
   218  
   219  func WithCUConfig(cu config.OBCUConfig, cuv1 config.OBCUConfig) tracerProviderOption {
   220  	return func(cfg *tracerProviderConfig) {
   221  		cfg.cuConfig = cu
   222  		cfg.cuConfigV1 = cuv1
   223  	}
   224  }
   225  
   226  func WithTCPPacket(count bool) tracerProviderOption {
   227  	return func(cfg *tracerProviderConfig) {
   228  		cfg.tcpPacket = count
   229  	}
   230  }
   231  
   232  func WithLabels(l map[string]string) tracerProviderOption {
   233  	return func(cfg *tracerProviderConfig) {
   234  		cfg.labels = l
   235  	}
   236  }
   237  
   238  func WithAggregatorWindow(window time.Duration) tracerProviderOption {
   239  	return func(cfg *tracerProviderConfig) {
   240  		cfg.aggregationWindow = window
   241  	}
   242  }
   243  
   244  func WithSelectThreshold(window time.Duration) tracerProviderOption {
   245  	return func(cfg *tracerProviderConfig) {
   246  		cfg.selectAggrThreshold = window
   247  	}
   248  }
   249  
   250  func WithBufferSizeThreshold(size int64) tracerProviderOption {
   251  	return tracerProviderOption(func(cfg *tracerProviderConfig) {
   252  		cfg.bufferSizeThreshold = size
   253  	})
   254  }
   255  
   256  func DebugMode(debug bool) tracerProviderOption {
   257  	return func(cfg *tracerProviderConfig) {
   258  		cfg.debugMode = debug
   259  	}
   260  }
   261  
   262  func WithBatchProcessor(p BatchProcessor) tracerProviderOption {
   263  	return func(cfg *tracerProviderConfig) {
   264  		cfg.batchProcessor = p
   265  	}
   266  }
   267  
   268  func WithSQLExecutor(f func() ie.InternalExecutor) tracerProviderOption {
   269  	return func(cfg *tracerProviderConfig) {
   270  		cfg.mux.Lock()
   271  		defer cfg.mux.Unlock()
   272  		cfg.sqlExecutor = f
   273  	}
   274  }
   275  
   276  func WithInitAction(init bool) tracerProviderOption {
   277  	return func(cfg *tracerProviderConfig) {
   278  		cfg.mux.Lock()
   279  		defer cfg.mux.Unlock()
   280  		cfg.needInit = init
   281  	}
   282  }
   283  
   284  var _ trace.IDGenerator = &moIDGenerator{}
   285  
   286  type moIDGenerator struct{}
   287  
   288  func (M moIDGenerator) NewIDs() (trace.TraceID, trace.SpanID) {
   289  	tid := trace.TraceID{}
   290  	binary.BigEndian.PutUint64(tid[:], util.Fastrand64())
   291  	binary.BigEndian.PutUint64(tid[8:], util.Fastrand64())
   292  	sid := trace.SpanID{}
   293  	binary.BigEndian.PutUint64(sid[:], util.Fastrand64())
   294  	return tid, sid
   295  }
   296  
   297  func (M moIDGenerator) NewSpanID() trace.SpanID {
   298  	sid := trace.SpanID{}
   299  	binary.BigEndian.PutUint64(sid[:], util.Fastrand64())
   300  	return sid
   301  }