1 Star 0 Fork 0

天雨流芳 / go-micro-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
prometheusinterceptor.go 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
天雨流芳 提交于 2024-04-02 17:03 . 链路追踪逻辑修改
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
}
1
https://gitee.com/tylf2018/go-micro-framework.git
git@gitee.com:tylf2018/go-micro-framework.git
tylf2018
go-micro-framework
go-micro-framework
a23f37e8bd2b

搜索帮助