/
handler.go
68 lines (58 loc) · 1.35 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package prometheus
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/ory/herodot"
)
const (
MetricsPrometheusPath = "/metrics/prometheus"
)
// Handler handles HTTP requests to health and version endpoints.
type Handler struct {
H herodot.Writer
VersionString string
}
// NewHandler instantiates a handler.
func NewHandler(
h herodot.Writer,
version string,
) *Handler {
return &Handler{
H: h,
VersionString: version,
}
}
type router interface {
GET(path string, handle httprouter.Handle)
}
// SetRoutes registers this handler's routes.
func (h *Handler) SetRoutes(r router) {
r.GET(MetricsPrometheusPath, h.Metrics)
}
// Metrics outputs prometheus metrics
//
// swagger:route GET /metrics/prometheus metadata prometheus
//
// Get snapshot metrics from the service. If you're using k8s, you can then add annotations to
// your deployment like so:
//
// ```
// metadata:
//
// annotations:
// prometheus.io/port: "4434"
// prometheus.io/path: "/metrics/prometheus"
//
// ```
//
// Produces:
// - plain/text
//
// Responses:
// 200: emptyResponse
func (h *Handler) Metrics(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
promhttp.Handler().ServeHTTP(rw, r)
}