fiberprometheus

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2023 License: MIT Imports: 8 Imported by: 0

README

fiberprometheus

Prometheus middleware for gofiber.

Release Discord Test Security Linter

Following metrics are available by default:

http_requests_total
http_request_duration_seconds
http_requests_in_progress_total
Install v2
go get -u github.com/gofiber/fiber/v2
go get -u github.com/arielsrv/fiberprometheus/v2
Example using v2
package main

import (
	"github.com/arielsrv/fiberprometheus/v2"
	"github.com/gofiber/fiber/v2"
)

func main() {
  app := fiber.New()

  // This here will appear as a label, one can also use
  // fiberprometheus.NewWith(servicename, namespace, subsystem )
  // or
  // NOTE: Following is not available in v1
  // labels := map[string]string{"custom_label1":"custom_value1", "custom_label2":"custom_value2"}
  // fiberprometheus.NewWithLabels(labels, namespace, subsystem )
  prometheus := fiberprometheus.New("my-service-name")
  prometheus.RegisterAt(app, "/metrics")
  app.Use(prometheus.Middleware)

  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello World")
  })

  app.Post("/some", func(c *fiber.Ctx) error {
    return c.SendString("Welcome!")
  })

  app.Listen(":3000")
}
Example using V1
package main

import (
  "github.com/gofiber/fiber"
  "github.com/ansrivas/fiberprometheus"
)

func main() {
  app := fiber.New()

  // This here will appear as a label, one can also use
  // fiberprometheus.NewWith(servicename, namespace, subsystem )
  prometheus := fiberprometheus.New("my-service-name")
  prometheus.RegisterAt(app, "/metrics")
  app.Use(prometheus.Middleware)

  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello World")
  })

  app.Post("/some", func(c *fiber.Ctx) {
    c.Send("Welcome!")
  })

  app.Listen(3000)
}
Result
Grafana Board

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FiberPrometheus

type FiberPrometheus struct {
	// contains filtered or unexported fields
}

FiberPrometheus ...

func New

func New(serviceName string) *FiberPrometheus

New creates a new instance of FiberPrometheus middleware serviceName is available as a const label.

func NewWith

func NewWith(serviceName, namespace, subsystem string) *FiberPrometheus

NewWith creates a new instance of FiberPrometheus middleware but with an ability to pass namespace and a custom subsystem Here serviceName is created as a constant-label for the metrics Namespace, subsystem get prefixed to the metrics.

For e.g. namespace = "my_app", subsystem = "http" then metrics would be `my_app_http_requests_total{...,service= "serviceName"}`.

func NewWithLabels

func NewWithLabels(labels map[string]string, namespace, subsystem string) *FiberPrometheus

NewWithLabels creates a new instance of FiberPrometheus middleware but with an ability to pass namespace and a custom subsystem Here labels are created as a constant-labels for the metrics Namespace, subsystem get prefixed to the metrics.

For e.g. namespace = "my_app", subsystem = "http" and labels = map[string]string{"key1": "value1", "key2":"value2"} then metrics would become `my_app_http_requests_total{...,key1= "value1", key2= "value2" }`.

func NewWithRegistry

func NewWithRegistry(registry prometheus.Registerer, serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus

NewWithRegistry creates a new instance of FiberPrometheus middleware but with an ability to pass a custom registry, serviceName, namespace, subsystem and labels Here labels are created as a constant-labels for the metrics Namespace, subsystem get prefixed to the metrics.

For e.g. namespace = "my_app", subsystem = "http" and labels = map[string]string{"key1": "value1", "key2":"value2"} then metrics would become `my_app_http_requests_total{...,key1= "value1", key2= "value2" }`.

func (*FiberPrometheus) Middleware

func (ps *FiberPrometheus) Middleware(ctx *fiber.Ctx) error

Middleware is the actual default middleware implementation.

func (*FiberPrometheus) RegisterAt

func (ps *FiberPrometheus) RegisterAt(app fiber.Router, url string, handlers ...fiber.Handler)

RegisterAt will register the prometheus handler at a given URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL