github.com/sunrise-zone/sunrise-node@v0.13.1-sr2/share/p2p/metrics.go (about)

     1  package p2p
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"go.opentelemetry.io/otel"
     8  	"go.opentelemetry.io/otel/attribute"
     9  	"go.opentelemetry.io/otel/metric"
    10  
    11  	"github.com/sunrise-zone/sunrise-node/libs/utils"
    12  )
    13  
    14  var meter = otel.Meter("shrex/eds")
    15  
    16  type status string
    17  
    18  const (
    19  	StatusBadRequest  status = "bad_request"
    20  	StatusSendRespErr status = "send_resp_err"
    21  	StatusSendReqErr  status = "send_req_err"
    22  	StatusReadRespErr status = "read_resp_err"
    23  	StatusInternalErr status = "internal_err"
    24  	StatusNotFound    status = "not_found"
    25  	StatusTimeout     status = "timeout"
    26  	StatusSuccess     status = "success"
    27  	StatusRateLimited status = "rate_limited"
    28  )
    29  
    30  type Metrics struct {
    31  	totalRequestCounter metric.Int64Counter
    32  }
    33  
    34  // ObserveRequests increments the total number of requests sent with the given status as an
    35  // attribute.
    36  func (m *Metrics) ObserveRequests(ctx context.Context, count int64, status status) {
    37  	if m == nil {
    38  		return
    39  	}
    40  	ctx = utils.ResetContextOnError(ctx)
    41  	m.totalRequestCounter.Add(ctx, count,
    42  		metric.WithAttributes(
    43  			attribute.String("status", string(status)),
    44  		))
    45  }
    46  
    47  func InitClientMetrics(protocol string) (*Metrics, error) {
    48  	totalRequestCounter, err := meter.Int64Counter(
    49  		fmt.Sprintf("shrex_%s_client_total_requests", protocol),
    50  		metric.WithDescription(fmt.Sprintf("Total count of sent shrex/%s requests", protocol)),
    51  	)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return &Metrics{
    57  		totalRequestCounter: totalRequestCounter,
    58  	}, nil
    59  }
    60  
    61  func InitServerMetrics(protocol string) (*Metrics, error) {
    62  	totalRequestCounter, err := meter.Int64Counter(
    63  		fmt.Sprintf("shrex_%s_server_total_responses", protocol),
    64  		metric.WithDescription(fmt.Sprintf("Total count of sent shrex/%s responses", protocol)),
    65  	)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return &Metrics{
    71  		totalRequestCounter: totalRequestCounter,
    72  	}, nil
    73  }