From c94175984b24fc562bc35170e2e13b7e8d17e7ab Mon Sep 17 00:00:00 2001 From: coder2z <2399158611@qq.com> Date: Mon, 19 Apr 2021 23:25:41 +0800 Subject: [PATCH] xmonitor --- xgrpc/server/interceptor.go | 14 +++++++------- xmonitor/metric.go | 13 +++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/xgrpc/server/interceptor.go b/xgrpc/server/interceptor.go index 1cfd0fa..38e8bc6 100644 --- a/xgrpc/server/interceptor.go +++ b/xgrpc/server/interceptor.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/coder2z/g-saber/xcast" "github.com/coder2z/g-saber/xlog" + "github.com/coder2z/g-server/xapp" "github.com/coder2z/g-server/xcode" "github.com/coder2z/g-server/xgrpc" "github.com/coder2z/g-server/xmonitor" @@ -57,10 +58,10 @@ func PrometheusUnaryServerInterceptor() grpc.UnaryServerInterceptor { startTime := time.Now() resp, err := handler(ctx, req) code := xcode.ExtractCodes(err) - xmonitor.ServerHandleHistogram.WithLabelValues(xmonitor.TypeGRPCUnary, info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info")).Observe(time.Since(startTime).Seconds()) - xmonitor.ServerHandleCounter.WithLabelValues(xmonitor.TypeGRPCUnary, info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info"), xcast.ToString(code.GetCode())).Inc() + xmonitor.ServerHandleHistogram.WithLabelValues(xmonitor.TypeGRPCUnary, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info")).Observe(time.Since(startTime).Seconds()) + xmonitor.ServerHandleCounter.WithLabelValues(xmonitor.TypeGRPCUnary, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info"), xcast.ToString(code.GetCode())).Inc() if code != xcode.OK { - xmonitor.ServerErrorCounter.WithLabelValues(xmonitor.TypeGRPCUnary, info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info"), xcast.ToString(code.GetCode())).Inc() + xmonitor.ServerErrorCounter.WithLabelValues(xmonitor.TypeGRPCUnary, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ctx, "info"), xcast.ToString(code.GetCode())).Inc() } return resp, err } @@ -71,10 +72,10 @@ func PrometheusStreamServerInterceptor() grpc.StreamServerInterceptor { startTime := time.Now() err := handler(srv, ss) code := xcode.ExtractCodes(err) - xmonitor.ServerHandleHistogram.WithLabelValues(xmonitor.TypeGRPCStream, info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info")).Observe(time.Since(startTime).Seconds()) - xmonitor.ServerHandleCounter.WithLabelValues(xmonitor.TypeGRPCStream, info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info"), xcast.ToString(code.GetCode())).Inc() + xmonitor.ServerHandleHistogram.WithLabelValues(xmonitor.TypeGRPCStream, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info")).Observe(time.Since(startTime).Seconds()) + xmonitor.ServerHandleCounter.WithLabelValues(xmonitor.TypeGRPCStream, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info"), xcast.ToString(code.GetCode())).Inc() if code != xcode.OK { - xmonitor.ServerErrorCounter.WithLabelValues(xmonitor.TypeGRPCUnary, info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info"), xcast.ToString(code.GetCode())).Inc() + xmonitor.ServerErrorCounter.WithLabelValues(xmonitor.TypeGRPCUnary, xapp.Name(), info.FullMethod, xgrpc.ExtractFromCtx(ss.Context(), "info"), xcast.ToString(code.GetCode())).Inc() } return err } @@ -101,7 +102,6 @@ func TraceUnaryServerInterceptor() grpc.UnaryServerInterceptor { } } - type contextedServerStream struct { grpc.ServerStream ctx context.Context diff --git a/xmonitor/metric.go b/xmonitor/metric.go index f5c315b..65a88dc 100644 --- a/xmonitor/metric.go +++ b/xmonitor/metric.go @@ -33,13 +33,13 @@ var ( var ( // ServerHandleCounter ... 指标: 服务类型,调用方法,客户端标识,返回的状态码 - ServerHandleCounter = NewCounterVec("server_handle_total", []string{"type", "method", "peer", "code"}) + ServerHandleCounter = NewCounterVec("server_handle_total", []string{"type", "name", "method", "peer", "code"}) - // ServerHandleCounter ... 指标: 服务类型,调用方法,客户端标识,返回的状态码 - ServerErrorCounter = NewCounterVec("server_error_total", []string{"type", "method", "peer", "code"}) + // ServerErrorCounter ... 指标: 服务类型,调用方法,客户端标识,返回的状态码 + ServerErrorCounter = NewCounterVec("server_error_total", []string{"type", "name", "method", "peer", "code"}) // ServerHandleHistogram ... - ServerHandleHistogram = NewHistogramVec("server_handle_seconds", []string{"type", "method", "peer"}) + ServerHandleHistogram = NewHistogramVec("server_handle_seconds", []string{"type", "name", "method", "peer"}) // ClientHandleCounter ... 指标: 客户端类型,客户端名称,调用方法,目标,返回的状态码 ClientHandleCounter = NewCounterVec("client_handle_total", []string{"type", "name", "method", "peer", "code"}) @@ -54,10 +54,11 @@ var ( JobHandleHistogram = NewHistogramVec("job_handle_seconds", []string{"type", "name"}) // LibHandleHistogram ... 指标: 类型,指令,address - LibHandleHistogram = NewHistogramVec("lib_handle_seconds", []string{"type", "method", "address"}) + LibHandleHistogram = NewHistogramVec("lib_handle_seconds", []string{"type", "name", "method", "address"}) // LibHandleCounter ... - LibHandleCounter = NewCounterVec("lib_handle_total", []string{"type", "method", "address", "code"}) + LibHandleCounter = NewCounterVec("lib_handle_total", []string{"type", "name", "method", "address", "code"}) + // LibHandleSummary LibHandleSummary = NewSummaryVec("lib_handle_stats", []string{"name", "status"})