Skip to content

Commit b933a95

Browse files
authored
Merge pull request #475 from gouthamve/negotiate-om-1.0
Negotiate OM v1.0.0
2 parents 2f04d2e + ad42fa5 commit b933a95

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

expfmt/encode.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
9999
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
100100
return FmtText
101101
}
102-
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion || ver == "") {
103-
return FmtOpenMetrics
102+
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
103+
if ver == OpenMetricsVersion_1_0_0 {
104+
return FmtOpenMetrics_1_0_0
105+
}
106+
return FmtOpenMetrics_0_0_1
104107
}
105108
}
106109
return FmtText
@@ -146,7 +149,7 @@ func NewEncoder(w io.Writer, format Format) Encoder {
146149
},
147150
close: func() error { return nil },
148151
}
149-
case FmtOpenMetrics:
152+
case FmtOpenMetrics_0_0_1, FmtOpenMetrics_1_0_0:
150153
return encoderCloser{
151154
encode: func(v *dto.MetricFamily) error {
152155
_, err := MetricFamilyToOpenMetrics(w, v)

expfmt/encode_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ func TestNegotiate(t *testing.T) {
6363
}
6464
}
6565

66+
func TestNegotiateOpenMetrics(t *testing.T) {
67+
tests := []struct {
68+
name string
69+
acceptHeaderValue string
70+
expectedFmt string
71+
}{
72+
{
73+
name: "OM format, no version",
74+
acceptHeaderValue: "application/openmetrics-text",
75+
expectedFmt: string(FmtOpenMetrics_0_0_1),
76+
},
77+
{
78+
name: "OM format, 0.0.1 version",
79+
acceptHeaderValue: "application/openmetrics-text;version=0.0.1",
80+
expectedFmt: string(FmtOpenMetrics_0_0_1),
81+
},
82+
{
83+
name: "OM format, 1.0.0 version",
84+
acceptHeaderValue: "application/openmetrics-text;version=1.0.0",
85+
expectedFmt: string(FmtOpenMetrics_1_0_0),
86+
},
87+
{
88+
name: "OM format, invalid version",
89+
acceptHeaderValue: "application/openmetrics-text;version=0.0.4",
90+
expectedFmt: string(FmtText),
91+
},
92+
}
93+
94+
for _, test := range tests {
95+
t.Run(test.name, func(t *testing.T) {
96+
h := http.Header{}
97+
h.Add(hdrAccept, test.acceptHeaderValue)
98+
actualFmt := string(NegotiateIncludingOpenMetrics(h))
99+
if actualFmt != test.expectedFmt {
100+
t.Errorf("expected Negotiate to return format %s, but got %s instead", test.expectedFmt, actualFmt)
101+
}
102+
})
103+
}
104+
}
66105
func TestEncode(t *testing.T) {
67106
var buff bytes.Buffer
68107
delimEncoder := NewEncoder(&buff, FmtProtoDelim)

expfmt/expfmt.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ type Format string
1919

2020
// Constants to assemble the Content-Type values for the different wire protocols.
2121
const (
22-
TextVersion = "0.0.4"
23-
ProtoType = `application/vnd.google.protobuf`
24-
ProtoProtocol = `io.prometheus.client.MetricFamily`
25-
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
26-
OpenMetricsType = `application/openmetrics-text`
27-
OpenMetricsVersion = "0.0.1"
22+
TextVersion = "0.0.4"
23+
ProtoType = `application/vnd.google.protobuf`
24+
ProtoProtocol = `io.prometheus.client.MetricFamily`
25+
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
26+
OpenMetricsType = `application/openmetrics-text`
27+
OpenMetricsVersion_0_0_1 = "0.0.1"
28+
OpenMetricsVersion_1_0_0 = "1.0.0"
2829

2930
// The Content-Type values for the different wire protocols.
30-
FmtUnknown Format = `<unknown>`
31-
FmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
32-
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
33-
FmtProtoText Format = ProtoFmt + ` encoding=text`
34-
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
35-
FmtOpenMetrics Format = OpenMetricsType + `; version=` + OpenMetricsVersion + `; charset=utf-8`
31+
FmtUnknown Format = `<unknown>`
32+
FmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
33+
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
34+
FmtProtoText Format = ProtoFmt + ` encoding=text`
35+
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
36+
FmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
37+
FmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
3638
)
3739

3840
const (

0 commit comments

Comments
 (0)