forked from hairyhenderson/caddyprom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
77 lines (65 loc) · 1.8 KB
/
setup.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
package caddyprom
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
const (
defaultPath = "/metrics"
defaultAddr = "0.0.0.0:9180"
)
var (
requestDuration *prometheus.SummaryVec
responseSize *prometheus.SummaryVec
)
func (m *Metrics) initMetrics(ctx caddy.Context) error {
log := ctx.Logger(m)
m.registerMetrics("caddy", "http")
if m.Path == "" {
m.Path = defaultPath
}
if m.Addr == "" {
m.Addr = defaultAddr
}
if !m.useCaddyAddr {
mux := http.NewServeMux()
mux.Handle(m.Path, m.metricsHandler)
srv := &http.Server{Handler: mux}
// if m.Addr does not have a port just add the default one
if !strings.Contains(m.Addr, ":") {
m.Addr += ":" + strings.Split(defaultAddr, ":")[1]
}
zap.S().Info("Binding prometheus exporter to %s", m.Addr)
listener, err := net.Listen("tcp", m.Addr)
if err != nil {
return fmt.Errorf("failed to listen to %s: %w", m.Addr, err)
}
go func() {
err := srv.Serve(listener)
if err != nil && err != http.ErrServerClosed {
log.Error("metrics handler's server failed to serve", zap.Error(err))
}
}()
}
return nil
}
func (m *Metrics) registerMetrics(namespace, subsystem string) {
httpLabels := []string{"code", "method", "path"}
requestDuration = promauto.NewSummaryVec(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "Histogram of the time (in seconds) each request took.",
}, httpLabels)
responseSize = promauto.NewSummaryVec(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "response_size_bytes",
Help: "Size of the returns response in bytes.",
}, httpLabels)
}