Skip to content

UTF-8 Content Negotiation #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

UTF-8 Content Negotiation #1102

wants to merge 4 commits into from

Conversation

ywwg
Copy link
Member

@ywwg ywwg commented Apr 8, 2025

Part of #1013

The remaining piece is configuration to force escaping even if a scraper does not request it

@@ -37,7 +37,7 @@
'write_to_textfile',
)

CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
CONTENT_TYPE_PLAIN = 'text/plain; version=0.0.4; charset=utf-8'
Copy link
Member Author

@ywwg ywwg Apr 10, 2025

Choose a reason for hiding this comment

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

text/plain v 0.0.4 is not the latest, so I thought it best to rename this

Copy link
Member

Choose a reason for hiding this comment

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

Though I agree with you, this will be a breaking change as other programs use CONTENT_TYPE_LATEST. Perhaps we should update this to version=1.0.0 instead?

@ywwg ywwg marked this pull request as ready for review April 10, 2025 14:08
@ywwg ywwg marked this pull request as draft April 10, 2025 14:17
@ywwg ywwg force-pushed the owilliams/utf8-02 branch from 61e4a01 to 3240802 Compare April 10, 2025 14:24
@ywwg
Copy link
Member Author

ywwg commented Apr 10, 2025

TODO need more openmetrics/exposition tests

@ywwg ywwg force-pushed the owilliams/utf8-02 branch 2 times, most recently from 33ea581 to 630fb65 Compare April 10, 2025 14:42
@ywwg ywwg marked this pull request as ready for review April 10, 2025 14:42
@ywwg ywwg marked this pull request as draft April 10, 2025 14:42
@ywwg ywwg force-pushed the owilliams/utf8-02 branch from 60097b9 to 769f7fb Compare April 10, 2025 15:00
@ywwg ywwg marked this pull request as ready for review April 10, 2025 15:00
@ywwg ywwg requested a review from vesari April 10, 2025 15:03


def _escape(s: str) -> str:
def _escape(s: str, escaping: str, is_labelname: bool) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love how messy this function signature is getting...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it is internal only so not the end of the world. Would it help at all to have a second function for specifically escaping label names?

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried another approach that looks better

@ywwg ywwg changed the title initial work on content negotiation for client python UTF-8 Content Negotiation Apr 10, 2025
Signed-off-by: Owen Williams <[email protected]>
@ywwg ywwg force-pushed the owilliams/utf8-02 branch from d7d7dc2 to 80fa206 Compare April 10, 2025 18:20
@@ -37,7 +37,7 @@
'write_to_textfile',
)

CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
CONTENT_TYPE_PLAIN = 'text/plain; version=0.0.4; charset=utf-8'
Copy link
Member

Choose a reason for hiding this comment

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

Though I agree with you, this will be a breaking change as other programs use CONTENT_TYPE_LATEST. Perhaps we should update this to version=1.0.0 instead?

escaping = _get_escaping(toks)
# Only return an escaping header if we have a good version and
# mimetype.
if version == '1.0.0':
Copy link
Member

Choose a reason for hiding this comment

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

Should this test that the version is at least 1.0.0? I imagine at least anything in 1.x should be ok?

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like there's a python lib for that!

Copy link
Member Author

Choose a reason for hiding this comment

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

lmk if I should just test for major version versus any >= version

Copy link
Member Author

Choose a reason for hiding this comment

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

(I think any greater version should be fine)



def _escape(s: str) -> str:
def _escape(s: str, escaping: str, is_labelname: bool) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it is internal only so not the end of the world. Would it help at all to have a second function for specifically escaping label names?

Copy link
Member

Choose a reason for hiding this comment

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

Is this used anywhere? Or just a nice example for testing locally?

Copy link
Member Author

Choose a reason for hiding this comment

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

just for testing locally

Copy link
Member Author

Choose a reason for hiding this comment

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

but is also pretty nice for people to get off the ground

@ywwg
Copy link
Member Author

ywwg commented Apr 30, 2025

Though I agree with you, this will be a breaking change as other programs use CONTENT_TYPE_LATEST. Perhaps we should update this to version=1.0.0 instead?

we do still need a 0.0.4 constant as the fallback when all else fails. What I could do is update LATEST to be 1.0.0 and then create the new PLAIN constant for 0.0.4

@ywwg ywwg requested a review from csmarchbanks April 30, 2025 18:25
@ywwg ywwg marked this pull request as draft April 30, 2025 18:31
@ywwg ywwg marked this pull request as ready for review April 30, 2025 18:49
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