Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus hook to count different log levels #67

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
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)
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
}

func getMetrics(test *Test) string {
metricsURL, err := url.JoinPath(test.adminURL(), "/metrics")
require.NoError(test.t, err)
Expand Down
Loading