mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 01:31:17 +00:00
24 lines
700 B
Go
24 lines
700 B
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// ServerInstrumentInterceptor instruments gRPC requests for errors and latency.
|
|
func ServerInstrumentInterceptor(duration *prometheus.HistogramVec) grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
begin := time.Now()
|
|
resp, err := handler(ctx, req)
|
|
status := "success"
|
|
if err != nil {
|
|
status = "error"
|
|
}
|
|
duration.WithLabelValues(gRPC, info.FullMethod, status, "false").Observe(time.Since(begin).Seconds())
|
|
return resp, err
|
|
}
|
|
}
|