From 1ccba2a8b6759b8cb32035eb938bd4b4e666e5d2 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 10:07:54 -0500 Subject: [PATCH 1/5] 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) From 6a2c7b9649b7e94856f96e64db9c629e51f58e14 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 15:07:25 -0500 Subject: [PATCH 2/5] Check log metrics value in integration test --- cmd/soroban-rpc/internal/daemon/metrics.go | 5 +++ cmd/soroban-rpc/internal/test/metrics_test.go | 36 +++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cmd/soroban-rpc/internal/daemon/metrics.go b/cmd/soroban-rpc/internal/daemon/metrics.go index 0053b48d..9d4b01a0 100644 --- a/cmd/soroban-rpc/internal/daemon/metrics.go +++ b/cmd/soroban-rpc/internal/daemon/metrics.go @@ -2,6 +2,7 @@ package daemon import ( "context" + supportlog "github.com/stellar/go/support/log" "runtime" "time" @@ -102,3 +103,7 @@ func (c *CoreClientWithMetrics) SubmitTransaction(ctx context.Context, envelopeB func (d *Daemon) CoreClient() interfaces.CoreClient { return d.coreClient } + +func (d *Daemon) Logger() *supportlog.Entry { + return d.logger +} diff --git a/cmd/soroban-rpc/internal/test/metrics_test.go b/cmd/soroban-rpc/internal/test/metrics_test.go index 0f1a1562..225ed342 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -2,11 +2,8 @@ package test import ( "fmt" - "github.com/prometheus/client_golang/prometheus/testutil" - "github.com/sirupsen/logrus" + io_prometheus_client "github.com/prometheus/client_model/go" "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" @@ -31,21 +28,38 @@ func TestMetrics(t *testing.T) { config.Version, ) require.Contains(t, metrics, buildMetric) -} - -func TestLogMetrics(t *testing.T) { - logMetrics := logmetrics.New("log_metrics_test") - logger := supportlog.New() - logger.AddHook(logMetrics) + logger := test.daemon.Logger() 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]) + metricFamilies, err := test.daemon.MetricsRegistry().Gather() + assert.NoError(t, err) + var metric *io_prometheus_client.MetricFamily + for _, mf := range metricFamilies { + if *mf.Name == "soroban_rpc_log_error_total" { + metric = mf + break + } + } + val := metric.Metric[0].Counter.GetValue() assert.Equal(t, val, 2.0) } +//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) From 691cd37b3a7a541b7f984ba6e9465f154042376c Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 15:18:22 -0500 Subject: [PATCH 3/5] Update metrics_test.go --- cmd/soroban-rpc/internal/test/metrics_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/cmd/soroban-rpc/internal/test/metrics_test.go b/cmd/soroban-rpc/internal/test/metrics_test.go index 225ed342..0764123d 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -47,19 +47,6 @@ func TestMetrics(t *testing.T) { assert.Equal(t, val, 2.0) } -//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) From c5a272e86669a4ff84962aafae38a5d516c83fdd Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 16:22:39 -0500 Subject: [PATCH 4/5] Update metrics_test.go --- cmd/soroban-rpc/internal/test/metrics_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/soroban-rpc/internal/test/metrics_test.go b/cmd/soroban-rpc/internal/test/metrics_test.go index 0764123d..964de7c3 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -44,7 +44,7 @@ func TestMetrics(t *testing.T) { } } val := metric.Metric[0].Counter.GetValue() - assert.Equal(t, val, 2.0) + assert.GreaterOrEqual(t, val, 2.0) } func getMetrics(test *Test) string { From 4bd9ea1e77d669932efe15fb26768a5a7d54b074 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 16 Feb 2024 16:33:18 -0500 Subject: [PATCH 5/5] Update metrics_test.go --- cmd/soroban-rpc/internal/test/metrics_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/soroban-rpc/internal/test/metrics_test.go b/cmd/soroban-rpc/internal/test/metrics_test.go index 964de7c3..e32330b5 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -43,6 +43,7 @@ func TestMetrics(t *testing.T) { break } } + assert.NotNil(t, metric) val := metric.Metric[0].Counter.GetValue() assert.GreaterOrEqual(t, val, 2.0) }