Skip to content

Commit

Permalink
add metric rpc.server.duration for stream request
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Aug 31, 2022
1 parent 20d3d5e commit e65f08c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
requestStartTime := time.Now()
ctx := ss.Context()
i := &InterceptorInfo{
StreamServerInfo: info,
Expand Down Expand Up @@ -458,14 +459,24 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
)
defer span.End()

err := handler(srv, wrapServerStream(ctx, ss))
meter := cfg.MeterProvider.Meter(
instrumentationName,
metric.WithInstrumentationVersion(SemVersion()),
)
rpcServerDuration, _ := meter.SyncFloat64().Histogram("rpc.server.duration")

err := handler(srv, wrapServerStream(ctx, ss))
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
attr = append(attr, semconv.RPCGRPCStatusCodeKey.Int64(int64(s.Code())))
rpcServerDuration.Record(ctx, elapsedTime, attr...)
} else {
span.SetAttributes(statusCodeAttr(grpc_codes.OK))
attr = append(attr, semconv.RPCGRPCStatusCodeOk)
rpcServerDuration.Record(ctx, elapsedTime, attr...)
}

return err
Expand Down

0 comments on commit e65f08c

Please sign in to comment.