Skip to content

Commit

Permalink
Add hook for logging errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Feb 16, 2024
1 parent 10ae826 commit 1ccba2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/soroban-rpc/internal/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions cmd/soroban-rpc/internal/test/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit 1ccba2a

Please sign in to comment.