From 705090c6035835d632bb5db952b2c6ce467d6f74 Mon Sep 17 00:00:00 2001 From: Andrew Davis <1709934+Savid@users.noreply.github.com> Date: Mon, 14 Nov 2022 15:36:15 +1000 Subject: [PATCH] feat(sentry): metrics --- cmd/sentry.go | 8 ++++++++ example_sentry.yaml | 3 +++ pkg/sentry/config.go | 3 +++ pkg/sentry/sentry.go | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/cmd/sentry.go b/cmd/sentry.go index 007ca9c5..0cfb4c28 100644 --- a/cmd/sentry.go +++ b/cmd/sentry.go @@ -5,6 +5,7 @@ import ( "github.com/creasty/defaults" "github.com/ethpandaops/xatu/pkg/sentry" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "gopkg.in/yaml.v2" ) @@ -31,6 +32,13 @@ var sentryCmd = &cobra.Command{ log.Info("Config loaded") + logLevel, err := logrus.ParseLevel(config.LoggingLevel) + if err != nil { + log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level") + } + + log.SetLevel(logLevel) + sentry, err := sentry.New(cmd.Context(), log, config) if err != nil { log.Fatal(err) diff --git a/example_sentry.yaml b/example_sentry.yaml index 63b502fe..3d5b717c 100644 --- a/example_sentry.yaml +++ b/example_sentry.yaml @@ -1,3 +1,6 @@ +logging: "debug" # panic,fatal,warm,info,debug,trace +metricsAddr: ":9090" + name: example-instance labels: diff --git a/pkg/sentry/config.go b/pkg/sentry/config.go index 2c1353e0..47a9f05d 100644 --- a/pkg/sentry/config.go +++ b/pkg/sentry/config.go @@ -9,6 +9,9 @@ import ( ) type Config struct { + LoggingLevel string `yaml:"logging" default:"info"` + MetricsAddr string `yaml:"metricsAddr" default:":9090"` + // The name of the sentry Name string `yaml:"name"` diff --git a/pkg/sentry/sentry.go b/pkg/sentry/sentry.go index 0d419bfa..5bd751d9 100644 --- a/pkg/sentry/sentry.go +++ b/pkg/sentry/sentry.go @@ -3,6 +3,7 @@ package sentry import ( "context" "errors" + "net/http" "os" "os/signal" "runtime" @@ -14,6 +15,7 @@ import ( "github.com/ethpandaops/xatu/pkg/sentry/ethereum" "github.com/ethpandaops/xatu/pkg/sentry/output" "github.com/go-co-op/gocron" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/sirupsen/logrus" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -59,6 +61,10 @@ func New(ctx context.Context, log logrus.FieldLogger, config *Config) (*Sentry, } func (s *Sentry) Start(ctx context.Context) error { + if err := s.ServeMetrics(ctx); err != nil { + return err + } + s.log.WithField("version", xatu.Full()).Info("Starting Xatu in sentry mode") s.beacon.OnReady(ctx, func(ctx context.Context) error { @@ -108,6 +114,25 @@ func (s *Sentry) Start(ctx context.Context) error { return nil } +func (s *Sentry) ServeMetrics(ctx context.Context) error { + go func() { + server := &http.Server{ + Addr: s.Config.MetricsAddr, + ReadHeaderTimeout: 15 * time.Second, + } + + server.Handler = promhttp.Handler() + + s.log.Infof("Serving metrics at %s", s.Config.MetricsAddr) + + if err := server.ListenAndServe(); err != nil { + s.log.Fatal(err) + } + }() + + return nil +} + func (s *Sentry) createNewClientMeta(ctx context.Context, topic xatu.ClientMeta_Event_Name) (*xatu.ClientMeta, error) { network, err := s.beacon.Metadata().NetworkName() if err != nil {