From 7d3c6c2217de2120c44cc1314f206b077aa9c341 Mon Sep 17 00:00:00 2001 From: Peter Olsar Date: Tue, 15 Oct 2024 21:05:56 -0700 Subject: [PATCH] feat: prometheus metrics for counting ok requests --- internal/checks/error.go | 7 +++++++ internal/checks/latency.go | 7 +++++++ internal/metrics/metrics.go | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/checks/error.go b/internal/checks/error.go index 02e49e9..180f33b 100644 --- a/internal/checks/error.go +++ b/internal/checks/error.go @@ -215,6 +215,13 @@ func (c *ErrorCheck) RecordRequest(data *types.RequestData) bool { // Even though this is a single HTTP request, we count each RPC JSON subresponse error. c.errorCircuitBreaker.RecordResponse(true) // JSON RPC subrequest error } else { + c.metricsContainer.ErrorLatencyCheckNoErrors.WithLabelValues( + c.upstreamConfig.ID, + c.upstreamConfig.HTTPURL, + metrics.HTTPRequest, + data.Method, + ).Inc() + c.errorCircuitBreaker.RecordResponse(false) // JSON RPC subrequest OK } } diff --git a/internal/checks/latency.go b/internal/checks/latency.go index 219bb98..83f6cdb 100644 --- a/internal/checks/latency.go +++ b/internal/checks/latency.go @@ -170,6 +170,13 @@ func (c *LatencyCheck) RecordRequest(data *types.RequestData) bool { ).Inc() isHighLatency = true + } else { + c.metricsContainer.ErrorLatencyCheckOkLatencies.WithLabelValues( + c.upstreamConfig.ID, + c.upstreamConfig.HTTPURL, + metrics.HTTPRequest, + data.Method, + ).Inc() } } diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 321acbf..5188417 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -259,6 +259,16 @@ var ( []string{"chain_name", "upstream_id", "url", "errorType", "method"}, ) + errorLatencyStatusCheckNoErrors = promauto.NewCounterVec( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Subsystem: "healthcheck", + Name: "latency_check_no_errors", + Help: "No errors of upstream requests.", + }, + []string{"chain_name", "upstream_id", "url", "errorType", "method"}, + ) + errorLatencyStatusHighLatencies = promauto.NewCounterVec( prometheus.CounterOpts{ Namespace: metricsNamespace, @@ -269,6 +279,16 @@ var ( []string{"chain_name", "upstream_id", "url", "errorType", "method"}, ) + errorLatencyStatusOkLatencies = promauto.NewCounterVec( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Subsystem: "healthcheck", + Name: "latency_check_ok_latency", + Help: "Latency of upstream OK.", + }, + []string{"chain_name", "upstream_id", "url", "errorType", "method"}, + ) + // System metrics fileDescriptorsUsed = promauto.NewGauge( prometheus.GaugeOpts{ @@ -305,9 +325,9 @@ type Container struct { SyncStatusCheckErrors *prometheus.CounterVec ErrorLatency *prometheus.GaugeVec - ErrorLatencyCheckRequests *prometheus.CounterVec - ErrorLatencyCheckDuration prometheus.ObserverVec + ErrorLatencyCheckNoErrors *prometheus.CounterVec ErrorLatencyCheckErrors *prometheus.CounterVec + ErrorLatencyCheckOkLatencies *prometheus.CounterVec ErrorLatencyCheckHighLatencies *prometheus.CounterVec } @@ -343,7 +363,9 @@ func NewContainer(chainName string) *Container { result.ErrorLatency = errorLatencyStatus.MustCurryWith(presetLabels) result.ErrorLatencyCheckErrors = errorLatencyStatusCheckErrors.MustCurryWith(presetLabels) + result.ErrorLatencyCheckNoErrors = errorLatencyStatusCheckNoErrors.MustCurryWith(presetLabels) result.ErrorLatencyCheckHighLatencies = errorLatencyStatusHighLatencies.MustCurryWith(presetLabels) + result.ErrorLatencyCheckOkLatencies = errorLatencyStatusOkLatencies.MustCurryWith(presetLabels) return result }