You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
4 years ago
|
package serverinterceptors
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/tal-tech/go-zero/core/metric"
|
||
|
"github.com/tal-tech/go-zero/core/timex"
|
||
4 years ago
|
"google.golang.org/grpc"
|
||
|
"google.golang.org/grpc/status"
|
||
|
)
|
||
|
|
||
|
const serverNamespace = "rpc_server"
|
||
|
|
||
|
var (
|
||
|
metricServerReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
|
||
|
Namespace: serverNamespace,
|
||
|
Subsystem: "requests",
|
||
|
Name: "duration_ms",
|
||
|
Help: "rpc server requests duration(ms).",
|
||
|
Labels: []string{"method"},
|
||
|
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000},
|
||
|
})
|
||
|
|
||
|
metricServerReqCodeTotal = metric.NewCounterVec(&metric.CounterVecOpts{
|
||
|
Namespace: serverNamespace,
|
||
|
Subsystem: "requests",
|
||
|
Name: "code_total",
|
||
|
Help: "rpc server requests code count.",
|
||
|
Labels: []string{"method", "code"},
|
||
|
})
|
||
|
)
|
||
|
|
||
|
func UnaryPromMetricInterceptor() grpc.UnaryServerInterceptor {
|
||
4 years ago
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (
|
||
|
interface{}, error) {
|
||
4 years ago
|
startTime := timex.Now()
|
||
|
resp, err := handler(ctx, req)
|
||
|
metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), info.FullMethod)
|
||
|
metricServerReqCodeTotal.Inc(info.FullMethod, strconv.Itoa(int(status.Code(err))))
|
||
|
return resp, err
|
||
|
}
|
||
|
}
|