Skip to content

Commit

Permalink
xmonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2z committed Apr 19, 2021
1 parent 3e4125f commit c941759
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
14 changes: 7 additions & 7 deletions xgrpc/server/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -101,7 +102,6 @@ func TraceUnaryServerInterceptor() grpc.UnaryServerInterceptor {
}
}


type contextedServerStream struct {
grpc.ServerStream
ctx context.Context
Expand Down
13 changes: 7 additions & 6 deletions xmonitor/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand All @@ -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"})

Expand Down

0 comments on commit c941759

Please sign in to comment.