forked from bakins/php-fpm-exporter
/
exporter.go
136 lines (115 loc) · 3.09 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
package exporter
import (
"context"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
)
// Exporter handles serving the metrics
type Exporter struct {
addr string
endpoint *url.URL
logger *zap.Logger
}
// 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 {
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 {
u, err := url.Parse(rawurl)
if err != nil {
return errors.Wrap(err, "failed to parse url")
}
e.endpoint = u
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("/metrics", promhttp.Handler())
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
}