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 4 commits
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
19 changes: 12 additions & 7 deletions cmd/soroban-rpc/internal/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon

import (
"context"
supportlog "github.com/stellar/go/support/log"
"runtime"
"time"

Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
20 changes: 20 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,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"
Expand All @@ -25,6 +28,23 @@ 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
}
}
val := metric.Metric[0].Counter.GetValue()
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
assert.GreaterOrEqual(t, val, 2.0)
}

func getMetrics(test *Test) string {
Expand Down
Loading