Skip to content

Commit

Permalink
Merge branch 'main' into add-spec-tools-crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic authored Feb 20, 2024
2 parents 441232e + 448d9ad commit 199bf42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
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
}
21 changes: 21 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,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 {
Expand Down

0 comments on commit 199bf42

Please sign in to comment.