Skip to content

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

Draft
bwplotka wants to merge 1 commit into
mainfrom
expfmt-ergonomics
Draft

bwplotka wants to merge 1 commit into
mainfrom
expfmt-ergonomics

Conversation

@bwplotka

@bwplotka bwplotka commented Sep 22, 2026

Copy link
Copy Markdown
Member

This PR attempts to improve the ergonomics of the expfmt package without breaking backwards compatibility.

NOTE: Still playing with different ways.

Fixes: #993

- 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.
- Add Format.Matches() to safely compare formats while ignoring
  dynamic parameters (e.g. escaping scheme) and whitespace differences.
- Provide DefaultAcceptedFormats, DefaultOpenMetricsAcceptedFormats, and
  DefaultOpenMetrics2AcceptedFormats slices for use with NegotiateAccept.
- Update Negotiate and NegotiateIncludingOpenMetrics to use the new slices.

Signed-off-by: bwplotka <bwplotka@gmail.com>
@bwplotka
bwplotka marked this pull request as draft September 22, 2026 15:56
Comment thread expfmt/encode.go
// DefaultAcceptedFormats contains the standard accepted formats for Negotiate,
// ordered by preference (delimited protobuf, protobuf text, compact protobuf text,
// and Prometheus text format).
DefaultAcceptedFormats = []Format{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Making these package vars makes them mutable as a side-effect ... are we worried about someone messing with these and changing the defaults? One possibility is to make them private and make public function accessors that return copies of the slice. It's kind of annoying that there's not a safe way to hand out immutable zero-cost copies of the default slice.

Comment thread expfmt/encode.go
// Prometheus text format).
//
// Deprecated: Use NegotiateAccept(h, FmtProtoDelim, FmtProtoText, FmtProtoCompact, FmtText)
// Deprecated: Use NegotiateAccept(h, DefaultAcceptedFormats...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remind me why this function is deprecated again? Having a negotiate function that uses default accepted formats seems preferable to making people get their own copy of the default formats

Would it make sense to have a private package var for the formats for Negotiate / NegotiateIncludingOpenMetrics, a getter that returns a slice for those, and to undeprecate the Negotiate / NegotiateIncludingOpenMetrics methods and just document the equivalent calls?

var defaultAcceptedFormats = ...

func DefaultAcceptedFormats() []Format {
  return slices.Clone(defaultAcceptedFormats)
}

func Negotiate(h http.Header) Format {
  return NegotiateAccept(h, defaultAcceptedFormats...)
}

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

2 participants