github.com/celestiaorg/celestia-node@v0.15.0-beta.1/pruner/metrics.go (about)

     1  package pruner
     2  
     3  import (
     4  	"context"
     5  
     6  	"go.opentelemetry.io/otel"
     7  	"go.opentelemetry.io/otel/attribute"
     8  	"go.opentelemetry.io/otel/metric"
     9  )
    10  
    11  var (
    12  	meter = otel.Meter("storage_pruner")
    13  )
    14  
    15  type metrics struct {
    16  	prunedCounter metric.Int64Counter
    17  
    18  	lastPruned   metric.Int64ObservableGauge
    19  	failedPrunes metric.Int64ObservableGauge
    20  }
    21  
    22  func (s *Service) WithMetrics() error {
    23  	prunedCounter, err := meter.Int64Counter("pruner_pruned_counter",
    24  		metric.WithDescription("pruner pruned header counter"))
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	failedPrunes, err := meter.Int64ObservableGauge("pruner_failed_counter",
    30  		metric.WithDescription("pruner failed prunes counter"))
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	lastPruned, err := meter.Int64ObservableGauge("pruner_last_pruned",
    36  		metric.WithDescription("pruner highest pruned height"))
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	callback := func(ctx context.Context, observer metric.Observer) error {
    42  		lastPrunedHeader := s.checkpoint.lastPrunedHeader.Load()
    43  		if lastPrunedHeader != nil {
    44  			observer.ObserveInt64(lastPruned, int64(lastPrunedHeader.Height()))
    45  		}
    46  		observer.ObserveInt64(failedPrunes, int64(len(s.checkpoint.FailedHeaders)))
    47  		return nil
    48  	}
    49  
    50  	if _, err := meter.RegisterCallback(callback, lastPruned, failedPrunes); err != nil {
    51  		return err
    52  	}
    53  
    54  	s.metrics = &metrics{
    55  		prunedCounter: prunedCounter,
    56  		lastPruned:    lastPruned,
    57  		failedPrunes:  failedPrunes,
    58  	}
    59  	return nil
    60  }
    61  
    62  func (m *metrics) observePrune(ctx context.Context, failed bool) {
    63  	if m == nil {
    64  		return
    65  	}
    66  	if ctx.Err() != nil {
    67  		ctx = context.Background()
    68  	}
    69  	m.prunedCounter.Add(ctx, 1, metric.WithAttributes(
    70  		attribute.Bool("failed", failed)))
    71  }