Skip to content

Commit

Permalink
feat: add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Apr 25, 2022
1 parent 1ceaafd commit e6b2b96
Show file tree
Hide file tree
Showing 3 changed files with 426 additions and 7 deletions.
19 changes: 18 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

"github.com/getsentry/sentry-go"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"

"github.com/packetframe/vertex/internal/config"
Expand All @@ -22,6 +25,13 @@ var (
sentryDsn = os.Getenv("SENTRY_DSN")
)

var (
metricRules = promauto.NewGauge(prometheus.GaugeOpts{
Name: "packetframe_vertex_rules",
Help: "Total vertex rules accounts",
})
)

// response returns a JSON response
func response(c *fiber.Ctx, status int, message string, data map[string]interface{}) error {
// Capitalize first letter
Expand Down Expand Up @@ -78,6 +88,8 @@ func main() {
log.Warnf("unable to retreive rules: %s", err)
}

metricRules.Set(float64(len(rules)))

for _, rule := range rules {
if time.Since(rule.CreatedAt) > rule.Expire {
log.Debugln("Found expired rule, deleting")
Expand Down Expand Up @@ -170,7 +182,12 @@ func main() {
return c.Status(http.StatusOK).SendString(cfg.String())
})

// go metrics.Listen(metricsListen)
// Start metrics server
go func() {
http.Handle("/metrics", promhttp.Handler())
log.Info("Starting metrics exporter on http://:8081/metrics")
log.Fatal(http.ListenAndServe(":8081", nil))
}()

startupMessage := fmt.Sprintf("Starting API %s on :8080", version)
sentry.CaptureMessage(startupMessage)
Expand Down
13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/getsentry/sentry-go v0.13.0
github.com/gofiber/fiber/v2 v2.32.0
github.com/jedib0t/go-pretty/v6 v6.3.1
github.com/sirupsen/logrus v1.4.2
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
gorm.io/driver/postgres v1.3.5
Expand All @@ -15,7 +16,10 @@ require (

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.0 // indirect
Expand All @@ -28,9 +32,13 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.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.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand All @@ -39,5 +47,6 @@ require (
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Loading

0 comments on commit e6b2b96

Please sign in to comment.