-
Notifications
You must be signed in to change notification settings - Fork 46
/
exporter.go
181 lines (156 loc) · 4.27 KB
/
exporter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package exporter
import (
"context"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
// Exporter handles serving the metrics
type Exporter struct {
addr string
endpoint *url.URL
fcgiEndpoint *url.URL
logger *zap.Logger
metricsEndpoint string
}
// OptionsFunc is a function passed to new for setting options on a new Exporter.
type OptionsFunc func(*Exporter) error
// New creates an exporter.
func New(options ...OptionsFunc) (*Exporter, error) {
e := &Exporter{
addr: ":9090",
}
for _, f := range options {
if err := f(e); err != nil {
return nil, errors.Wrap(err, "failed to set options")
}
}
if e.logger == nil {
l, err := NewLogger()
if err != nil {
return nil, errors.Wrap(err, "failed to create logger")
}
e.logger = l
}
if e.endpoint == nil && e.fcgiEndpoint == nil {
u, _ := url.Parse("http://localhost:9000/status")
e.endpoint = u
}
return e, nil
}
// SetLogger creates a function that will set the logger.
// Generally only used when create a new Exporter.
func SetLogger(l *zap.Logger) func(*Exporter) error {
return func(e *Exporter) error {
e.logger = l
return nil
}
}
// SetAddress creates a function that will set the listening address.
// Generally only used when create a new Exporter.
func SetAddress(addr string) func(*Exporter) error {
return func(e *Exporter) error {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return errors.Wrapf(err, "invalid address")
}
e.addr = net.JoinHostPort(host, port)
return nil
}
}
// SetEndpoint creates a function that will set the URL endpoint to contact
// php-fpm.
// Generally only used when create a new Exporter.
func SetEndpoint(rawurl string) func(*Exporter) error {
return func(e *Exporter) error {
if rawurl == "" {
return nil
}
u, err := url.Parse(rawurl)
if err != nil {
return errors.Wrap(err, "failed to parse url")
}
e.endpoint = u
return nil
}
}
// SetFastcgi creates a function that will set the fastcgi URL endpoint to contact
// php-fpm. If this is set, then fastcgi is used rather than HTTP.
// Generally only used when create a new Exporter.
func SetFastcgi(rawurl string) func(*Exporter) error {
return func(e *Exporter) error {
if rawurl == "" {
return nil
}
u, err := url.Parse(rawurl)
if err != nil {
return errors.Wrap(err, "failed to parse url")
}
e.fcgiEndpoint = u
return nil
}
}
// SetMetricsEndpoint sets the path under which to expose metrics.
// Generally only used when create a new Exporter.
func SetMetricsEndpoint(path string) func(*Exporter) error {
return func(e *Exporter) error {
if path == "" || path == "/" {
return nil
}
e.metricsEndpoint = path
return nil
}
}
var healthzOK = []byte("ok\n")
func (e *Exporter) healthz(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(healthzOK)
}
// Run starts the http server and collecting metrics. It generally does not return.
func (e *Exporter) Run() error {
c := e.newCollector()
if err := prometheus.Register(c); err != nil {
return errors.Wrap(err, "failed to register metrics")
}
prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), ""))
prometheus.Unregister(prometheus.NewGoCollector())
http.HandleFunc("/healthz", e.healthz)
http.Handle(e.metricsEndpoint, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>php-fpm exporter</title></head>
<body>
<h1>php-fpm exporter</h1>
<p><a href="` + e.metricsEndpoint + `">Metrics</a></p>
</body>
</html>`))
})
stopChan := make(chan os.Signal)
signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM)
srv := &http.Server{Addr: e.addr}
var g errgroup.Group
g.Go(func() error {
// TODO: allow TLS
return srv.ListenAndServe()
})
g.Go(func() error {
<-stopChan
// XXX: should shutdown time be configurable?
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
return nil
})
if err := g.Wait(); err != http.ErrServerClosed {
return errors.Wrap(err, "failed to run server")
}
return nil
}