Skip to content

expfmt: simplify format constants and provide default negotiation slices - #992

Open
bwplotka wants to merge 3 commits into
mainfrom
expfmt-ergonomics
Open

bwplotka wants to merge 3 commits into
mainfrom
expfmt-ergonomics

Conversation

@bwplotka

@bwplotka bwplotka commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

This PR improves the ergonomics of the expfmt package without breaking backwards compatibility by simplifying format constants, exposing default format negotiation slices as cloned getters.

cc @dashpole

Changes

  • Undeprecate Fmt... constants and ProtoFmt: Clarify in documentation that these represent baseline Content-Types used for HTTP headers and content negotiation, while advising against direct equality comparison (==) due to dynamic parameters (like ; escaping=...) and whitespace variations.
  • Clarify NewFormat documentation: Clarify that the latest stable version will be returned if a format type has more than one version.

Rationale: Make sure it's clear we will add FmtOpenMetrics_2_0_0 once stable.

  • Export FmtOpenMetrics_2_0_0: Expose the OpenMetrics 2.0.0 constant alongside the existing OpenMetrics format constants.

Rationales:

  • Format comparisons were still possible, new exp.NewFormat is not helping much
  • Using custom formats in Negotiate etc is now easier e.g.

Instead:

expfmt.NegotiateAccept(req.Header, 
  expfmt.NewFormat(expfmt.TypeProtoDelim), expfmt.NewFormat(expfmt.TypeProtoText), expfmt.NewFormat(expfmt.TypeProtoCompact), expfmt.NewFormat(expfmt.TypeTextPlain)
)

You can:

expfmt.NegotiateAccept(req.Header, expfmt.DefaultOpenMetricsAcceptedFormats()...)`

or

acc := expfmt.DefaultOpenMetricsAcceptedFormats()
acc = append(acc, expfmt.FmtOpenMetrics_2_0_0)
expfmt.NegotiateAccept(req.Header, acc...)`
  • Add Format.Version(): Provide a helper to extract the format version parameter (e.g., "0.0.4", "1.0.0", "2.0.0"), or empty string if unspecified or invalid.
  • Provide Default Accepted Format slices: Expose DefaultAcceptedFormats(), DefaultOpenMetricsAcceptedFormats() returning safely cloned slices via slices.Clone() for use with NegotiateAccept.

Fixes: #993

@bwplotka
bwplotka marked this pull request as draft September 22, 2026 15:56
Comment thread expfmt/encode.go Outdated
Comment thread expfmt/encode.go Outdated
@bwplotka bwplotka changed the title expfmt: simplify format constants and provide default negotiation slices expfmt: simplify format constants Sep 25, 2026
@bwplotka bwplotka changed the title expfmt: simplify format constants expfmt: simplify format constants and provide default negotiation slices Sep 25, 2026
- Undeprecate Fmt... constants and ProtoFmt, clarifying documentation
  that these represent baseline Content-Types.
- Export FmtOpenMetrics_2_0_0 constant.
- Add Format.Version() to extract format version parameter.
- Provide DefaultAcceptedFormats(), DefaultOpenMetricsAcceptedFormats(), and
  DefaultOpenMetrics2AcceptedFormats() functions returning cloned slices of
  default formats for use with NegotiateAccept.
- Deprecate Negotiate and NegotiateIncludingOpenMetrics in favor of
  NegotiateAccept with the respective default formats.
- Clarify NewFormat documentation that the latest stable version
  is returned when multiple versions exist.

Signed-off-by: bwplotka <bwplotka@gmail.com>
@bwplotka
bwplotka marked this pull request as ready for review September 25, 2026 09:53
@bwplotka
bwplotka requested review from SuperQ and ywwg September 25, 2026 09:53
Comment thread expfmt/encode.go
FmtText,
}

defaultOpenMetricsAcceptedFormats = []Format{

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the idea is to add 2.0 once stable

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.

Still FmtOpenMetrics_2_0_0 emits no warning (it did before)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What warning you have in mind? I don't recall any in the past? Also we probably don't want to pass logger to NewEncoder 🤔

Comment thread expfmt/expfmt.go Outdated
@roidelapluie

Copy link
Copy Markdown
Member

Thank you. The direction looks good!

Co-authored-by: Julien <291750+roidelapluie@users.noreply.github.com>
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
Comment thread expfmt/expfmt.go Outdated
Co-authored-by: David Ashpole <dashpole@google.com>
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

@ywwg ywwg 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.

looks good! a win for usability

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.

Improve expfmt.Format with expfmt.NegotiateAccept ergonomics

5 participants