Skip to content

Commit

Permalink
feat(sentry): metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Savid committed Nov 14, 2022
1 parent de374af commit 705090c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions example_sentry.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
logging: "debug" # panic,fatal,warm,info,debug,trace
metricsAddr: ":9090"

name: example-instance

labels:
Expand Down
3 changes: 3 additions & 0 deletions pkg/sentry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
25 changes: 25 additions & 0 deletions pkg/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sentry
import (
"context"
"errors"
"net/http"
"os"
"os/signal"
"runtime"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 705090c

Please sign in to comment.