Skip to content

Commit

Permalink
Add Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ihippik committed Sep 16, 2022
1 parent bea1a03 commit d14c499
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cmd/wal-listener/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"fmt"
"net/http"
"runtime/debug"

"github.com/evalphobia/logrus_sentry"
"github.com/jackc/pgx"
"github.com/nats-io/nats.go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

Expand Down Expand Up @@ -53,6 +55,18 @@ func getConf(path string) (*config.Config, error) {
return &cfg, nil
}

func initMetrics(logger *logrus.Entry) {
const addr = ":2112"

logger.WithField("addr", addr).Infoln("metrics handler")

http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(addr, nil); err != nil {
logger.WithError(err).Errorln("init metrics handler")
return
}
}

// initLogger init logrus preferences.
func initLogger(cfg config.LoggerCfg, version string) *logrus.Entry {
logger := logrus.New()
Expand Down
2 changes: 2 additions & 0 deletions cmd/wal-listener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func main() {

initSentry(cfg.Monitoring.SentryDSN, logger)

go initMetrics(logger)

natsConn, err := nats.Connect(cfg.Nats.Address)
if err != nil {
return fmt.Errorf("nats connection: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ require (
github.com/jackc/pgx v3.6.2+incompatible
github.com/magiconair/properties v1.8.6
github.com/nats-io/nats.go v1.16.0
github.com/prometheus/client_golang v1.13.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.11.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nats-io/jwt v1.2.2 // indirect
github.com/nats-io/nats-server/v2 v2.1.2 // indirect
Expand All @@ -38,6 +43,9 @@ require (
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect
github.com/spf13/afero v1.9.2 // indirect
Expand All @@ -50,6 +58,7 @@ require (
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit d14c499

Please sign in to comment.