Skip to content

Commit

Permalink
Merge pull request #475 from gouthamve/negotiate-om-1.0
Browse files Browse the repository at this point in the history
Negotiate OM v1.0.0
  • Loading branch information
gouthamve committed May 2, 2023
2 parents 2f04d2e + ad42fa5 commit b933a95
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
9 changes: 6 additions & 3 deletions expfmt/encode.go
Expand Up @@ -99,8 +99,11 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
return FmtText
}
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion || ver == "") {
return FmtOpenMetrics
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
if ver == OpenMetricsVersion_1_0_0 {
return FmtOpenMetrics_1_0_0
}
return FmtOpenMetrics_0_0_1
}
}
return FmtText
Expand Down Expand Up @@ -146,7 +149,7 @@ func NewEncoder(w io.Writer, format Format) Encoder {
},
close: func() error { return nil },
}
case FmtOpenMetrics:
case FmtOpenMetrics_0_0_1, FmtOpenMetrics_1_0_0:
return encoderCloser{
encode: func(v *dto.MetricFamily) error {
_, err := MetricFamilyToOpenMetrics(w, v)
Expand Down
39 changes: 39 additions & 0 deletions expfmt/encode_test.go
Expand Up @@ -63,6 +63,45 @@ func TestNegotiate(t *testing.T) {
}
}

func TestNegotiateOpenMetrics(t *testing.T) {
tests := []struct {
name string
acceptHeaderValue string
expectedFmt string
}{
{
name: "OM format, no version",
acceptHeaderValue: "application/openmetrics-text",
expectedFmt: string(FmtOpenMetrics_0_0_1),
},
{
name: "OM format, 0.0.1 version",
acceptHeaderValue: "application/openmetrics-text;version=0.0.1",
expectedFmt: string(FmtOpenMetrics_0_0_1),
},
{
name: "OM format, 1.0.0 version",
acceptHeaderValue: "application/openmetrics-text;version=1.0.0",
expectedFmt: string(FmtOpenMetrics_1_0_0),
},
{
name: "OM format, invalid version",
acceptHeaderValue: "application/openmetrics-text;version=0.0.4",
expectedFmt: string(FmtText),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h := http.Header{}
h.Add(hdrAccept, test.acceptHeaderValue)
actualFmt := string(NegotiateIncludingOpenMetrics(h))
if actualFmt != test.expectedFmt {
t.Errorf("expected Negotiate to return format %s, but got %s instead", test.expectedFmt, actualFmt)
}
})
}
}
func TestEncode(t *testing.T) {
var buff bytes.Buffer
delimEncoder := NewEncoder(&buff, FmtProtoDelim)
Expand Down
26 changes: 14 additions & 12 deletions expfmt/expfmt.go
Expand Up @@ -19,20 +19,22 @@ type Format string

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

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

const (
Expand Down

0 comments on commit b933a95

Please sign in to comment.