github.com/grafana/pyroscope@v1.18.0/pkg/metastore/fsm/tracing_tx.go (about)

     1  package fsm
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/opentracing/opentracing-go"
     7  	"go.etcd.io/bbolt"
     8  )
     9  
    10  // tracingTx wraps a BoltDB transaction to automatically trace transaction lifecycle.
    11  // It holds a span that encompasses the entire transaction, providing visibility into
    12  // transaction timing without requiring manual instrumentation.
    13  //
    14  // The span should be created by the caller and will be finished when the transaction
    15  // is committed or rolled back.
    16  type tracingTx struct {
    17  	*bbolt.Tx
    18  	span    opentracing.Span
    19  	spanCtx context.Context // Context with the span, for child operations
    20  }
    21  
    22  // newTracingTx creates a tracing transaction wrapper.
    23  // The span parameter can be nil if no tracing is desired (e.g., on follower nodes).
    24  func newTracingTx(tx *bbolt.Tx, span opentracing.Span, spanCtx context.Context) *tracingTx {
    25  	return &tracingTx{
    26  		Tx:      tx,
    27  		span:    span,
    28  		spanCtx: spanCtx,
    29  	}
    30  }
    31  
    32  // Commit commits the transaction and finishes the span.
    33  func (t *tracingTx) Commit() error {
    34  	if t.span != nil {
    35  		defer t.span.Finish()
    36  		t.span.LogKV("operation", "commit")
    37  	}
    38  	return t.Tx.Commit()
    39  }
    40  
    41  // Rollback rolls back the transaction and finishes the span.
    42  func (t *tracingTx) Rollback() error {
    43  	if t.span != nil {
    44  		defer t.span.Finish()
    45  		t.span.LogKV("operation", "rollback")
    46  	}
    47  	return t.Tx.Rollback()
    48  }