mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 01:31:17 +00:00
Bumped all packages that make the build fail:
gvt update github.com/golang/protobuf/proto
gvt fetch github.com/golang/protobuf/ptypes
gvt fetch google.golang.org/genproto/googleapis/rpc/status
gvt update google.golang.org/grpc/status
gvt update google.golang.org/grpc/transport
gvt update golang.org/x/net/http2
31 lines
909 B
Go
31 lines
909 B
Go
package middleware
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/weaveworks/common/httpgrpc"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// ServerInstrumentInterceptor instruments gRPC requests for errors and latency.
|
|
func ServerInstrumentInterceptor(hist *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)
|
|
duration := time.Since(begin).Seconds()
|
|
respStatus := "success"
|
|
if err != nil {
|
|
if errResp, ok := httpgrpc.HTTPResponseFromError(err); ok {
|
|
respStatus = strconv.Itoa(int(errResp.Code))
|
|
} else {
|
|
respStatus = "error"
|
|
}
|
|
}
|
|
hist.WithLabelValues(gRPC, info.FullMethod, respStatus, "false").Observe(duration)
|
|
return resp, err
|
|
}
|
|
}
|