-
Notifications
You must be signed in to change notification settings - Fork 366
expfmt: simplify format constants and provide default negotiation slices #992
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ func init() { | |
| FmtProtoText, | ||
| FmtProtoCompact, | ||
| FmtOpenMetrics_1_0_0, | ||
| fmtOpenMetrics_2_0_0, | ||
| FmtOpenMetrics_2_0_0, | ||
| FmtOpenMetrics_0_0_1, | ||
| } { | ||
| if parsed := goautoneg.ParseAccept(string(f)); len(parsed) > 0 { | ||
|
|
@@ -45,6 +45,41 @@ func init() { | |
| } | ||
| } | ||
|
|
||
| var ( | ||
| // DefaultAcceptedFormats contains the standard accepted formats for Negotiate, | ||
| // ordered by preference (delimited protobuf, protobuf text, compact protobuf text, | ||
| // and Prometheus text format). | ||
| DefaultAcceptedFormats = []Format{ | ||
| FmtProtoDelim, | ||
| FmtProtoText, | ||
| FmtProtoCompact, | ||
| FmtText, | ||
| } | ||
|
|
||
| // DefaultOpenMetricsAcceptedFormats contains the standard accepted formats including | ||
| // OpenMetrics 1.0.0 and 0.0.1, ordered by preference. | ||
| DefaultOpenMetricsAcceptedFormats = []Format{ | ||
| FmtOpenMetrics_1_0_0, | ||
| FmtOpenMetrics_0_0_1, | ||
| FmtProtoDelim, | ||
| FmtProtoText, | ||
| FmtProtoCompact, | ||
| FmtText, | ||
| } | ||
|
|
||
| // DefaultOpenMetrics2AcceptedFormats contains the accepted formats including | ||
| // OpenMetrics 2.0.0, 1.0.0, and 0.0.1, ordered by preference. | ||
| DefaultOpenMetrics2AcceptedFormats = []Format{ | ||
| FmtOpenMetrics_2_0_0, | ||
| FmtOpenMetrics_1_0_0, | ||
| FmtOpenMetrics_0_0_1, | ||
| FmtProtoDelim, | ||
| FmtProtoText, | ||
| FmtProtoCompact, | ||
| FmtText, | ||
| } | ||
| ) | ||
|
|
||
| // Encoder types encode metric families into an underlying wire protocol. | ||
| type Encoder interface { | ||
| Encode(*dto.MetricFamily) error | ||
|
|
@@ -78,19 +113,19 @@ func (ec encoderCloser) Close() error { | |
| // appropriate accepted type is found, FmtText is returned (which is the | ||
| // Prometheus text format). | ||
| // | ||
| // Deprecated: Use NegotiateAccept(h, FmtProtoDelim, FmtProtoText, FmtProtoCompact, FmtText) | ||
| // Deprecated: Use NegotiateAccept(h, DefaultAcceptedFormats...) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...)
} |
||
| // or specify only the formats supported by your server. | ||
| func Negotiate(h http.Header) Format { | ||
| return NegotiateAccept(h, FmtProtoDelim, FmtProtoText, FmtProtoCompact, FmtText) | ||
| return NegotiateAccept(h, DefaultAcceptedFormats...) | ||
| } | ||
|
|
||
| // NegotiateIncludingOpenMetrics works like Negotiate but includes | ||
| // FmtOpenMetrics as an option for the result. | ||
| // | ||
| // Deprecated: Use NegotiateAccept(h, FmtOpenMetrics_1_0_0, FmtOpenMetrics_0_0_1, FmtProtoDelim, FmtProtoText, FmtProtoCompact, FmtText) | ||
| // Deprecated: Use NegotiateAccept(h, DefaultOpenMetricsAcceptedFormats...) | ||
| // or specify only the formats supported by your server. | ||
| func NegotiateIncludingOpenMetrics(h http.Header) Format { | ||
| return NegotiateAccept(h, FmtOpenMetrics_1_0_0, FmtOpenMetrics_0_0_1, FmtProtoDelim, FmtProtoText, FmtProtoCompact, FmtText) | ||
| return NegotiateAccept(h, DefaultOpenMetricsAcceptedFormats...) | ||
| } | ||
|
|
||
| // NegotiateAccept returns the Content-Type based on the given Accept header | ||
|
|
||
There was a problem hiding this comment.
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.