Skip to content

Commit 02cf4ae

Browse files
authored
[#77]: feat: status code in gRPC request metric
2 parents 8885f48 + 6e37229 commit 02cf4ae

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ github.com/roadrunner-server/goridge/v3 v3.6.2 h1:LH5HXfCygDp05KnOaXpa4fqVPWTsH7
4646
github.com/roadrunner-server/goridge/v3 v3.6.2/go.mod h1:3B95k/wM5GGAD0h2hZlJagS9PlTDGs5jh8MpZYC12vA=
4747
github.com/roadrunner-server/sdk/v4 v4.2.0 h1:hqNlqJV2MXZ8DF1wJnouUdV/55Hae6VL37fVXT1aIr8=
4848
github.com/roadrunner-server/sdk/v4 v4.2.0/go.mod h1:aIzXmg8DZBJ4Tbtvihp/s6VH4e2oSdivOqm/8V+HuUc=
49-
github.com/roadrunner-server/tcplisten v1.2.1 h1:9hVVMlCRvMPewnJCnfSe/kKAqn2ZOF3wHy+ji0M/NKU=
50-
github.com/roadrunner-server/tcplisten v1.2.1/go.mod h1:TRJLGwIruiJ7QhmGVRgJFY5Ch72mPoLhLAxuxLnavpU=
5149
github.com/roadrunner-server/tcplisten v1.3.0 h1:VDd6IbP8oIjm5vKvMVozeZgeHgOcoP0XYLOyOqcZHCY=
5250
github.com/roadrunner-server/tcplisten v1.3.0/go.mod h1:VR6Ob5am0oEuLMOeLiVvQxG9ShykAEgrlvZddX8EfoU=
5351
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=

plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (p *Plugin) Init(cfg common.Configurer, log common.Logger, server common.Se
9797
Namespace: namespace,
9898
Name: "request_total",
9999
Help: "Total number of handled GRPC requests after server restart.",
100-
}, []string{"grpc_method"})
100+
}, []string{"grpc_method", "grpc_code"})
101101

102102
p.requestDuration = prometheus.NewHistogramVec(
103103
prometheus.HistogramOpts{

server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import (
1515
"github.com/roadrunner-server/grpc/v4/proxy"
1616
"go.uber.org/zap"
1717
"google.golang.org/grpc"
18+
"google.golang.org/grpc/codes"
1819
"google.golang.org/grpc/credentials"
1920
"google.golang.org/grpc/keepalive"
21+
"google.golang.org/grpc/status"
2022
)
2123

2224
func (p *Plugin) createGRPCserver(interceptors map[string]common.Interceptor) (*grpc.Server, error) {
@@ -78,8 +80,16 @@ func (p *Plugin) interceptor(ctx context.Context, req any, info *grpc.UnaryServe
7880

7981
resp, err := handler(ctx, req)
8082

83+
s, ok := status.FromError(err)
84+
var statusCode codes.Code
85+
if ok {
86+
statusCode = s.Code()
87+
} else {
88+
statusCode = status.New(codes.Unknown, err.Error()).Code()
89+
}
90+
8191
defer func() {
82-
p.requestCounter.WithLabelValues(info.FullMethod).Inc()
92+
p.requestCounter.WithLabelValues(info.FullMethod, statusCode.String()).Inc()
8393
p.requestDuration.WithLabelValues(info.FullMethod).Observe(time.Since(start).Seconds())
8494
p.queueSize.Dec()
8595
}()

0 commit comments

Comments
 (0)