Skip to content

Commit

Permalink
Merge pull request #32 from rijdendetreinen/prometheus-integration
Browse files Browse the repository at this point in the history
Prometheus integration
  • Loading branch information
geertw authored Jun 28, 2022
2 parents 1d79d43 + 45a96c0 commit 3916ff7
Show file tree
Hide file tree
Showing 6 changed files with 893 additions and 2 deletions.
33 changes: 33 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,44 @@ import (
"encoding/json"
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rijdendetreinen/gotrain/stores"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)

var (
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "gotrain",
Subsystem: "http",
Name: "duration",
Help: "Duration of HTTP requests.",
}, []string{"path"})
)

var (
httpReqs = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "gotrain",
Subsystem: "http",
Name: "requests",
Help: "HTTP requests",
}, []string{"path", "url"})
)

// prometheusMiddleware implements mux.MiddlewareFunc.
func prometheusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path))
next.ServeHTTP(w, r)
timer.ObserveDuration()
httpReqs.WithLabelValues(path, r.URL.Path).Add(1)
})
}

// ServeAPI serves the REST API on the given address
func ServeAPI(address string, exit chan bool) {
srv := &http.Server{Addr: address}
Expand All @@ -33,6 +65,7 @@ func ServeAPI(address string, exit chan bool) {
router.HandleFunc("/v2/services/stats", serviceCounters).Methods("GET")
router.HandleFunc("/v2/services/service/{id}/{date}", serviceDetails).Methods("GET")

router.Use(prometheusMiddleware)
srv.Handler = router

go listenAndServe(srv, exit)
Expand Down
6 changes: 6 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rijdendetreinen/gotrain/api"
"github.com/rijdendetreinen/gotrain/prometheus_interface"
"github.com/rijdendetreinen/gotrain/receiver"
"github.com/rijdendetreinen/gotrain/stores"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -66,6 +67,11 @@ func startServer(cmd *cobra.Command) {
apiAddress := viper.GetString("api.address")
go api.ServeAPI(apiAddress, exitRestAPI)

if viper.GetBool("prometheus.enabled") {
prometheus_interface.SetupPrometheus()
prometheus_interface.StartPrometheusInterface()
}

setupCleanupScheduler()
setupDowntimeDetector()
setupAutoSave()
Expand Down
3 changes: 3 additions & 0 deletions config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ archive:
address: :6379
password: ""
db: 0
prometheus:
enabled: true
address: :2121
#sentry:
# dsn: https://example@sentry.io/123456
# warnings: true
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ go 1.15

require (
github.com/beevik/etree v1.1.0
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/evalphobia/logrus_sentry v0.8.2
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/getsentry/sentry-go v0.13.0 // indirect
github.com/go-redis/redis v6.15.9+incompatible
github.com/gorilla/mux v1.7.4
github.com/gorilla/websocket v1.4.2 // indirect
github.com/pebbe/zmq4 v1.2.1
github.com/prometheus/client_golang v1.12.2
github.com/rickb777/date v1.13.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
github.com/spf13/viper v1.12.0
)
Loading

0 comments on commit 3916ff7

Please sign in to comment.