diff --git a/api/server.go b/api/server.go index 0379c6af..8f239f1b 100644 --- a/api/server.go +++ b/api/server.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog/log" "github.com/sprintertech/sprinter-signing/api/handlers" "github.com/sprintertech/sprinter-signing/health" @@ -18,12 +19,16 @@ func Serve( unlockHandler *handlers.UnlockHandler, statusHandler *handlers.StatusHandler, confirmationsHandler *handlers.ConfirmationsHandler, + prometheusEnabled bool, ) { r := mux.NewRouter() r.HandleFunc("/v1/chains/{chainId:[0-9]+}/unlocks", unlockHandler.HandleUnlock).Methods("POST") r.HandleFunc("/v1/chains/{chainId:[0-9]+}/signatures", signingHandler.HandleSigning).Methods("POST") r.HandleFunc("/v1/chains/{chainId:[0-9]+}/signatures/{depositId}", statusHandler.HandleRequest).Methods("GET") r.HandleFunc("/v1/chains/{chainId:[0-9]+}/confirmations", confirmationsHandler.HandleRequest).Methods("GET") + if prometheusEnabled { + r.Handle("/metrics", promhttp.Handler()).Methods("GET") + } r.HandleFunc("/health", health.HealthHandler()).Methods("GET") server := &http.Server{ diff --git a/app/app.go b/app/app.go index 87a56a6e..de11bb29 100644 --- a/app/app.go +++ b/app/app.go @@ -62,6 +62,8 @@ import ( "github.com/sygmaprotocol/sygma-core/relayer/message" "github.com/sygmaprotocol/sygma-core/store" "github.com/sygmaprotocol/sygma-core/store/lvldb" + promexporter "go.opentelemetry.io/otel/exporters/prometheus" + sdkmetric "go.opentelemetry.io/otel/sdk/metric" ) var Version string @@ -112,7 +114,14 @@ func Run() error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - mp, err := observability.InitMetricProvider(context.Background(), configuration.RelayerConfig.OpenTelemetryCollectorURL) + var meterOpts []sdkmetric.Option + if configuration.RelayerConfig.PrometheusEnabled { + promExporter, err := promexporter.New() + panicOnError(err) + meterOpts = append(meterOpts, sdkmetric.WithReader(promExporter)) + } + + mp, err := observability.InitMetricProvider(context.Background(), configuration.RelayerConfig.OpenTelemetryCollectorURL, meterOpts...) panicOnError(err) defer func() { if err := mp.Shutdown(context.Background()); err != nil { @@ -430,7 +439,8 @@ func Run() error { signingHandler, unlockHandler, statusHandler, - confirmationsHandler) + confirmationsHandler, + configuration.RelayerConfig.PrometheusEnabled) sig := <-sysErr log.Info().Msgf("terminating got ` [%v] signal", sig) diff --git a/config/relayer/config.go b/config/relayer/config.go index 666b3a61..46458a0a 100644 --- a/config/relayer/config.go +++ b/config/relayer/config.go @@ -25,6 +25,7 @@ type RelayerConfig struct { CoinmarketcapConfig CoinmarketcapConfig SolverConfig SolverConfig ApiAddr string + PrometheusEnabled bool } type CoinmarketcapConfig struct { @@ -72,6 +73,7 @@ type RawRelayerConfig struct { CoinmarketcapConfig CoinmarketcapConfig `mapstructure:"CoinmarketcapConfig" json:"coinmarketcapConfig"` SolverConfig SolverConfig `mapstructure:"SolverConfig" json:"solverConfig"` ApiAddr string `mapstructure:"apiAddr" default:"0.0.0.0:3000"` + PrometheusEnabled bool `mapstructure:"PrometheusEnabled" json:"prometheusEnabled" default:"false"` } type RawMpcRelayerConfig struct { @@ -147,6 +149,7 @@ func NewRelayerConfig(rawConfig RawRelayerConfig) (RelayerConfig, error) { config.Id = rawConfig.Id config.ApiAddr = rawConfig.ApiAddr config.SolverConfig = rawConfig.SolverConfig + config.PrometheusEnabled = rawConfig.PrometheusEnabled return config, nil } diff --git a/go.mod b/go.mod index e22764dc..87aa23f9 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/stretchr/testify v1.10.0 github.com/sygmaprotocol/sygma-core v0.0.0-20250304150334-bd39ac4f7b82 go.opentelemetry.io/otel v1.16.0 + go.opentelemetry.io/otel/exporters/prometheus v0.39.0 go.opentelemetry.io/otel/metric v1.16.0 go.uber.org/mock v0.5.2 golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 @@ -190,7 +191,7 @@ require ( github.com/pelletier/go-toml v1.9.4 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.20.5 // indirect + github.com/prometheus/client_golang v1.20.5 github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.61.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect @@ -209,7 +210,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0 // indirect go.opentelemetry.io/otel/sdk v1.16.0 // indirect - go.opentelemetry.io/otel/sdk/metric v0.39.0 // indirect + go.opentelemetry.io/otel/sdk/metric v0.39.0 go.opentelemetry.io/otel/trace v1.16.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/go.sum b/go.sum index ddfbb5c2..fc74094a 100644 --- a/go.sum +++ b/go.sum @@ -1046,6 +1046,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 h1:f6BwB2OACc3FCbYVzn go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0/go.mod h1:UqL5mZ3qs6XYhDnZaW1Ps4upD+PX6LipH40AoeuIlwU= go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0 h1:IZXpCEtI7BbX01DRQEWTGDkvjMB6hEhiEZXS+eg2YqY= go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0/go.mod h1:xY111jIZtWb+pUUgT4UiiSonAaY2cD2Ts5zvuKLki3o= +go.opentelemetry.io/otel/exporters/prometheus v0.39.0 h1:whAaiHxOatgtKd+w0dOi//1KUxj3KoPINZdtDaDj3IA= +go.opentelemetry.io/otel/exporters/prometheus v0.39.0/go.mod h1:4jo5Q4CROlCpSPsXLhymi+LYrDXd2ObU5wbKayfZs7Y= go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo= go.opentelemetry.io/otel/metric v1.16.0/go.mod h1:QE47cpOmkwipPiefDwo2wDzwJrlfxxNYodqc4xnGCo4= go.opentelemetry.io/otel/sdk v1.16.0 h1:Z1Ok1YsijYL0CSJpHt4cS3wDDh7p572grzNrBMiMWgE=