github.com/RevenueMonster/sqlike@v1.0.6/plugin/opentracing/opentracing.go (about)

     1  package opentracing
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/RevenueMonster/sqlike/sql/instrumented"
     7  	"github.com/opentracing/opentracing-go"
     8  	"github.com/opentracing/opentracing-go/ext"
     9  )
    10  
    11  // TraceOptions :
    12  type TraceOptions struct {
    13  	// Component is a component name in opentracing
    14  	// component: value
    15  	Component string
    16  
    17  	// DBInstance is a db instance name in opentracing
    18  	// db.instance: value
    19  	DBInstance string
    20  
    21  	// DBType is a db type in opentracing
    22  	// db.type: value
    23  	DBType string
    24  
    25  	// DBUser is a db user in opentracing
    26  	// db.user: value
    27  	DBUser string
    28  
    29  	// Ping is a flag to log the ping
    30  	Ping bool
    31  
    32  	// Prepare is a flag to log the prepare stmt
    33  	Prepare bool
    34  
    35  	// RowsNext is a flag to log the rows next
    36  	RowsNext     bool
    37  	RowsClose    bool
    38  	RowsAffected bool
    39  	LastInsertID bool
    40  
    41  	// when Query is true, it will log all the query statement
    42  	Query bool
    43  
    44  	// when Exec is true, it will log all the exec statement
    45  	Exec       bool
    46  	BeginTx    bool
    47  	TxCommit   bool
    48  	TxRollback bool
    49  
    50  	// when Args is true, it will log all the arguments of the query
    51  	Args bool
    52  }
    53  
    54  // OpenTracingInterceptor :
    55  type OpenTracingInterceptor struct {
    56  	opts TraceOptions
    57  	instrumented.NullInterceptor
    58  }
    59  
    60  // TraceOption :
    61  type TraceOption func(*TraceOptions)
    62  
    63  var (
    64  	noopTracer                          = &opentracing.NoopTracer{}
    65  	_          instrumented.Interceptor = (*OpenTracingInterceptor)(nil)
    66  )
    67  
    68  // NewInterceptor :
    69  func NewInterceptor(opts ...TraceOption) instrumented.Interceptor {
    70  	it := new(OpenTracingInterceptor)
    71  	it.opts.Component = "database/sql"
    72  	it.opts.DBType = "sql"
    73  	for _, opt := range opts {
    74  		opt(&it.opts)
    75  	}
    76  	return it
    77  }
    78  
    79  // MaybeStartSpanFromContext :
    80  func (ot *OpenTracingInterceptor) MaybeStartSpanFromContext(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
    81  	var span opentracing.Span
    82  	opentracing.StartSpanFromContext(ctx, operationName)
    83  	if sp := opentracing.SpanFromContext(ctx); sp != nil {
    84  		span, ctx = opentracing.StartSpanFromContext(ctx, operationName)
    85  	} else {
    86  		span = noopTracer.StartSpan(operationName)
    87  	}
    88  	ext.DBInstance.Set(span, ot.opts.DBInstance)
    89  	ext.DBType.Set(span, ot.opts.DBType)
    90  	ext.DBUser.Set(span, ot.opts.DBUser)
    91  	ext.Component.Set(span, ot.opts.Component)
    92  	return span, ctx
    93  }