Skip to content

feat(encoding): Add MediaType::encode - #384

Merged
krisztianfekete merged 8 commits into
prometheus:masterfrom
kalleep:content-negotiation-encode
Nov 17, 2026
Merged

krisztianfekete merged 8 commits into
prometheus:masterfrom
kalleep:content-negotiation-encode

Conversation

@kalleep

@kalleep kalleep commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #381. Adds MediaType::encode, so callers don't have to match on the format themselves.

let media_type = negotiate(accept, &[Format::PrometheusText0_0_4, Format::OpenMetricsText1_0_0]);
let mut body = Vec::new();
media_type.encode(&registry, &mut body).unwrap();

encode writes to any io::Write. The text formats write straight into the writer, so the output is not buffered a second time. The protobuf format is encoded with encode_to_vec and then written.

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, added a few small comments!

Comment thread src/encoding/negotiate.rs
}
}

/// Encodes `registry` into `writer` in the format of this media type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we document that if this fails, part of the output may already be in writer, so a handler must not send the buffer it was writing into?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 22701de

Comment thread src/encoding/negotiate.rs Outdated
Comment on lines +85 to +86
registry: &Registry,
writer: &mut W,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: text::encode and prometheus_text::encode take the writer first, but have no hard opinion on this one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated, better to be consistent

Comment thread src/encoding/negotiate.rs Outdated
Comment on lines +101 to +102
let body = prometheus_protobuf::encode_to_vec(registry)?;
writer.write_all(&body)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This holds the whole protobuf body 2x. We could avoid with encoding each family into a reused scratch buffer. Fine as a follow-up though, but please open an issue to track this in that case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes I was thinking about this before but just forgot to revisit it, changed in 3106abd

Comment thread src/encoding/negotiate.rs
}

#[test]
fn encodes_formats() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please add a test with a writer that fails, asserting EncodeError::Io and that the original ErrorKind is kept? That error path has no test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added test to cover it

Comment thread CHANGELOG.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please move it to [Unreleased].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hm my changelog entry is already under [Unreleased] but I moved the pr link. Also moved #381 since I am pretty sure this was not released if that is what you meant?

See f5b15c3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, that's what I meant, sorry for the weird wording. Thanks!

Encodes a registry into an io::Write in the negotiated format, so callers
don't have to match on the format themselves.

Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
Signed-off-by: Kalle <23356117+kalleep@users.noreply.github.com>
@kalleep
kalleep force-pushed the content-negotiation-encode branch from a150ada to 3106abd Compare November 17, 2026 10:59
@krisztianfekete
krisztianfekete added this pull request to the merge queue Nov 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 17, 2026
@krisztianfekete
krisztianfekete added this pull request to the merge queue Nov 17, 2026
@krisztianfekete
krisztianfekete removed this pull request from the merge queue due to a manual request Nov 17, 2026
@krisztianfekete
krisztianfekete added this pull request to the merge queue Nov 17, 2026
Merged via the queue into prometheus:master with commit 98ab0ab Nov 17, 2026
11 checks passed
@kalleep
kalleep deleted the content-negotiation-encode branch November 17, 2026 13:13
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