-
Notifications
You must be signed in to change notification settings - Fork 10
/
exporter.go
130 lines (109 loc) · 3.04 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
package exporter
import (
"context"
"net"
"net/http"
"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
gearmanAddr string
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: "127.0.0.1:9418",
gearmanAddr: "127.0.0.1:4730",
}
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
}
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
}
}
// SetGearmanAddress creates a function that will set the address to contact gearman.
// Generally only used when create a new Exporter.
func SetGearmanAddress(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.gearmanAddr = net.JoinHostPort(host, port)
return nil
}
}
var healthzOK = []byte("ok\n")
func (e *Exporter) healthz(w http.ResponseWriter, r *http.Request) {
// TODO: check if we can contact gearman?
_, _ = w.Write(healthzOK)
}
// Run starts the http server and collecting metrics. It generally does not return.
func (e *Exporter) Run() error {
c := e.newCollector(newGearman(e.gearmanAddr))
if err := prometheus.Register(c); err != nil {
return errors.Wrap(err, "failed to register metrics")
}
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
}