-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (53 loc) · 1.98 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
//go:generate go-extpoints
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/utilitywarehouse/go-operational/op"
"github.com/utilitywarehouse/json-rpc-proxy/extpoints"
_ "github.com/utilitywarehouse/json-rpc-proxy/handlers"
)
var endpoints = extpoints.Endpoints
var gitHash string
func main() {
oph := op.NewHandler(op.NewStatus(
"bill-rpc-handler",
"Handles outbound flat JSON HTTP calls from Bill, and passes the real work on to a subsequent service").
AddOwner("telecoms team", "#telecom").
AddLink("github", "https://github.com/utilitywarehouse/uw-bill-rpc-handler").
SetRevision(gitHash).
ReadyAlways(),
)
router := mux.NewRouter()
router.NewRoute().PathPrefix("/__/").Handler(oph)
endpointProviders := endpoints.All()
log.Printf("handlers: %+v", endpointProviders)
histoOpts := prometheus.HistogramOpts{
Name: "bill_rpc_handler_downstream_latencies",
Help: "A labeled histogram of the downstream service latencies",
}
latenciesHisto := prometheus.NewHistogramVec(histoOpts, []string{"route"})
prometheus.DefaultRegisterer.MustRegister(latenciesHisto)
for route, ep := range endpointProviders {
log.Printf("registered handler for route: %s", route)
router.HandleFunc("/"+route, createInstrumentedHandler(route, ep(), latenciesHisto))
}
router.Handle("/metrics", promhttp.Handler())
log.Printf("router: %#v", router)
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
log.Fatal(http.ListenAndServe(":8000", loggedRouter))
}
func createInstrumentedHandler(route string, provider http.HandlerFunc, histo *prometheus.HistogramVec) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
now := time.Now()
provider.ServeHTTP(w, req)
seconds := float64(time.Since(now).Seconds())
histo.WithLabelValues(route).Observe(seconds)
}
}