Skip to content

Commit de52e0f

Browse files
committed
fix(api): always report grouped and granular status codes in metrics
1 parent 686cf89 commit de52e0f

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type config struct {
1111
recordInFlight bool
1212
recordSize bool
1313
recordDuration bool
14-
groupedStatus bool
1514
recorder Recorder
1615
attributes func(serverName, route string, request *http.Request) []attribute.KeyValue
1716
shouldRecord func(serverName, route string, request *http.Request) bool
@@ -22,7 +21,6 @@ func defaultConfig() *config {
2221
recordInFlight: true,
2322
recordDuration: true,
2423
recordSize: true,
25-
groupedStatus: true,
2624
attributes: DefaultAttributes,
2725
shouldRecord: func(_, _ string, _ *http.Request) bool {
2826
return true

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const MetricPrefix = "metric."
1919
// (after body parsing) began. This allows metrics to exclude body upload/parsing time.
2020
const processingStartTimeKey = "metrics.processingStartTime"
2121

22+
const HTTPStatusCodeGranularKey = attribute.Key("http.status_code_granular")
23+
2224
// SetProcessingStartTime stores the current time as the processing start time in the gin context.
2325
func SetProcessingStartTime(c *gin.Context) {
2426
c.Set(processingStartTimeKey, time.Now())
@@ -73,17 +75,16 @@ func Middleware(meterProvider metric.MeterProvider, service string, options ...O
7375
reqAttributes...,
7476
)
7577

78+
code := ginCtx.Writer.Status()
7679
if errors.Is(ctx.Err(), context.Canceled) {
7780
// 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 {
81-
code := ginCtx.Writer.Status() / 100 * 100
82-
resAttributes = append(resAttributes, semconv.HTTPStatusCodeKey.Int(code))
83-
} else {
84-
resAttributes = append(resAttributes, semconv.HTTPAttributesFromHTTPStatusCode(ginCtx.Writer.Status())...)
81+
code = 499
8582
}
8683

84+
groupedCode := code / 100 * 100
85+
resAttributes = append(resAttributes, semconv.HTTPStatusCodeKey.Int(groupedCode))
86+
resAttributes = append(resAttributes, HTTPStatusCodeGranularKey.Int(code))
87+
8788
// Append attributes from ginCtx
8889
resAttributes = append(resAttributes, attributesFromGinContext(ginCtx, MetricPrefix)...)
8990

0 commit comments

Comments
 (0)