Skip to content

fix: Add http.response.status_code to mt-broker-ingress and imc-dispatcher metrics#8891

Merged
knative-prow[bot] merged 5 commits intoknative:mainfrom
creydr:add-status-code-attribute-to-mt-broker-ingress-metrics
Mar 2, 2026
Merged

fix: Add http.response.status_code to mt-broker-ingress and imc-dispatcher metrics#8891
knative-prow[bot] merged 5 commits intoknative:mainfrom
creydr:add-status-code-attribute-to-mt-broker-ingress-metrics

Conversation

@creydr
Copy link
Member

@creydr creydr commented Mar 2, 2026

Adding the missing http.response.status_code attribute to the mt-broker-ingress and imc-dispatcher metrics

@creydr creydr requested review from Cali0707 and twoGiants March 2, 2026 13:55
@knative-prow knative-prow bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Mar 2, 2026
@knative-prow knative-prow bot requested a review from matzew March 2, 2026 13:55
@creydr creydr changed the title fix: Add http.response.status_code to mt-broker-ingress and imc-dispatcher metrics fix: Add http.response.status_code to mt-broker-ingress and imc-dispatcher metrics Mar 2, 2026
@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.60%. Comparing base (ec13a36) to head (955c8a7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/observability/newcontext.go 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8891      +/-   ##
==========================================
- Coverage   50.64%   50.60%   -0.04%     
==========================================
  Files         409      409              
  Lines       21650    21657       +7     
==========================================
- Hits        10964    10960       -4     
- Misses       9832     9843      +11     
  Partials      854      854              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@Cali0707 Cali0707 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@creydr what do you think of putting the specifics of response status code metrics into a reusable function similar to

func WithRequestLabels(ctx context.Context, r *http.Request) context.Context {
labeler, ok := otelhttp.LabelerFromContext(ctx)
if !ok {
ctx = otelhttp.ContextWithLabeler(ctx, labeler)
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
labeler.Add(RequestScheme.With(scheme))
return ctx
}
? We could also clean up
dispatchInfo, err := h.eventDispatcher.SendEvent(ctx, *event, target, opts...)
labeler, _ := otelhttp.LabelerFromContext(ctx)
attrs := []attribute.KeyValue{semconv.HTTPResponseStatusCode(dispatchInfo.ResponseCode)}
attrs = append(attrs, labeler.Get()...)
h.dispatchDuration.Record(ctx, dispatchInfo.Duration.Seconds(), metric.WithAttributes(attrs...))
with this as well

@knative-prow knative-prow bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Mar 2, 2026
@creydr
Copy link
Member Author

creydr commented Mar 2, 2026

@creydr what do you think of putting the specifics of response status code metrics into a reusable function similar to

func WithRequestLabels(ctx context.Context, r *http.Request) context.Context {
labeler, ok := otelhttp.LabelerFromContext(ctx)
if !ok {
ctx = otelhttp.ContextWithLabeler(ctx, labeler)
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
labeler.Add(RequestScheme.With(scheme))
return ctx
}

? We could also clean up

dispatchInfo, err := h.eventDispatcher.SendEvent(ctx, *event, target, opts...)
labeler, _ := otelhttp.LabelerFromContext(ctx)
attrs := []attribute.KeyValue{semconv.HTTPResponseStatusCode(dispatchInfo.ResponseCode)}
attrs = append(attrs, labeler.Get()...)
h.dispatchDuration.Record(ctx, dispatchInfo.Duration.Seconds(), metric.WithAttributes(attrs...))

with this as well

Good point. Updated it

@Cali0707
Copy link
Member

Cali0707 commented Mar 2, 2026

@creydr

WARNING: DATA RACE
Write at 0x00c00006ad80 by goroutine 47:
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch.func1()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:319 +0x749
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch.gowrap1()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:330 +0x97

Previous read at 0x00c00006ad80 by goroutine 48:
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch.func1()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:328 +0xb4a
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch.gowrap1()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:330 +0x97

Goroutine 47 (running) created at:
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:311 +0x108
  knative.dev/eventing/pkg/channel/fanout.NewFanoutEventHandler.createEventReceiverFunction.func4()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:297 +0x13a
  knative.dev/eventing/pkg/channel.(*EventReceiver).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/event_receiver.go:335 +0x1aec
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:303 +0x58
  knative.dev/eventing/pkg/channel/multichannelfanout.(*EventHandler).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/multichannelfanout/multi_channel_fanout_event_handler.go:160 +0x3e1
  go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.(*middleware).serveHTTP()
      /home/runner/work/eventing/eventing/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/handler.go:178 +0x2161
  go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewMiddleware.func1.1()
      /home/runner/work/eventing/eventing/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/handler.go:66 +0x67
  net/http.HandlerFunc.ServeHTTP()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:2322 +0x47
  knative.dev/pkg/network/handlers.(*Drainer).ServeHTTP()
      /home/runner/work/eventing/eventing/vendor/knative.dev/pkg/network/handlers/drain.go:113 +0x1b9
  net/http.serverHandler.ServeHTTP()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:3340 +0x2a1
  net/http.(*conn).serve()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:2109 +0xda4
  net/http.(*Server).Serve.gowrap3()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:3493 +0x4f

Goroutine 48 (finished) created at:
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).dispatch()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:311 +0x108
  knative.dev/eventing/pkg/channel/fanout.NewFanoutEventHandler.createEventReceiverFunction.func4()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:297 +0x13a
  knative.dev/eventing/pkg/channel.(*EventReceiver).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/event_receiver.go:335 +0x1aec
  knative.dev/eventing/pkg/channel/fanout.(*FanoutEventHandler).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/fanout/fanout_event_handler.go:303 +0x58
  knative.dev/eventing/pkg/channel/multichannelfanout.(*EventHandler).ServeHTTP()
      /home/runner/work/eventing/eventing/pkg/channel/multichannelfanout/multi_channel_fanout_event_handler.go:160 +0x3e1
  go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.(*middleware).serveHTTP()
      /home/runner/work/eventing/eventing/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/handler.go:178 +0x2161
  go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewMiddleware.func1.1()
      /home/runner/work/eventing/eventing/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/handler.go:66 +0x67
  net/http.HandlerFunc.ServeHTTP()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:2322 +0x47
  knative.dev/pkg/network/handlers.(*Drainer).ServeHTTP()
      /home/runner/work/eventing/eventing/vendor/knative.dev/pkg/network/handlers/drain.go:113 +0x1b9
  net/http.serverHandler.ServeHTTP()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:3340 +0x2a1
  net/http.(*conn).serve()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:2109 +0xda4
  net/http.(*Server).Serve.gowrap3()
      /opt/hostedtoolcache/go/1.25.7/x64/src/net/http/server.go:3493 +0x4f

@creydr creydr requested a review from Cali0707 March 2, 2026 15:05
Copy link
Member

@Cali0707 Cali0707 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

Thanks @creydr !

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Mar 2, 2026
@knative-prow
Copy link

knative-prow bot commented Mar 2, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Cali0707, creydr

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot merged commit 223b43f into knative:main Mar 2, 2026
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants