github.com/cosmos/cosmos-sdk@v0.50.10/telemetry/wrapper.go (about)

     1  package telemetry
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashicorp/go-metrics"
     7  )
     8  
     9  // Common metric key constants
    10  const (
    11  	MetricKeyBeginBlocker       = "begin_blocker"
    12  	MetricKeyEndBlocker         = "end_blocker"
    13  	MetricKeyPrepareCheckStater = "prepare_check_stater"
    14  	MetricKeyPrecommiter        = "precommiter"
    15  	MetricLabelNameModule       = "module"
    16  )
    17  
    18  // NewLabel creates a new instance of Label with name and value
    19  func NewLabel(name, value string) metrics.Label {
    20  	return metrics.Label{Name: name, Value: value}
    21  }
    22  
    23  // ModuleMeasureSince provides a short hand method for emitting a time measure
    24  // metric for a module with a given set of keys. If any global labels are defined,
    25  // they will be added to the module label.
    26  func ModuleMeasureSince(module string, start time.Time, keys ...string) {
    27  	if !IsTelemetryEnabled() {
    28  		return
    29  	}
    30  
    31  	metrics.MeasureSinceWithLabels(
    32  		keys,
    33  		start.UTC(),
    34  		append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...),
    35  	)
    36  }
    37  
    38  // ModuleSetGauge provides a short hand method for emitting a gauge metric for a
    39  // module with a given set of keys. If any global labels are defined, they will
    40  // be added to the module label.
    41  func ModuleSetGauge(module string, val float32, keys ...string) {
    42  	if !IsTelemetryEnabled() {
    43  		return
    44  	}
    45  
    46  	metrics.SetGaugeWithLabels(
    47  		keys,
    48  		val,
    49  		append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...),
    50  	)
    51  }
    52  
    53  // IncrCounter provides a wrapper functionality for emitting a counter metric with
    54  // global labels (if any).
    55  func IncrCounter(val float32, keys ...string) {
    56  	if !IsTelemetryEnabled() {
    57  		return
    58  	}
    59  
    60  	metrics.IncrCounterWithLabels(keys, val, globalLabels)
    61  }
    62  
    63  // IncrCounterWithLabels provides a wrapper functionality for emitting a counter
    64  // metric with global labels (if any) along with the provided labels.
    65  func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) {
    66  	if !IsTelemetryEnabled() {
    67  		return
    68  	}
    69  
    70  	metrics.IncrCounterWithLabels(keys, val, append(labels, globalLabels...))
    71  }
    72  
    73  // SetGauge provides a wrapper functionality for emitting a gauge metric with
    74  // global labels (if any).
    75  func SetGauge(val float32, keys ...string) {
    76  	if !IsTelemetryEnabled() {
    77  		return
    78  	}
    79  
    80  	metrics.SetGaugeWithLabels(keys, val, globalLabels)
    81  }
    82  
    83  // SetGaugeWithLabels provides a wrapper functionality for emitting a gauge
    84  // metric with global labels (if any) along with the provided labels.
    85  func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) {
    86  	if !IsTelemetryEnabled() {
    87  		return
    88  	}
    89  
    90  	metrics.SetGaugeWithLabels(keys, val, append(labels, globalLabels...))
    91  }
    92  
    93  // MeasureSince provides a wrapper functionality for emitting a a time measure
    94  // metric with global labels (if any).
    95  func MeasureSince(start time.Time, keys ...string) {
    96  	if !IsTelemetryEnabled() {
    97  		return
    98  	}
    99  
   100  	metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels)
   101  }
   102  
   103  // Now return the current time if telemetry is enabled or a zero time if it's not
   104  func Now() time.Time {
   105  	if !IsTelemetryEnabled() {
   106  		return time.Time{}
   107  	}
   108  
   109  	return time.Now()
   110  }