33 lines
709 B
Go
33 lines
709 B
Go
|
|
package metrics
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"google.golang.org/grpc"
|
||
|
|
"google.golang.org/grpc/status"
|
||
|
|
)
|
||
|
|
|
||
|
|
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
|
||
|
|
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
||
|
|
start := time.Now()
|
||
|
|
resp, err := handler(ctx, req)
|
||
|
|
elapsed := time.Since(start).Seconds()
|
||
|
|
|
||
|
|
code := "OK"
|
||
|
|
if err != nil {
|
||
|
|
if st, ok := status.FromError(err); ok {
|
||
|
|
code = strconv.Itoa(int(st.Code()))
|
||
|
|
} else {
|
||
|
|
code = "Unknown"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
RPCDurationSeconds.WithLabelValues(info.FullMethod).Observe(elapsed)
|
||
|
|
RPCRequestsTotal.WithLabelValues(info.FullMethod, code).Inc()
|
||
|
|
|
||
|
|
return resp, err
|
||
|
|
}
|
||
|
|
}
|