Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ywwg committed Feb 23, 2024
1 parent a5a635b commit 3dffe36
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
5 changes: 0 additions & 5 deletions prometheus/promhttp/http.go
Expand Up @@ -160,15 +160,11 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
}

var contentType expfmt.Format
if req.Header != nil {
fmt.Println("NEGOTIATE content type received: ", req.Header)
}
if opts.EnableOpenMetrics {
contentType = expfmt.NegotiateIncludingOpenMetrics(req.Header)
} else {
contentType = expfmt.Negotiate(req.Header)
}
fmt.Println("NEGOTIATE deduced content type: ", contentType)
header := rsp.Header()
header.Set(contentTypeHeader, string(contentType))

Expand Down Expand Up @@ -211,7 +207,6 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
}

for _, mf := range mfs {
// This is the place where the metric family is getting escaped on the receive side.
if handleError(enc.Encode(mf)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion prometheus/push/push.go
Expand Up @@ -109,7 +109,7 @@ func New(url, job string) *Pusher {
gatherers: prometheus.Gatherers{reg},
registerer: reg,
client: &http.Client{},
expfmt: expfmt.FmtProtoDelim,
expfmt: expfmt.NewFormat(expfmt.TypeProtoDelim),
}
}

Expand Down
2 changes: 1 addition & 1 deletion prometheus/push/push_test.go
Expand Up @@ -84,7 +84,7 @@ func TestPush(t *testing.T) {
}

buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, expfmt.FmtProtoDelim)
enc := expfmt.NewEncoder(buf, expfmt.NewFormat(expfmt.TypeProtoDelim))

for _, mf := range mfs {
if err := enc.Encode(mf); err != nil {
Expand Down
40 changes: 20 additions & 20 deletions prometheus/registry_test.go
Expand Up @@ -92,7 +92,7 @@ func testHandler(t testing.TB) {
},
}
externalBuf := &bytes.Buffer{}
enc := expfmt.NewEncoder(externalBuf, expfmt.FmtProtoDelim)
enc := expfmt.NewEncoder(externalBuf, expfmt.NewFormat(expfmt.TypeProtoDelim))
if err := enc.Encode(externalMetricFamily); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ metric: <
},
}
buf := &bytes.Buffer{}
enc = expfmt.NewEncoder(buf, expfmt.FmtProtoDelim)
enc = expfmt.NewEncoder(buf, expfmt.NewFormat(expfmt.TypeProtoDelim))
if err := enc.Encode(expectedMetricFamily); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -361,7 +361,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: []byte{},
},
Expand All @@ -372,7 +372,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: []byte{},
},
Expand All @@ -383,7 +383,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: []byte{},
},
Expand All @@ -394,7 +394,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values`,
},
body: []byte{},
},
Expand All @@ -405,7 +405,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: expectedMetricFamilyAsText,
},
Expand All @@ -417,7 +417,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values`,
},
body: expectedMetricFamilyAsBytes,
},
Expand All @@ -429,7 +429,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: externalMetricFamilyAsText,
},
Expand All @@ -441,7 +441,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values`,
},
body: externalMetricFamilyAsBytes,
},
Expand All @@ -453,7 +453,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand All @@ -472,7 +472,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: []byte{},
},
Expand All @@ -483,7 +483,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: expectedMetricFamilyAsText,
},
Expand All @@ -495,7 +495,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand All @@ -514,7 +514,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand All @@ -533,7 +533,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=text`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=text; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand All @@ -552,7 +552,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand All @@ -571,7 +571,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`,
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand Down Expand Up @@ -609,7 +609,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: expectedMetricFamilyAsText,
},
Expand Down Expand Up @@ -666,7 +666,7 @@ collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"
},
out: output{
headers: map[string]string{
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
"Content-Type": `text/plain; version=0.0.4; charset=utf-8; escaping=values`,
},
body: bytes.Join(
[][]byte{
Expand Down
2 changes: 1 addition & 1 deletion prometheus/testutil/promlint/promlint.go
Expand Up @@ -68,7 +68,7 @@ func (l *Linter) Lint() ([]Problem, error) {
var problems []Problem

if l.r != nil {
d := expfmt.NewDecoder(l.r, expfmt.FmtText_1_0_0)
d := expfmt.NewDecoder(l.r, expfmt.NewFormat(expfmt.TypeTextPlain))

mf := &dto.MetricFamily{}
for {
Expand Down
4 changes: 2 additions & 2 deletions prometheus/testutil/testutil.go
Expand Up @@ -265,13 +265,13 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
// result.
func compare(got, want []*dto.MetricFamily) error {
var gotBuf, wantBuf bytes.Buffer
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText_1_0_0)
enc := expfmt.NewEncoder(&gotBuf, expfmt.NewFormat(expfmt.TypeTextPlain))
for _, mf := range got {
if err := enc.Encode(mf); err != nil {
return fmt.Errorf("encoding gathered metrics failed: %w", err)
}
}
enc = expfmt.NewEncoder(&wantBuf, expfmt.FmtText_1_0_0)
enc = expfmt.NewEncoder(&wantBuf, expfmt.NewFormat(expfmt.TypeTextPlain))
for _, mf := range want {
if err := enc.Encode(mf); err != nil {
return fmt.Errorf("encoding expected metrics failed: %w", err)
Expand Down

0 comments on commit 3dffe36

Please sign in to comment.