infra/op-ufm/pkg/metrics/metrics.go

167 lines
4.3 KiB
Go
Raw Normal View History

package metrics
import (
2023-07-15 00:36:58 +03:00
fmt "fmt"
"regexp"
"strings"
"time"
2023-07-15 00:08:02 +03:00
"github.com/ethereum/go-ethereum/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
MetricsNamespace = "ufm"
)
var (
2023-07-15 00:08:02 +03:00
Debug bool
2023-07-18 20:21:19 +03:00
errorsTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "errors_total",
2023-07-18 20:21:19 +03:00
Help: "Count of errors",
}, []string{
"provider",
"error",
})
rpcLatency = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "rpc_latency",
Help: "RPC latency per provider, client and method (ms)",
}, []string{
"provider",
"client",
"method",
})
roundTripLatency = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "roundtrip_latency",
Help: "Round trip latency per provider (ms)",
}, []string{
"provider",
})
gasUsed = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "gas_used",
Help: "Gas used per provider",
}, []string{
"provider",
})
firstSeenLatency = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "first_seen_latency",
Help: "First seen latency latency per provider (ms)",
}, []string{
"provider_source",
"provider_seen",
})
providerToProviderLatency = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "provider_to_provider_latency",
Help: "Provider to provider latency (ms)",
}, []string{
"provider_source",
"provider_seen",
})
networkTransactionsInFlight = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "transactions_inflight",
Help: "Transactions in flight, per network",
}, []string{
"network",
})
)
2023-07-15 00:36:58 +03:00
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z ]+`)
func RecordError(provider string, errorLabel string) {
2023-07-15 00:08:02 +03:00
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric inc",
"m", "errors_total",
"provider", provider,
"error", errorLabel)
2023-07-15 00:08:02 +03:00
}
errorsTotal.WithLabelValues(provider, errorLabel).Inc()
}
2023-07-15 00:36:58 +03:00
// RecordErrorDetails concats the error message to the label removing non-alpha chars
func RecordErrorDetails(provider string, label string, err error) {
errClean := nonAlphanumericRegex.ReplaceAllString(err.Error(), "")
errClean = strings.ReplaceAll(errClean, " ", "_")
errClean = strings.ReplaceAll(errClean, "__", "_")
label = fmt.Sprintf("%s.%s", label, errClean)
2023-07-15 00:36:58 +03:00
RecordError(provider, label)
}
func RecordRPCLatency(provider string, client string, method string, latency time.Duration) {
2023-07-15 00:08:02 +03:00
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric set",
"m", "rpc_latency",
"provider", provider,
"client", client,
"method", method,
"latency", latency)
2023-07-15 00:08:02 +03:00
}
rpcLatency.WithLabelValues(provider, client, method).Set(float64(latency.Milliseconds()))
}
func RecordRoundTripLatency(provider string, latency time.Duration) {
2023-07-15 00:08:02 +03:00
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric set",
"m", "roundtrip_latency",
"provider", provider,
"latency", latency)
2023-07-15 00:08:02 +03:00
}
roundTripLatency.WithLabelValues(provider).Set(float64(latency.Milliseconds()))
}
func RecordGasUsed(provider string, val uint64) {
2023-07-15 00:08:02 +03:00
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric add",
"m", "gas_used",
"provider", provider,
"val", val)
2023-07-15 00:08:02 +03:00
}
2023-07-18 20:21:19 +03:00
gasUsed.WithLabelValues(provider).Set(float64(val))
}
2023-07-15 00:08:02 +03:00
func RecordFirstSeenLatency(providerSource string, providerSeen string, latency time.Duration) {
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric set",
"m", "first_seen_latency",
"provider_source", providerSource,
"provider_seen", providerSeen,
"latency", latency)
2023-07-15 00:08:02 +03:00
}
firstSeenLatency.WithLabelValues(providerSource, providerSeen).Set(float64(latency.Milliseconds()))
}
2023-07-15 00:08:02 +03:00
func RecordProviderToProviderLatency(providerSource string, providerSeen string, latency time.Duration) {
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric set",
"m", "provider_to_provider_latency",
"provider_source", providerSource,
"provider_seen", providerSeen,
"latency", latency)
2023-07-15 00:08:02 +03:00
}
providerToProviderLatency.WithLabelValues(providerSource, providerSeen).Set(float64(latency.Milliseconds()))
}
func RecordTransactionsInFlight(network string, count int) {
2023-07-15 00:08:02 +03:00
if Debug {
2023-08-31 23:31:34 +03:00
log.Debug("metric set",
"m", "transactions_inflight",
"network", network,
"count", count)
2023-07-15 00:08:02 +03:00
}
networkTransactionsInFlight.WithLabelValues(network).Set(float64(count))
}