-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 2.67 KB
/
main.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
package main
import (
"log"
"net"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/knl/pulley/internal/config"
"github.com/knl/pulley/internal/metrics"
"github.com/knl/pulley/internal/service"
"github.com/knl/pulley/internal/version"
)
func newWebhookHandler(pulley *service.Pulley) http.Handler {
// instrument the hook handler, so we could track how well we respond
inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "webhook_in_flight_requests",
Help: "A gauge of requests currently being served by the webhook handler.",
})
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "webhook_requests_total",
Help: "A counter for requests to the webhook handler.",
},
[]string{"code", "method"},
)
// duration is partitioned by the HTTP method and handler. It uses custom
// buckets based on the expected request duration.
duration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "webhook_request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: []float64{.05, .1, .25, 1, 2.5, 10},
},
[]string{"method"},
)
// requestSize has no labels, making it a zero-dimensional
// ObserverVec.
requestSize := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "webhook_request_size_bytes",
Help: "A histogram of request sizes.",
Buckets: []float64{1024, 2048, 4096, 16 * 1024, 64 * 1024},
},
[]string{},
)
// Register all of the metrics in the standard registry.
prometheus.MustRegister(inFlightGauge, counter, duration, requestSize)
// Instrument the handlers with all the metrics, injecting the "handler"
// label by currying.
return promhttp.InstrumentHandlerInFlight(inFlightGauge,
promhttp.InstrumentHandlerDuration(duration,
promhttp.InstrumentHandlerCounter(counter,
promhttp.InstrumentHandlerRequestSize(requestSize, pulley.HookHandler()),
),
),
)
}
func main() {
log.Println("server started")
log.Println(version.Print())
config, err := config.Setup()
if err != nil {
log.Fatal("Configuration step failed", err)
}
log.Println(config.Print())
pulley := service.Pulley{
Updates: make(chan interface{}, 100),
Metrics: metrics.NewGithubMetrics(),
Token: config.WebhookToken,
}
pulley.MetricsProcessor(config.DefaultContextChecker(), config.TrackBuildTimes)
http.Handle("/"+config.WebhookPath, newWebhookHandler(&pulley))
http.Handle("/"+config.MetricsPath, promhttp.Handler())
// Listen & Serve
addr := net.JoinHostPort(config.Host, config.Port)
log.Printf("[service] listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}