From 1ccba2a8b6759b8cb32035eb938bd4b4e666e5d2 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 10:07:54 -0500 Subject: [PATCH] Add hook for logging errors and warnings --- cmd/soroban-rpc/internal/daemon/metrics.go | 14 +++++++------- cmd/soroban-rpc/internal/test/metrics_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cmd/soroban-rpc/internal/daemon/metrics.go b/cmd/soroban-rpc/internal/daemon/metrics.go index c7c44484..0053b48d 100644 --- a/cmd/soroban-rpc/internal/daemon/metrics.go +++ b/cmd/soroban-rpc/internal/daemon/metrics.go @@ -16,13 +16,17 @@ import ( ) func (d *Daemon) registerMetrics() { + // LogMetricsHook is a metric which counts log lines emitted by soroban rpc + logMetricsHook := logmetrics.New(prometheusNamespace) + d.logger.AddHook(logMetricsHook) + for _, counter := range logMetricsHook { + d.metricsRegistry.MustRegister(counter) + } + buildInfoGauge := prometheus.NewGaugeVec( prometheus.GaugeOpts{Namespace: prometheusNamespace, Subsystem: "build", Name: "info"}, []string{"version", "goversion", "commit", "branch", "build_timestamp"}, ) - // LogMetricsHook is a metric which counts log lines emitted by soroban rpc - LogMetricsHook := logmetrics.New(prometheusNamespace) - // buildInfoGauge.With(prometheus.Labels{ "version": config.Version, "commit": config.CommitHash, @@ -34,10 +38,6 @@ func (d *Daemon) registerMetrics() { d.metricsRegistry.MustRegister(prometheus.NewGoCollector()) d.metricsRegistry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) d.metricsRegistry.MustRegister(buildInfoGauge) - - for _, counter := range LogMetricsHook { - d.metricsRegistry.MustRegister(counter) - } } func (d *Daemon) MetricsRegistry() *prometheus.Registry { diff --git a/cmd/soroban-rpc/internal/test/metrics_test.go b/cmd/soroban-rpc/internal/test/metrics_test.go index b807cb77..0f1a1562 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -2,6 +2,12 @@ package test import ( "fmt" + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/sirupsen/logrus" + "github.com/stellar/go/support/errors" + supportlog "github.com/stellar/go/support/log" + "github.com/stellar/go/support/logmetrics" + "github.com/stretchr/testify/assert" "io" "net/http" "net/url" @@ -27,6 +33,19 @@ func TestMetrics(t *testing.T) { require.Contains(t, metrics, buildMetric) } +func TestLogMetrics(t *testing.T) { + logMetrics := logmetrics.New("log_metrics_test") + logger := supportlog.New() + logger.AddHook(logMetrics) + + err := errors.Errorf("test-error") + logger.WithError(err).Error("test error 1") + logger.WithError(err).Error("test error 2") + + val := testutil.ToFloat64(logMetrics[logrus.ErrorLevel]) + assert.Equal(t, val, 2.0) +} + func getMetrics(test *Test) string { metricsURL, err := url.JoinPath(test.adminURL(), "/metrics") require.NoError(test.t, err)