diff --git a/cmd/soroban-rpc/internal/daemon/metrics.go b/cmd/soroban-rpc/internal/daemon/metrics.go index c7c44484..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" @@ -16,13 +17,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 +39,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 { @@ -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 b807cb77..e32330b5 100644 --- a/cmd/soroban-rpc/internal/test/metrics_test.go +++ b/cmd/soroban-rpc/internal/test/metrics_test.go @@ -2,6 +2,9 @@ package test import ( "fmt" + io_prometheus_client "github.com/prometheus/client_model/go" + "github.com/stellar/go/support/errors" + "github.com/stretchr/testify/assert" "io" "net/http" "net/url" @@ -25,6 +28,24 @@ func TestMetrics(t *testing.T) { config.Version, ) require.Contains(t, metrics, buildMetric) + + logger := test.daemon.Logger() + err := errors.Errorf("test-error") + logger.WithError(err).Error("test error 1") + logger.WithError(err).Error("test error 2") + + 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 + } + } + assert.NotNil(t, metric) + val := metric.Metric[0].Counter.GetValue() + assert.GreaterOrEqual(t, val, 2.0) } func getMetrics(test *Test) string {