Skip to content

Commit

Permalink
ft(prometheus): monitoring middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantprateek committed Jul 26, 2023
1 parent 6ae5f51 commit 1963bc0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 476 deletions.
73 changes: 48 additions & 25 deletions api-gateway/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,74 @@ package handlers
import (
"log"
"strings"
"time"

v1Handlers "landate/api-gateway/handlers/versions"
"landate/api-gateway/middlewares"
route "landate/api-gateway/routes"
config "landate/config"

"github.com/ansrivas/fiberprometheus/v2"
// "github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "http_request_duration_seconds_2",
Help: "The total number of processed events",
})
)

func recordMetrics() {
go func() {
for {
opsProcessed.Inc()
time.Sleep(2 * time.Second)
}
}()
}

func Gateway() {

router := fiber.New()

// API Authorization Middleware
router.Use(middlewares.ValidateAPIKey)

promefiber := fiberprometheus.New("http_request_duration_seconds")
promefiber.RegisterAt(router, "/metrics")
router.Use(promefiber.Middleware)
// Previous Feature
// promefiber := fiberprometheus.New("http_request_duration_seconds")
// promefiber.RegisterAt(router, "/metrics")
// router.Use(promefiber.Middleware)

// Prometheus Configuration
// prouter := fiber.New()
// requestDuration := prometheus.NewHistogramVec(
// prometheus.HistogramOpts{
// Name: "http_request_duration_seconds",
// Help: "Histogram of the request duration.",
// Buckets: prometheus.DefBuckets,
// },
// []string{"method", "route", "status"},
// )
// prometheus.MustRegister(requestDuration)
// prouter.Use(func(c *fiber.Ctx) error {
// start := time.Now()
// err := c.Next()
// duration := time.Since(start).Seconds()
// requestDuration.WithLabelValues(c.Method(), c.Path(), string(c.Response().StatusCode())).Observe(duration)
// return err
// })
// prouter.Get("/metrics", adaptor.HTTPHandler(promhttp.Handler()))
// go func() {
// log.Fatal(prouter.Listen(":2222"))
// }()
prouter := fiber.New()
requestDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Histogram of the request duration.",
Buckets: prometheus.DefBuckets,
},
[]string{"method", "route", "status"},
)
prometheus.MustRegister(requestDuration)
prouter.Use(func(c *fiber.Ctx) error {
start := time.Now()
err := c.Next()
duration := time.Since(start).Seconds()
requestDuration.WithLabelValues(c.Method(), c.Path(), string(c.Response().StatusCode())).Observe(duration)
return err
})
recordMetrics()
prouter.Get("/metrics", adaptor.HTTPHandler(promhttp.Handler()))
go func() {
log.Fatal(prouter.Listen(":2222"))
}()
// ========== END of PROMETHEUS Middleware ==========

router.Use(logger.New(logger.Config{
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module landate
go 1.20

require (
github.com/ansrivas/fiberprometheus/v2 v2.6.0
github.com/aws/aws-sdk-go v1.44.280
github.com/gofiber/fiber/v2 v2.48.0
github.com/hashicorp/consul/api v1.20.0
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.16.0
github.com/rabbitmq/amqp091-go v1.8.1
go.mongodb.org/mongo-driver v1.11.6
golang.org/x/net v0.10.0
Expand All @@ -21,9 +21,9 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/gofiber/adaptor/v2 v2.2.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v0.12.0 // indirect
Expand All @@ -46,7 +46,6 @@ require (
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
Expand Down
Loading

0 comments on commit 1963bc0

Please sign in to comment.