Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs or examples for creating custom prometheus metrics #76

Closed
3 tasks done
starkers opened this issue May 18, 2022 · 3 comments
Closed
3 tasks done

Docs or examples for creating custom prometheus metrics #76

starkers opened this issue May 18, 2022 · 3 comments

Comments

@starkers
Copy link

starkers commented May 18, 2022

Issue Description

Hello, I'm just checking out the middleware (don't want to write my own)..

Typically I'm used to just using the vanilla prometheus promauto (EG promauto.NewCounter() to create custom metrics. However with this library and the examples it is not clear how this is done with this middleware.

I can see severally public functions but honestly I can't figure out how to just do this simple thing.

Is this even possible? If so, I'm happy to PR some docs to echo with an example if someone can explain to me how to access them. (thanks)

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

Actual behaviour

Steps to reproduce

Working code to debug

Version/commit

@thomasklein94
Copy link

Hi @starkers,

I've just ran into this yesterday. I was able to figure it out (I think), and pushed a PR to the documentation here.

@ankittiwari-harness
Copy link
Contributor

Hi @starkers I faced a similar issue and I was able to do it in a slightly different way. As the issue is still open. adding the approach.

I initialised and registered my metrics prior to attaching the middleware and was able to add custom metrics in the platform.

Borrowing from the added doc above, it works in below format too

import "github.com/prometheus/client_golang/prometheus"

type Metrics struct {
	customCnt *prometheus.Metric
	customDur *prometheus.Metric
}

func NewMetrics() *Metrics {
	       customCnt: &prometheus.Metric{
			Name:        "custom_total",
			Description: "Custom counter events.",
			Type:        "counter_vec",
			Args:        []string{"label_one", "label_two"},
		},
		customDur: &prometheus.Metric{
			Name:        "custom_duration_seconds",
			Description: "Custom duration observations.",
			Type:        "histogram_vec",
			Args:        []string{"label_one", "label_two"},
			Buckets:     prom.DefBuckets, // or your Buckets
		},
	prometheus.MustRegister(customCnt, customDur, dequeueCounter)

        return &Metrics{customCnt:  customCnt, customDur: customDur}


}

Make sure the above code is invoked before your initializing echo server

import 	"github.com/labstack/echo-contrib/prometheus"

func New(config *config.Config) *echo.Echo {

  e := echo.New()
  p := prometheus.NewPrometheus("echo", urlSkipperFunc)
  p.Use(e)
}

@aldas
Copy link
Contributor

aldas commented May 23, 2023

closing, new middleware was added with #94

custom metrics can be added now as follows

NB: this example creates custom registry but you could register these also with default prometheus registry

package main

import (
	"errors"
	"github.com/labstack/echo-contrib/echoprometheus"
	"github.com/labstack/echo/v4"
	"github.com/prometheus/client_golang/prometheus"
	"log"
	"net/http"
)

func main() {
	e := echo.New()

	customRegistry := prometheus.NewRegistry() // create custom registry for your custom metrics
	customCounter := prometheus.NewCounter(    // create new counter metric. This is replacement for `prometheus.Metric` struct
		prometheus.CounterOpts{
			Name: "custom_requests_total",
			Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
		},
	)
	if err := customRegistry.Register(customCounter); err != nil { // register your new counter metric with metrics registry
		log.Fatal(err)
	}

	e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
		AfterNext: func(c echo.Context, err error) {
			customCounter.Inc() // use our custom metric in middleware. after every request increment the counter
		},
		Registerer: customRegistry, // use our custom registry instead of default Prometheus registry
	}))
	// register route for getting gathered metrics data from our custom Registry
	e.GET("/metrics", echoprometheus.NewHandlerWithConfig(echoprometheus.HandlerConfig{Gatherer: customRegistry})) 

	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

@aldas aldas closed this as completed May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants