Skip to content

Commit 4cf22c5

Browse files
author
Arthur Silva Sens
authored
Merge pull request #665 from prometheus/superq/revert_unexport
Revert #576 and add deprecation notice
2 parents 1d53a04 + c3e8247 commit 4cf22c5

File tree

6 files changed

+83
-76
lines changed

6 files changed

+83
-76
lines changed

expfmt/decode.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,29 @@ func ResponseFormat(h http.Header) Format {
4545

4646
mediatype, params, err := mime.ParseMediaType(ct)
4747
if err != nil {
48-
return fmtUnknown
48+
return FmtUnknown
4949
}
5050

5151
const textType = "text/plain"
5252

5353
switch mediatype {
5454
case ProtoType:
5555
if p, ok := params["proto"]; ok && p != ProtoProtocol {
56-
return fmtUnknown
56+
return FmtUnknown
5757
}
5858
if e, ok := params["encoding"]; ok && e != "delimited" {
59-
return fmtUnknown
59+
return FmtUnknown
6060
}
61-
return fmtProtoDelim
61+
return FmtProtoDelim
6262

6363
case textType:
6464
if v, ok := params["version"]; ok && v != TextVersion {
65-
return fmtUnknown
65+
return FmtUnknown
6666
}
67-
return fmtText
67+
return FmtText
6868
}
6969

70-
return fmtUnknown
70+
return FmtUnknown
7171
}
7272

7373
// NewDecoder returns a new decoder based on the given input format.

expfmt/decode_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func TestProtoMultiMessageDecoder(t *testing.T) {
423423
}
424424

425425
buf := bytes.NewReader(data)
426-
decoder := NewDecoder(buf, fmtProtoDelim)
426+
decoder := NewDecoder(buf, FmtProtoDelim)
427427
var metrics []*dto.MetricFamily
428428
for {
429429
var mf dto.MetricFamily
@@ -448,27 +448,27 @@ func testDiscriminatorHTTPHeader(t testing.TB) {
448448
}{
449449
{
450450
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="delimited"`},
451-
output: fmtProtoDelim,
451+
output: FmtProtoDelim,
452452
},
453453
{
454454
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="illegal"; encoding="delimited"`},
455-
output: fmtUnknown,
455+
output: FmtUnknown,
456456
},
457457
{
458458
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="illegal"`},
459-
output: fmtUnknown,
459+
output: FmtUnknown,
460460
},
461461
{
462462
input: map[string]string{"Content-Type": `text/plain; version=0.0.4`},
463-
output: fmtText,
463+
output: FmtText,
464464
},
465465
{
466466
input: map[string]string{"Content-Type": `text/plain`},
467-
output: fmtText,
467+
output: FmtText,
468468
},
469469
{
470470
input: map[string]string{"Content-Type": `text/plain; version=0.0.3`},
471-
output: fmtUnknown,
471+
output: FmtUnknown,
472472
},
473473
}
474474

@@ -574,7 +574,7 @@ func TestTextDecoderWithBufioReader(t *testing.T) {
574574

575575
var decoded bool
576576
r := bufio.NewReader(strings.NewReader(example))
577-
dec := NewDecoder(r, fmtText)
577+
dec := NewDecoder(r, FmtText)
578578
for {
579579
var mf dto.MetricFamily
580580
if err := dec.Decode(&mf); err != nil {

expfmt/encode.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ func Negotiate(h http.Header) Format {
7777
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
7878
switch ac.Params["encoding"] {
7979
case "delimited":
80-
return fmtProtoDelim + escapingScheme
80+
return FmtProtoDelim + escapingScheme
8181
case "text":
82-
return fmtProtoText + escapingScheme
82+
return FmtProtoText + escapingScheme
8383
case "compact-text":
84-
return fmtProtoCompact + escapingScheme
84+
return FmtProtoCompact + escapingScheme
8585
}
8686
}
8787
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
88-
return fmtText + escapingScheme
88+
return FmtText + escapingScheme
8989
}
9090
}
91-
return fmtText + escapingScheme
91+
return FmtText + escapingScheme
9292
}
9393

9494
// NegotiateIncludingOpenMetrics works like Negotiate but includes
@@ -110,26 +110,26 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
110110
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
111111
switch ac.Params["encoding"] {
112112
case "delimited":
113-
return fmtProtoDelim + escapingScheme
113+
return FmtProtoDelim + escapingScheme
114114
case "text":
115-
return fmtProtoText + escapingScheme
115+
return FmtProtoText + escapingScheme
116116
case "compact-text":
117-
return fmtProtoCompact + escapingScheme
117+
return FmtProtoCompact + escapingScheme
118118
}
119119
}
120120
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
121-
return fmtText + escapingScheme
121+
return FmtText + escapingScheme
122122
}
123123
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
124124
switch ver {
125125
case OpenMetricsVersion_1_0_0:
126-
return fmtOpenMetrics_1_0_0 + escapingScheme
126+
return FmtOpenMetrics_1_0_0 + escapingScheme
127127
default:
128-
return fmtOpenMetrics_0_0_1 + escapingScheme
128+
return FmtOpenMetrics_0_0_1 + escapingScheme
129129
}
130130
}
131131
}
132-
return fmtText + escapingScheme
132+
return FmtText + escapingScheme
133133
}
134134

135135
// NewEncoder returns a new encoder based on content type negotiation. All

expfmt/encode_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -222,56 +222,56 @@ func TestEncode(t *testing.T) {
222222
// 1: Untyped ProtoDelim
223223
{
224224
metric: metric1,
225-
format: fmtProtoDelim,
225+
format: FmtProtoDelim,
226226
},
227-
// 2: Untyped fmtProtoCompact
227+
// 2: Untyped FmtProtoCompact
228228
{
229229
metric: metric1,
230-
format: fmtProtoCompact,
230+
format: FmtProtoCompact,
231231
},
232-
// 3: Untyped fmtProtoText
232+
// 3: Untyped FmtProtoText
233233
{
234234
metric: metric1,
235-
format: fmtProtoText,
235+
format: FmtProtoText,
236236
},
237-
// 4: Untyped fmtText
237+
// 4: Untyped FmtText
238238
{
239239
metric: metric1,
240-
format: fmtText,
240+
format: FmtText,
241241
expOut: `# TYPE foo_metric untyped
242242
foo_metric 1.234
243243
`,
244244
},
245-
// 5: Untyped fmtOpenMetrics_0_0_1
245+
// 5: Untyped FmtOpenMetrics_0_0_1
246246
{
247247
metric: metric1,
248-
format: fmtOpenMetrics_0_0_1,
248+
format: FmtOpenMetrics_0_0_1,
249249
expOut: `# TYPE foo_metric unknown
250250
foo_metric 1.234
251251
`,
252252
},
253-
// 6: Untyped fmtOpenMetrics_1_0_0
253+
// 6: Untyped FmtOpenMetrics_1_0_0
254254
{
255255
metric: metric1,
256-
format: fmtOpenMetrics_1_0_0,
256+
format: FmtOpenMetrics_1_0_0,
257257
expOut: `# TYPE foo_metric unknown
258258
foo_metric 1.234
259259
`,
260260
},
261-
// 7: Simple Counter fmtOpenMetrics_0_0_1 unit opted in
261+
// 7: Simple Counter FmtOpenMetrics_0_0_1 unit opted in
262262
{
263263
metric: metric1,
264-
format: fmtOpenMetrics_0_0_1,
264+
format: FmtOpenMetrics_0_0_1,
265265
options: []EncoderOption{WithUnit()},
266266
expOut: `# TYPE foo_metric_seconds unknown
267267
# UNIT foo_metric_seconds seconds
268268
foo_metric_seconds 1.234
269269
`,
270270
},
271-
// 8: Simple Counter fmtOpenMetrics_1_0_0 unit opted out
271+
// 8: Simple Counter FmtOpenMetrics_1_0_0 unit opted out
272272
{
273273
metric: metric1,
274-
format: fmtOpenMetrics_1_0_0,
274+
format: FmtOpenMetrics_1_0_0,
275275
expOut: `# TYPE foo_metric unknown
276276
foo_metric 1.234
277277
`,
@@ -310,7 +310,7 @@ foo_metric 1.234
310310

311311
func TestEscapedEncode(t *testing.T) {
312312
var buff bytes.Buffer
313-
delimEncoder := NewEncoder(&buff, fmtProtoDelim+"; escaping=underscores")
313+
delimEncoder := NewEncoder(&buff, FmtProtoDelim+"; escaping=underscores")
314314
metric := &dto.MetricFamily{
315315
Name: proto.String("foo.metric"),
316316
Type: dto.MetricType_UNTYPED.Enum(),
@@ -346,7 +346,7 @@ func TestEscapedEncode(t *testing.T) {
346346

347347
buff.Reset()
348348

349-
compactEncoder := NewEncoder(&buff, fmtProtoCompact)
349+
compactEncoder := NewEncoder(&buff, FmtProtoCompact)
350350
err = compactEncoder.Encode(metric)
351351
if err != nil {
352352
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -359,7 +359,7 @@ func TestEscapedEncode(t *testing.T) {
359359

360360
buff.Reset()
361361

362-
protoTextEncoder := NewEncoder(&buff, fmtProtoText)
362+
protoTextEncoder := NewEncoder(&buff, FmtProtoText)
363363
err = protoTextEncoder.Encode(metric)
364364
if err != nil {
365365
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -372,7 +372,7 @@ func TestEscapedEncode(t *testing.T) {
372372

373373
buff.Reset()
374374

375-
textEncoder := NewEncoder(&buff, fmtText)
375+
textEncoder := NewEncoder(&buff, FmtText)
376376
err = textEncoder.Encode(metric)
377377
if err != nil {
378378
t.Errorf("unexpected error during encode: %s", err.Error())

expfmt/expfmt.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,31 @@ type Format string
3232
// it on the wire, new content-type strings will have to be agreed upon and
3333
// added here.
3434
const (
35-
TextVersion = "0.0.4"
36-
ProtoType = `application/vnd.google.protobuf`
37-
ProtoProtocol = `io.prometheus.client.MetricFamily`
38-
protoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
35+
TextVersion = "0.0.4"
36+
ProtoType = `application/vnd.google.protobuf`
37+
ProtoProtocol = `io.prometheus.client.MetricFamily`
38+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoCompact) instead.
39+
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
3940
OpenMetricsType = `application/openmetrics-text`
4041
OpenMetricsVersion_0_0_1 = "0.0.1"
4142
OpenMetricsVersion_1_0_0 = "1.0.0"
4243

43-
// The Content-Type values for the different wire protocols. Note that these
44-
// values are now unexported. If code was relying on comparisons to these
45-
// constants, instead use FormatType().
46-
fmtUnknown Format = `<unknown>`
47-
fmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
48-
fmtProtoDelim Format = protoFmt + ` encoding=delimited`
49-
fmtProtoText Format = protoFmt + ` encoding=text`
50-
fmtProtoCompact Format = protoFmt + ` encoding=compact-text`
51-
fmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
52-
fmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
44+
// The Content-Type values for the different wire protocols. Do not do direct
45+
// comparisons to these constants, instead use the comparison functions.
46+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeUnknown) instead.
47+
FmtUnknown Format = `<unknown>`
48+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeTextPlain) instead.
49+
FmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
50+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoDelim) instead.
51+
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
52+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoText) instead.
53+
FmtProtoText Format = ProtoFmt + ` encoding=text`
54+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoCompact) instead.
55+
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
56+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeOpenMetrics) instead.
57+
FmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
58+
// Deprecated: Use expfmt.NewFormat(expfmt.TypeOpenMetrics) instead.
59+
FmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
5360
)
5461

5562
const (
@@ -79,30 +86,30 @@ const (
7986
func NewFormat(t FormatType) Format {
8087
switch t {
8188
case TypeProtoCompact:
82-
return fmtProtoCompact
89+
return FmtProtoCompact
8390
case TypeProtoDelim:
84-
return fmtProtoDelim
91+
return FmtProtoDelim
8592
case TypeProtoText:
86-
return fmtProtoText
93+
return FmtProtoText
8794
case TypeTextPlain:
88-
return fmtText
95+
return FmtText
8996
case TypeOpenMetrics:
90-
return fmtOpenMetrics_1_0_0
97+
return FmtOpenMetrics_1_0_0
9198
default:
92-
return fmtUnknown
99+
return FmtUnknown
93100
}
94101
}
95102

96103
// NewOpenMetricsFormat generates a new OpenMetrics format matching the
97104
// specified version number.
98105
func NewOpenMetricsFormat(version string) (Format, error) {
99106
if version == OpenMetricsVersion_0_0_1 {
100-
return fmtOpenMetrics_0_0_1, nil
107+
return FmtOpenMetrics_0_0_1, nil
101108
}
102109
if version == OpenMetricsVersion_1_0_0 {
103-
return fmtOpenMetrics_1_0_0, nil
110+
return FmtOpenMetrics_1_0_0, nil
104111
}
105-
return fmtUnknown, fmt.Errorf("unknown open metrics version string")
112+
return FmtUnknown, fmt.Errorf("unknown open metrics version string")
106113
}
107114

108115
// FormatType deduces an overall FormatType for the given format.

expfmt/expfmt_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ func TestToFormatType(t *testing.T) {
2828
expected FormatType
2929
}{
3030
{
31-
format: fmtProtoCompact,
31+
format: FmtProtoCompact,
3232
expected: TypeProtoCompact,
3333
},
3434
{
35-
format: fmtProtoDelim,
35+
format: FmtProtoDelim,
3636
expected: TypeProtoDelim,
3737
},
3838
{
39-
format: fmtProtoText,
39+
format: FmtProtoText,
4040
expected: TypeProtoText,
4141
},
4242
{
43-
format: fmtOpenMetrics_1_0_0,
43+
format: FmtOpenMetrics_1_0_0,
4444
expected: TypeOpenMetrics,
4545
},
4646
{
47-
format: fmtText,
47+
format: FmtText,
4848
expected: TypeTextPlain,
4949
},
5050
{
51-
format: fmtOpenMetrics_0_0_1,
51+
format: FmtOpenMetrics_0_0_1,
5252
expected: TypeOpenMetrics,
5353
},
5454
{
@@ -104,7 +104,7 @@ func TestToEscapingScheme(t *testing.T) {
104104
expected model.EscapingScheme
105105
}{
106106
{
107-
format: fmtProtoCompact,
107+
format: FmtProtoCompact,
108108
expected: model.ValueEncodingEscaping,
109109
},
110110
{

0 commit comments

Comments
 (0)