Skip to content

feat(context): add AbortedByHandler() and AbortedBy() to track abort origin - #4701

Open
raju-mechatronics wants to merge 2 commits into
gin-gonic:masterfrom
raju-mechatronics:feature/abort-caller-info
Open

raju-mechatronics wants to merge 2 commits into
gin-gonic:masterfrom
raju-mechatronics:feature/abort-caller-info

Conversation

@raju-mechatronics

Copy link
Copy Markdown
Contributor

Summary

Adds two new methods to Context that expose which handler in the chain called Abort(), enabling better observability and conditional logic in middleware.

New API

// AbortedByHandler returns the HandlerFunc that called Abort(), or nil if not aborted by a handler.
func (c *Context) AbortedByHandler() HandlerFunc

// AbortedBy returns the name of the handler that called Abort(), or "" if not applicable.
func (c *Context) AbortedBy() string

Behavior

  • First-abort-wins: if Abort() is called multiple times, only the first caller is recorded.
  • Returns nil/"" when the context was not aborted
  • Copy() preserves the tracked abort origin.
  • reset() clears it on context reuse.

Use Cases

// Logging
func Logger() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Next()
        if c.IsAborted() {
            log.Printf("request aborted by: %s", c.AbortedBy())
        }
    }
}

// Observability
span.SetAttribute("gin.aborted_by", c.AbortedBy())

// Conditional error handling
if c.IsAborted() && c.AbortedBy() == "authMiddleware" {
    // handle auth rejections differently
}

@codecov

codecov Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.65%. Comparing base (0f09c3a) to head (6063f34).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4701   +/-   ##
=======================================
  Coverage   98.65%   98.65%           
=======================================
  Files          48       48           
  Lines        3188     3201   +13     
=======================================
+ Hits         3145     3158   +13     
  Misses         43       43           
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.64% <100.00%> (+<0.01%) ⬆️
-tags go_json 98.58% <100.00%> (+<0.01%) ⬆️
-tags nomsgpack 98.63% <100.00%> (+<0.01%) ⬆️
go-1.26 98.34% <100.00%> (+<0.01%) ⬆️
go-1.27 98.67% <100.00%> (+<0.01%) ⬆️
macos-latest 98.65% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 98.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@FarmerChillax

Copy link
Copy Markdown
Contributor

// Observability
span.SetAttribute("gin.aborted_by", c.AbortedBy())

same to add this to otel-gin?

@raju-mechatronics

raju-mechatronics commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor Author

// Observability
span.SetAttribute("gin.aborted_by", c.AbortedBy())

same to add this to otel-gin?

Good point. Let's see if this PR merges first.

@raju-mechatronics

Copy link
Copy Markdown
Contributor Author

@appleboy can you please take a look at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants