github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/tracing/tx_tracing.go (about)

     1  package tracing
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/opentracing/opentracing-go"
     7  	"github.com/unicornultrafoundation/go-u2u/common"
     8  )
     9  
    10  var (
    11  	enabled   bool
    12  	txSpans   = make(map[common.Hash]opentracing.Span)
    13  	txSpansMu sync.RWMutex
    14  
    15  	noopSpan = opentracing.NoopTracer{}.StartSpan("")
    16  )
    17  
    18  func SetEnabled(val bool) {
    19  	enabled = val
    20  }
    21  
    22  func Enabled() bool {
    23  	return enabled
    24  }
    25  
    26  func StartTx(tx common.Hash, operation string) {
    27  	if !enabled {
    28  		return
    29  	}
    30  
    31  	txSpansMu.Lock()
    32  	defer txSpansMu.Unlock()
    33  
    34  	if _, ok := txSpans[tx]; ok {
    35  		return
    36  	}
    37  
    38  	span := opentracing.StartSpan("lifecycle")
    39  	span.SetTag("txhash", tx.String())
    40  	span.SetTag("enter", operation)
    41  	txSpans[tx] = span
    42  }
    43  
    44  func FinishTx(tx common.Hash, operation string) {
    45  	if !enabled {
    46  		return
    47  	}
    48  
    49  	txSpansMu.Lock()
    50  	defer txSpansMu.Unlock()
    51  
    52  	span, ok := txSpans[tx]
    53  	if !ok {
    54  		return
    55  	}
    56  
    57  	span.SetTag("exit", operation)
    58  	span.Finish()
    59  	delete(txSpans, tx)
    60  }
    61  
    62  func CheckTx(tx common.Hash, operation string) opentracing.Span {
    63  	if !enabled {
    64  		return noopSpan
    65  	}
    66  
    67  	txSpansMu.RLock()
    68  	defer txSpansMu.RUnlock()
    69  
    70  	span, ok := txSpans[tx]
    71  
    72  	if !ok {
    73  		return noopSpan
    74  	}
    75  
    76  	return opentracing.GlobalTracer().StartSpan(
    77  		operation,
    78  		opentracing.ChildOf(span.Context()),
    79  	)
    80  }