Skip to content

Commit 492d856

Browse files
authored
otelgrpc: stats handlers record durations in ms instead of ns (#4548)
1 parent d9e86fb commit 492d856

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616

1717
- Fix `StreamClientInterceptor` in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` to end the spans synchronously. (#4537)
1818
- Fix data race in stats handlers when processing messages received and sent metrics in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#4577)
19+
- The stats handlers `NewClientHandler`, `NewServerHandler` in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` now records RPC durations in `ms` instead of `ns`. (#4548)
1920

2021
## [1.21.0/0.46.0/0.15.0/0.1.0] - 2023-11-10
2122

instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,11 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
340340
grpcStatusCodeAttr := statusCodeAttr(s.Code())
341341
span.SetAttributes(grpcStatusCodeAttr)
342342

343-
elapsedTime := time.Since(before).Milliseconds()
343+
// Use floating point division here for higher precision (instead of Millisecond method).
344+
elapsedTime := float64(time.Since(before)) / float64(time.Millisecond)
345+
344346
metricAttrs = append(metricAttrs, grpcStatusCodeAttr)
345-
cfg.rpcDuration.Record(ctx, float64(elapsedTime), metric.WithAttributes(metricAttrs...))
347+
cfg.rpcDuration.Record(ctx, elapsedTime, metric.WithAttributes(metricAttrs...))
346348

347349
return resp, err
348350
}

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,13 @@ func (c *config) handleRPC(ctx context.Context, rs stats.RPCStats) {
194194
span.End()
195195

196196
metricAttrs = append(metricAttrs, rpcStatusAttr)
197-
c.rpcDuration.Record(wctx, float64(rs.EndTime.Sub(rs.BeginTime)), metric.WithAttributes(metricAttrs...))
197+
198+
// Use floating point division here for higher precision (instead of Millisecond method).
199+
elapsedTime := float64(rs.EndTime.Sub(rs.BeginTime)) / float64(time.Millisecond)
200+
201+
c.rpcDuration.Record(wctx, elapsedTime, metric.WithAttributes(metricAttrs...))
198202
c.rpcRequestsPerRPC.Record(wctx, atomic.LoadInt64(&gctx.messagesReceived), metric.WithAttributes(metricAttrs...))
199203
c.rpcResponsesPerRPC.Record(wctx, atomic.LoadInt64(&gctx.messagesSent), metric.WithAttributes(metricAttrs...))
200-
201204
default:
202205
return
203206
}

0 commit comments

Comments
 (0)