Skip to content

Commit

Permalink
add Prometheus endpoint middleware; default register grpc prometheus …
Browse files Browse the repository at this point in the history
…interceptor
  • Loading branch information
lukasjarosch committed Jun 23, 2019
1 parent 62716b9 commit 40ef9a0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 46 deletions.
4 changes: 2 additions & 2 deletions a_main-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/middleware/frequency.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/go-kit/kit/metrics"
)

// RequestFrequencyMiddleware is an endpoint middleware which counts all failed and succeeded requests
func RequestFrequency(frequency metrics.Counter, methodName string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
Expand Down
45 changes: 45 additions & 0 deletions pkg/middleware/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package middleware

import (
"context"
"time"

"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"fmt"
)

var requestDuration = prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
Name: "grpc_request_duration_ms",
Help: "Request duration in milliseconds",
Buckets: []float64{50, 100, 250, 500, 1000},
}, []string{"method"})

var requestsCurrent = prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Name: "grpc_requests_current",
Help: "The current numer of gRPC requests by endpoint",
}, []string{"method"})

var requestStatus = prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Name: "grpc_requests_total",
Help: "The total number of gRPC requests and whether the business failed or not",
}, []string{"method", "success"})

// Prometheus adds basic RED metrics on all endpoints. The transport layer (gRPC) should also have metrics attached and
// will then take care of monitoring grpc endpoints including their status.
func Prometheus(methodName string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
requestsCurrent.With("method", methodName).Add(1)

defer func(begin time.Time) {
requestDuration.With("method", methodName).Observe(time.Since(begin).Seconds())
requestsCurrent.With("method", methodName).Add(-1)
requestStatus.With("method", methodName, "success", fmt.Sprint(err == nil))
}(time.Now())

return next(ctx, request)
}
}
}
Loading

0 comments on commit 40ef9a0

Please sign in to comment.