github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/metrics/retry.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     7  )
     8  
     9  func retry(config Config) (t trace.Retry) {
    10  	config = config.WithSystem("retry")
    11  	errs := config.CounterVec("errors", "status", "retry_label", "final")
    12  	attempts := config.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}, "retry_label")
    13  	latency := config.TimerVec("latency", "retry_label")
    14  	t.OnRetry = func(info trace.RetryLoopStartInfo) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
    15  		label := info.Label
    16  		if label == "" {
    17  			return nil
    18  		}
    19  		start := time.Now()
    20  
    21  		return func(info trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
    22  			if info.Error != nil && config.Details()&trace.RetryEvents != 0 {
    23  				errs.With(map[string]string{
    24  					"status":      errorBrief(info.Error),
    25  					"retry_label": label,
    26  					"final":       "false",
    27  				}).Inc()
    28  			}
    29  
    30  			return func(info trace.RetryLoopDoneInfo) {
    31  				if config.Details()&trace.RetryEvents != 0 {
    32  					attempts.With(map[string]string{
    33  						"retry_label": label,
    34  					}).Record(float64(info.Attempts))
    35  					errs.With(map[string]string{
    36  						"status":      errorBrief(info.Error),
    37  						"retry_label": label,
    38  						"final":       "true",
    39  					}).Inc()
    40  					latency.With(map[string]string{
    41  						"retry_label": label,
    42  					}).Record(time.Since(start))
    43  				}
    44  			}
    45  		}
    46  	}
    47  
    48  	return t
    49  }