package handlers

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

var (
	RedirectHandlerRedirectCountMetric = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "router_redirect_handler_redirect_total",
			Help: "Number of redirects handled by router redirect handlers",
		},
		[]string{
			"redirect_code",
			"redirect_type",
			"redirect_url",
		},
	)

	BackendHandlerRequestCountMetric = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "router_backend_handler_request_total",
			Help: "Number of requests handled by router backend handlers",
		},
		[]string{
			"backend_id",
			"request_method",
		},
	)

	BackendHandlerResponseDurationSecondsMetric = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name: "router_backend_handler_response_duration_seconds",
			Help: "Histogram of response durations by router backend handlers",
			Buckets: prometheus.ExponentialBuckets(
				0.25, 2, 6, // This buckets [...0.25  0.5  1  2  4  8...]
			),
		},
		[]string{
			"backend_id",
			"request_method",
			"response_code",
		},
	)
)

func initMetrics() {
	prometheus.MustRegister(RedirectHandlerRedirectCountMetric)

	prometheus.MustRegister(BackendHandlerRequestCountMetric)
	prometheus.MustRegister(BackendHandlerResponseDurationSecondsMetric)
}

package handlers

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

var (
	RedirectHandlerRedirectCountMetric = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "router_redirect_handler_redirect_total",
			Help: "Number of redirects handled by router redirect handlers",
		},
		[]string{
			"redirect_code",
			"redirect_type",
			"redirect_url",
		},
	)

	BackendHandlerRequestCountMetric = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "router_backend_handler_request_total",
			Help: "Number of requests handled by router backend handlers",
		},
		[]string{
			"backend_id",
			"request_method",
		},
	)

	BackendHandlerResponseDurationSecondsMetric = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name: "router_backend_handler_response_duration_seconds",
			Help: "Histogram of response durations by router backend handlers",
			Buckets: prometheus.ExponentialBuckets(
				0.0625, 2, 7, // This buckets [...0.0625  0.125  0.25  0.5  1  2  4...]
			),
		},
		[]string{
			"backend_id",
			"request_method",
			"response_code",
		},
	)
)

func initMetrics() {
	prometheus.MustRegister(RedirectHandlerRedirectCountMetric)

	prometheus.MustRegister(BackendHandlerRequestCountMetric)
	prometheus.MustRegister(BackendHandlerResponseDurationSecondsMetric)
}

Adjust histogram buckets for faster responses
Adjust histogram buckets for faster responses

At the moment, router's metrics bucket response times into:

* Less than 250ms
* 250ms - 500ms
* 500ms - 1s
* 1s - 2s
* 2s - 4s
* 4s - 8s
* More than 8s

From looking at our metrics, the majority of our responses complete in
less than 250ms, so this is not a particularly useful distribution.

By starting the exponential buckets at 1/16 of a second instead of 1/4
we should get a much more useful view of the latency distribution.

I've added one more bucket, so we can still see the 2-4 second range.
Responses that take more than 4 seconds should just be considered as
"very slow", so losing the "more than 8 seconds" bucket isn't really a
problem.
