Skip to content

Gin instrumentation: support reading Client IP from custom headers, and make sure proxy is trusted#6095

Merged
dmathieu merged 11 commits intoopen-telemetry:mainfrom
ItalyPaleAle:gin-clientip
Mar 25, 2025
Merged

Gin instrumentation: support reading Client IP from custom headers, and make sure proxy is trusted#6095
dmathieu merged 11 commits intoopen-telemetry:mainfrom
ItalyPaleAle:gin-clientip

Conversation

@ItalyPaleAle
Copy link
Copy Markdown
Contributor

@ItalyPaleAle ItalyPaleAle commented Sep 8, 2024

With Gin, it's possible to configure the server to read the Client IP from custom headers; examples include X-Real-Ip or CF-Connecting-IP. This PR makes it possible to set as span attribute the same IP that Gin reads.

Additionally, it makes sure that headers such as "X-Forwarded-For" are used only if Gin is configured to trust the upstream server

@ItalyPaleAle ItalyPaleAle requested a review from a team September 8, 2024 21:34
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Sep 8, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Sep 9, 2024

PS: Also fixed unit tests, where there were assertions inside handlers, which are executed in separate goroutines

Could you split that into another PR, to make things easier to review?

@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

PS: Also fixed unit tests, where there were assertions inside handlers, which are executed in separate goroutines

Could you split that into another PR, to make things easier to review?

Done

Comment thread instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.8%. Comparing base (1883203) to head (b19ba59).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #6095   +/-   ##
=====================================
  Coverage   75.7%   75.8%           
=====================================
  Files        207     207           
  Lines      19403   19469   +66     
=====================================
+ Hits       14695   14758   +63     
- Misses      4273    4275    +2     
- Partials     435     436    +1     
Files with missing lines Coverage Δ
...lei/go-restful/otelrestful/internal/semconv/env.go 89.1% <100.0%> (ø)
...o-restful/otelrestful/internal/semconv/httpconv.go 86.9% <100.0%> (+0.1%) ⬆️
...go-restful/otelrestful/internal/semconv/v1.20.0.go 89.9% <100.0%> (ø)
...stful/otelrestful/internal/semconvutil/httpconv.go 95.7% <100.0%> (+<0.1%) ⬆️
...hub.com/emicklei/go-restful/otelrestful/restful.go 100.0% <100.0%> (ø)
...umentation/github.com/gin-gonic/gin/otelgin/gin.go 92.9% <100.0%> (+0.3%) ⬆️
....com/gin-gonic/gin/otelgin/internal/semconv/env.go 92.5% <100.0%> (ø)
...gin-gonic/gin/otelgin/internal/semconv/httpconv.go 86.9% <100.0%> (+0.1%) ⬆️
.../gin-gonic/gin/otelgin/internal/semconv/v1.20.0.go 89.9% <100.0%> (ø)
...gonic/gin/otelgin/internal/semconvutil/httpconv.go 95.7% <100.0%> (+<0.1%) ⬆️
... and 17 more

... and 1 file with indirect coverage changes

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

Copy link
Copy Markdown
Member

@dmathieu dmathieu left a comment

Choose a reason for hiding this comment

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

This will need a changelog entry.

Comment thread instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go Outdated
Comment thread CHANGELOG.md Outdated
Comment thread instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go Outdated
Comment thread CHANGELOG.md Outdated
@ItalyPaleAle ItalyPaleAle requested a review from a team as a code owner September 20, 2024 14:51
@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

@dmathieu is this PR ready to be merged?

@dmathieu
Copy link
Copy Markdown
Member

PRs need two approvals.

Comment thread CHANGELOG.md Outdated
@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

PRs need two approvals.

@dmathieu is there a way I get a review/approval from another person?

@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Oct 7, 2024

Ping @open-telemetry/go-approvers for review.

Note that this instrumentation has no owner, and is therefore in the process of being deprecated/removed unless someone steps up to own it (which also explains the lack of reviews).
#6190

@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

Looks like this instrumentation may have an owner now. If I fix the merge conflicts, can this PR be merged now?
@open-telemetry/go-approvers @akats7

@dmathieu dmathieu requested a review from akats7 February 7, 2025 08:24
@akats7
Copy link
Copy Markdown
Contributor

akats7 commented Feb 10, 2025

Hey @ItalyPaleAle, can you please resolve the conflicts

… sure proxy is trusted)

With Gin, it's possible to configure the server to read the Client IP from custom headers; examples include `X-Real-Ip` or `CF-Connecting-IP`. This PR makes it possible to set as span attribute the same IP that Gin reads.

Additionally, it makes sure that headers such as "X-Forwarded-For" are used only if Gin is configured to trust the upstream server

PS: Also fixed unit tests, where there were assertions inside handlers, which are executed in separate goroutines
@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

@akats7 sorry for the delay, as the amount of merge conflicts that had accumulated meant I had to recreate the PR from scratch.

Should be good for your review now!

@dmathieu dmathieu changed the title Gin exporter: support reading Client IP from custom headers (and make sure proxy is trusted) Gin instrumentation: support reading Client IP from custom headers (and make sure proxy is trusted Mar 19, 2025
@ItalyPaleAle ItalyPaleAle changed the title Gin instrumentation: support reading Client IP from custom headers (and make sure proxy is trusted Gin instrumentation: support reading Client IP from custom headers, and make sure proxy is trusted Mar 19, 2025
@ItalyPaleAle
Copy link
Copy Markdown
Contributor Author

Could I get a review on this please?

Also, is there a way to ensure PRs like this don't keep accumulating conflicts in the changelog?

Comment thread instrumentation/net/http/otelhttp/internal/semconvutil/httpconv.go
Comment thread CHANGELOG.md Outdated
Comment thread instrumentation/net/http/otelhttp/internal/semconvutil/httpconv.go
@dmathieu dmathieu merged commit a262a05 into open-telemetry:main Mar 25, 2025
@ItalyPaleAle ItalyPaleAle deleted the gin-clientip branch March 25, 2025 14:51
@MrAlias MrAlias added this to the v1.36.0 milestone Mar 27, 2025
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.

4 participants