代码拉取完成,页面将自动刷新
package clientinterceptors
import (
"context"
"gitee.com/tylf2018/go-micro-framework/core/metric"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"strconv"
"time"
)
const serverNamespace = "rpc_client"
/*
两个基本指标: 1. 每个请求的耗时( histogram ) 2. 每个请求的状态计数器( counter )
/v1 状态码 有label 主要是状态码
*/
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 UnaryPrometheusInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
startTime := time.Now()
// 记录了耗时
err := invoker(ctx, method, req, reply, cc, opts...)
// 参数 v: 截至时间 以 毫秒为单位, labels: rpc的方法名作为标签
metricServerReqDur.Observe(int64(time.Since(startTime)/time.Millisecond), method)
// 记录状态码 和 访问次数
metricServerReqCodeTotal.Inc(method, strconv.Itoa(int(status.Code(err))))
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。