This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcaddyprom.go
122 lines (105 loc) · 3.11 KB
/
caddyprom.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Package caddyprom implements a metrics module for Caddy v2 that exports in
// the Prometheus text format.
//
// The simplest use could be in a Caddyfile like:
//
// {
// order prometheus first
// }
// localhost
//
// prometheus
//
package caddyprom // import "github.com/hairyhenderson/caddyprom"
import (
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(Metrics{})
httpcaddyfile.RegisterHandlerDirective("prometheus", parseCaddyfile)
}
// Metrics -
type Metrics struct {
Addr string `json:"address,omitempty"`
Path string `json:"path,omitempty"`
useCaddyAddr bool
latencyBuckets []float64
sizeBuckets []float64
metricsHandler http.Handler
}
// CaddyModule returns the Caddy module information.
func (Metrics) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.prometheus",
New: func() caddy.Module { return new(Metrics) },
}
}
type zapLogger struct {
zl *zap.Logger
}
func (l *zapLogger) Println(v ...interface{}) {
l.zl.Sugar().Error(v...)
}
// Provision -
func (m *Metrics) Provision(ctx caddy.Context) error {
log := ctx.Logger(m)
m.metricsHandler = promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
ErrorHandling: promhttp.HTTPErrorOnError,
ErrorLog: &zapLogger{log},
})
return m.initMetrics(ctx)
}
// UnmarshalCaddyfile -
func (m *Metrics) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
setInline := d.Args(&m.Addr) // setInline is true if the address was set inline
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "address": // optional: m.Addr has a default value
if !setInline {
d.Args(&m.Addr)
} else {
return d.Errf("listen address has already been set")
}
case "path": // optional: m.Path has a default value
d.Args(&m.Path)
default:
return d.Errf("unrecognized subdirective '%s'", d.Val())
}
}
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Metrics
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// ServeHTTP - instrument the handler
// fulfils the caddyhttp.MiddlewareHandler interface
func (m Metrics) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) (err error) {
// TODO: break this out to record the rest of the metrics
chain :=
promhttp.InstrumentHandlerCounter(requestCount,
promhttp.InstrumentHandlerDuration(requestDuration,
promhttp.InstrumentHandlerResponseSize(responseSize, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = next.ServeHTTP(w, r)
})),
),
)
chain.ServeHTTP(w, r)
return err
}
// Interface guards
var (
_ caddy.Provisioner = (*Metrics)(nil)
_ caddyhttp.MiddlewareHandler = (*Metrics)(nil)
_ caddyfile.Unmarshaler = (*Metrics)(nil)
)