Skip to content

feat: replace cgi with uri for encode and decode#2028

Open
xuan-cao-swi wants to merge 2 commits intoopen-telemetry:mainfrom
xuan-cao-swi:replace-cgi
Open

feat: replace cgi with uri for encode and decode#2028
xuan-cao-swi wants to merge 2 commits intoopen-telemetry:mainfrom
xuan-cao-swi:replace-cgi

Conversation

@xuan-cao-swi
Copy link
Contributor

This PR tries to use URI.encode/decode_uri_component to replace CGI.escape/unescape.

Although cgi/escape is present in Ruby 4.0.0, it is not available in Ruby versions less than 4.0.0. We can use cgi/escape once all Ruby 3 versions are deprecated, if required.

The difference between these two implementation is the treatment for asterisk (*), which CGI encode to %2A, but URI preserve * (see more in #2016).

CGI will silence the invalid char encode/decode, while URI will raise the error.

@xuan-cao-swi xuan-cao-swi changed the title fix!: replace cgi fix!: replace cgi with uri for encode and decode Jan 15, 2026

raw_value = getter.get(carrier, carrier_key)
value = CGI.unescape(raw_value)
value = URI.decode_uri_component(raw_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the same changes need to happen for B3 headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems B3 propagator headers should only contain trace ID, span ID, and sampling decision, which have well-defined headers that don't require decoding.

Copy link
Contributor

@arielvalentin arielvalentin Feb 3, 2026

Choose a reason for hiding this comment

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

Am I missing something? Then where these baggage headers encoded incorrectly when using the b3 propagators?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not decode before, also I don't see any other language decode these key and value
I can create an issue to discuss about decode b3 baggage headers

#
# SPDX-License-Identifier: Apache-2.0

require 'cgi'
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the CGI methods we're calling in the propagators the same in 3.x and 4.x, but the only difference is what needs to be required?

To help me better understand the issue, would something like this also solve the problem?

if RUBY_VERSION >= '4.0'
  require 'cgi/escape'
else
  require 'cgi'
end

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, I just saw the conversation on #2016. It sounds like URI is compatible with the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, other language use their stdlib to decode

@arielvalentin arielvalentin changed the title fix!: replace cgi with uri for encode and decode feat: replace cgi with uri for encode and decode Feb 10, 2026
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.

3 participants