github.com/Finschia/finschia-sdk@v0.48.1/telemetry/wrapper.go (about)

     1  package telemetry
     2  
     3  import (
     4  	"time"
     5  
     6  	metrics "github.com/armon/go-metrics"
     7  )
     8  
     9  // Common metric key constants
    10  const (
    11  	MetricKeyBeginBlocker = "begin_blocker"
    12  	MetricKeyEndBlocker   = "end_blocker"
    13  	MetricLabelNameModule = "module"
    14  )
    15  
    16  // NewLabel creates a new instance of Label with name and value
    17  func NewLabel(name, value string) metrics.Label {
    18  	return metrics.Label{Name: name, Value: value}
    19  }
    20  
    21  // ModuleMeasureSince provides a short hand method for emitting a time measure
    22  // metric for a module with a given set of keys. If any global labels are defined,
    23  // they will be added to the module label.
    24  func ModuleMeasureSince(module string, start time.Time, keys ...string) {
    25  	metrics.MeasureSinceWithLabels(
    26  		keys,
    27  		start.UTC(),
    28  		append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...),
    29  	)
    30  }
    31  
    32  // ModuleSetGauge provides a short hand method for emitting a gauge metric for a
    33  // module with a given set of keys. If any global labels are defined, they will
    34  // be added to the module label.
    35  func ModuleSetGauge(module string, val float32, keys ...string) {
    36  	metrics.SetGaugeWithLabels(
    37  		keys,
    38  		val,
    39  		append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...),
    40  	)
    41  }
    42  
    43  // IncrCounter provides a wrapper functionality for emitting a counter metric with
    44  // global labels (if any).
    45  func IncrCounter(val float32, keys ...string) {
    46  	metrics.IncrCounterWithLabels(keys, val, globalLabels)
    47  }
    48  
    49  // IncrCounterWithLabels provides a wrapper functionality for emitting a counter
    50  // metric with global labels (if any) along with the provided labels.
    51  func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) {
    52  	metrics.IncrCounterWithLabels(keys, val, append(labels, globalLabels...))
    53  }
    54  
    55  // SetGauge provides a wrapper functionality for emitting a gauge metric with
    56  // global labels (if any).
    57  func SetGauge(val float32, keys ...string) {
    58  	metrics.SetGaugeWithLabels(keys, val, globalLabels)
    59  }
    60  
    61  // SetGaugeWithLabels provides a wrapper functionality for emitting a gauge
    62  // metric with global labels (if any) along with the provided labels.
    63  func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) {
    64  	metrics.SetGaugeWithLabels(keys, val, append(labels, globalLabels...))
    65  }
    66  
    67  // MeasureSince provides a wrapper functionality for emitting a a time measure
    68  // metric with global labels (if any).
    69  func MeasureSince(start time.Time, keys ...string) {
    70  	metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels)
    71  }