@@ -94,7 +94,21 @@ func Negotiate(h http.Header) Format {
9494// temporary and will disappear once FmtOpenMetrics is fully supported and as
9595// such may be negotiated by the normal Negotiate function.
9696func NegotiateIncludingOpenMetrics (h http.Header ) Format {
97+ formats := NegotiateFormatsIncludingOpenMetrics (h )
98+ return formats [0 ]
99+ }
100+
101+ // NegotiateFormatsIncludingOpenMetrics works like NegotiateIncludingOpenMetrics
102+ // but returns all recognised formats from the Accept header in descending
103+ // preference order instead of stopping at the first match. If no recognised
104+ // formats are found, the slice contains FmtText (with the negotiated escaping
105+ // scheme) as the default fallback.
106+ //
107+ // Callers that only support a subset of formats can iterate the result and pick
108+ // the first format they can serve.
109+ func NegotiateFormatsIncludingOpenMetrics (h http.Header ) []Format {
97110 escapingScheme := Format (fmt .Sprintf ("; escaping=%s" , Format (model .NameEscapingScheme .String ())))
111+ var formats []Format
98112 for _ , ac := range goautoneg .ParseAccept (h .Get (hdrAccept )) {
99113 if escapeParam := ac .Params [model .EscapingKey ]; escapeParam != "" {
100114 switch Format (escapeParam ) {
@@ -108,26 +122,31 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
108122 if ac .Type + "/" + ac .SubType == ProtoType && ac .Params ["proto" ] == ProtoProtocol {
109123 switch ac .Params ["encoding" ] {
110124 case "delimited" :
111- return FmtProtoDelim + escapingScheme
125+ formats = append ( formats , FmtProtoDelim + escapingScheme )
112126 case "text" :
113- return FmtProtoText + escapingScheme
127+ formats = append ( formats , FmtProtoText + escapingScheme )
114128 case "compact-text" :
115- return FmtProtoCompact + escapingScheme
129+ formats = append ( formats , FmtProtoCompact + escapingScheme )
116130 }
131+ continue
117132 }
118133 if ac .Type == "text" && ac .SubType == "plain" && (ver == TextVersion || ver == "" ) {
119- return FmtText + escapingScheme
134+ formats = append (formats , FmtText + escapingScheme )
135+ continue
120136 }
121137 if ac .Type + "/" + ac .SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "" ) {
122138 switch ver {
123139 case OpenMetricsVersion_1_0_0 :
124- return FmtOpenMetrics_1_0_0 + escapingScheme
140+ formats = append ( formats , FmtOpenMetrics_1_0_0 + escapingScheme )
125141 default :
126- return FmtOpenMetrics_0_0_1 + escapingScheme
142+ formats = append ( formats , FmtOpenMetrics_0_0_1 + escapingScheme )
127143 }
128144 }
129145 }
130- return FmtText + escapingScheme
146+ if len (formats ) == 0 {
147+ return []Format {FmtText + escapingScheme }
148+ }
149+ return formats
131150}
132151
133152// NewEncoder returns a new encoder based on content type negotiation. All
0 commit comments