Skip to content

Commit 686cf89

Browse files
committed
fix(api): record client cancellations as 499 in metrics
Follow-up to 164f31a - applies the same client cancellation handling to the metrics middleware. Cancelled requests are now recorded with http.status_code=499 (nginx convention for client-closed-request) and a client.canceled=true attribute, preventing them from inflating the 5xx error rate.
1 parent b270ce9 commit 686cf89

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

packages/api/internal/middleware/otel/metrics/middleware.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package metrics
22

33
import (
4+
"context"
5+
"errors"
46
"fmt"
57
"strings"
68
"time"
@@ -71,7 +73,11 @@ func Middleware(meterProvider metric.MeterProvider, service string, options ...O
7173
reqAttributes...,
7274
)
7375

74-
if cfg.groupedStatus {
76+
if errors.Is(ctx.Err(), context.Canceled) {
77+
// 499 is the nginx convention for "client closed request before server responded"
78+
resAttributes = append(resAttributes, semconv.HTTPStatusCodeKey.Int(499))
79+
resAttributes = append(resAttributes, attribute.Bool("client.canceled", true))
80+
} else if cfg.groupedStatus {
7581
code := ginCtx.Writer.Status() / 100 * 100
7682
resAttributes = append(resAttributes, semconv.HTTPStatusCodeKey.Int(code))
7783
} else {

0 commit comments

Comments
 (0)