-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
82 lines (70 loc) · 2.48 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package metrics
import (
"math/big"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
)
// IncrCountMetricWithLabels increases a count metric from a module with the provided labels by a count of 1.
func IncrCountMetricWithLabels(module string, metric string, labels ...gometrics.Label) {
telemetry.IncrCounterWithLabels(
[]string{module, metric, Count},
1,
labels,
)
}
// IncrSuccessOrErrorCounter increments either the success or error counter for a given handler
// based on whether the given error is nil or not. This function is intended to be called in a
// defer block at the top of any function which returns an error.
func IncrSuccessOrErrorCounter(err error, module string, handler string, callback string, labels ...gometrics.Label) {
successOrError := Success
if err != nil {
successOrError = Error
}
telemetry.IncrCounterWithLabels(
[]string{
module,
handler,
successOrError,
Count,
},
1,
append(
[]gometrics.Label{
GetLabelForStringValue(Callback, callback),
},
labels...,
),
)
}
// GetLabelForBoolValue returns a telemetry label for a given label and bool value.
func GetLabelForBoolValue(labelName string, labelValue bool) gometrics.Label {
return GetLabelForStringValue(labelName, strconv.FormatBool(labelValue))
}
// GetLabelForIntValue returns a telemetry label for a given label and int value.
func GetLabelForIntValue(labelName string, labelValue int) gometrics.Label {
return GetLabelForStringValue(labelName, strconv.Itoa(labelValue))
}
// GetLabelForStringValue returns a telemetry label for a given label and string value.
func GetLabelForStringValue(labelName string, labelValue string) gometrics.Label {
return telemetry.NewLabel(labelName, labelValue)
}
// GetMetricValueFromBigInt returns a telemetry value (float32) from an integer value.
// Any rounding information is ignored, so this function should only be used for metrics.
func GetMetricValueFromBigInt(i *big.Int) float32 {
r, _ := new(big.Float).SetInt(i).Float32()
return r
}
// GetCallbackMetricFromCtx determines the callback metric based on the context. Note that DeliverTx is implied
// if the context is not CheckTx or ReCheckTx. This function is unable to account for other callbacks like
// PrepareCheckState or EndBlocker.
func GetCallbackMetricFromCtx(ctx sdk.Context) string {
if ctx.IsReCheckTx() {
return ReCheckTx
}
if ctx.IsCheckTx() {
return CheckTx
}
return DeliverTx
}