Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #576 and add deprecation notice #665

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions expfmt/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ func ResponseFormat(h http.Header) Format {

mediatype, params, err := mime.ParseMediaType(ct)
if err != nil {
return fmtUnknown
return FmtUnknown
}

const textType = "text/plain"

switch mediatype {
case ProtoType:
if p, ok := params["proto"]; ok && p != ProtoProtocol {
return fmtUnknown
return FmtUnknown
}
if e, ok := params["encoding"]; ok && e != "delimited" {
return fmtUnknown
return FmtUnknown
}
return fmtProtoDelim
return FmtProtoDelim

case textType:
if v, ok := params["version"]; ok && v != TextVersion {
return fmtUnknown
return FmtUnknown
}
return fmtText
return FmtText
}

return fmtUnknown
return FmtUnknown
}

// NewDecoder returns a new decoder based on the given input format.
Expand Down
16 changes: 8 additions & 8 deletions expfmt/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestProtoMultiMessageDecoder(t *testing.T) {
}

buf := bytes.NewReader(data)
decoder := NewDecoder(buf, fmtProtoDelim)
decoder := NewDecoder(buf, FmtProtoDelim)
var metrics []*dto.MetricFamily
for {
var mf dto.MetricFamily
Expand All @@ -448,27 +448,27 @@ func testDiscriminatorHTTPHeader(t testing.TB) {
}{
{
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="delimited"`},
output: fmtProtoDelim,
output: FmtProtoDelim,
},
{
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="illegal"; encoding="delimited"`},
output: fmtUnknown,
output: FmtUnknown,
},
{
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="illegal"`},
output: fmtUnknown,
output: FmtUnknown,
},
{
input: map[string]string{"Content-Type": `text/plain; version=0.0.4`},
output: fmtText,
output: FmtText,
},
{
input: map[string]string{"Content-Type": `text/plain`},
output: fmtText,
output: FmtText,
},
{
input: map[string]string{"Content-Type": `text/plain; version=0.0.3`},
output: fmtUnknown,
output: FmtUnknown,
},
}

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

var decoded bool
r := bufio.NewReader(strings.NewReader(example))
dec := NewDecoder(r, fmtText)
dec := NewDecoder(r, FmtText)
for {
var mf dto.MetricFamily
if err := dec.Decode(&mf); err != nil {
Expand Down
24 changes: 12 additions & 12 deletions expfmt/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ func Negotiate(h http.Header) Format {
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
switch ac.Params["encoding"] {
case "delimited":
return fmtProtoDelim + escapingScheme
return FmtProtoDelim + escapingScheme
case "text":
return fmtProtoText + escapingScheme
return FmtProtoText + escapingScheme
case "compact-text":
return fmtProtoCompact + escapingScheme
return FmtProtoCompact + escapingScheme
}
}
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
return fmtText + escapingScheme
return FmtText + escapingScheme
}
}
return fmtText + escapingScheme
return FmtText + escapingScheme
}

// NegotiateIncludingOpenMetrics works like Negotiate but includes
Expand All @@ -110,26 +110,26 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
switch ac.Params["encoding"] {
case "delimited":
return fmtProtoDelim + escapingScheme
return FmtProtoDelim + escapingScheme
case "text":
return fmtProtoText + escapingScheme
return FmtProtoText + escapingScheme
case "compact-text":
return fmtProtoCompact + escapingScheme
return FmtProtoCompact + escapingScheme
}
}
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
return fmtText + escapingScheme
return FmtText + escapingScheme
}
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
switch ver {
case OpenMetricsVersion_1_0_0:
return fmtOpenMetrics_1_0_0 + escapingScheme
return FmtOpenMetrics_1_0_0 + escapingScheme
default:
return fmtOpenMetrics_0_0_1 + escapingScheme
return FmtOpenMetrics_0_0_1 + escapingScheme
}
}
}
return fmtText + escapingScheme
return FmtText + escapingScheme
}

// NewEncoder returns a new encoder based on content type negotiation. All
Expand Down
38 changes: 19 additions & 19 deletions expfmt/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,56 +222,56 @@ func TestEncode(t *testing.T) {
// 1: Untyped ProtoDelim
{
metric: metric1,
format: fmtProtoDelim,
format: FmtProtoDelim,
},
// 2: Untyped fmtProtoCompact
// 2: Untyped FmtProtoCompact
{
metric: metric1,
format: fmtProtoCompact,
format: FmtProtoCompact,
},
// 3: Untyped fmtProtoText
// 3: Untyped FmtProtoText
{
metric: metric1,
format: fmtProtoText,
format: FmtProtoText,
},
// 4: Untyped fmtText
// 4: Untyped FmtText
{
metric: metric1,
format: fmtText,
format: FmtText,
expOut: `# TYPE foo_metric untyped
foo_metric 1.234
`,
},
// 5: Untyped fmtOpenMetrics_0_0_1
// 5: Untyped FmtOpenMetrics_0_0_1
{
metric: metric1,
format: fmtOpenMetrics_0_0_1,
format: FmtOpenMetrics_0_0_1,
expOut: `# TYPE foo_metric unknown
foo_metric 1.234
`,
},
// 6: Untyped fmtOpenMetrics_1_0_0
// 6: Untyped FmtOpenMetrics_1_0_0
{
metric: metric1,
format: fmtOpenMetrics_1_0_0,
format: FmtOpenMetrics_1_0_0,
expOut: `# TYPE foo_metric unknown
foo_metric 1.234
`,
},
// 7: Simple Counter fmtOpenMetrics_0_0_1 unit opted in
// 7: Simple Counter FmtOpenMetrics_0_0_1 unit opted in
{
metric: metric1,
format: fmtOpenMetrics_0_0_1,
format: FmtOpenMetrics_0_0_1,
options: []EncoderOption{WithUnit()},
expOut: `# TYPE foo_metric_seconds unknown
# UNIT foo_metric_seconds seconds
foo_metric_seconds 1.234
`,
},
// 8: Simple Counter fmtOpenMetrics_1_0_0 unit opted out
// 8: Simple Counter FmtOpenMetrics_1_0_0 unit opted out
{
metric: metric1,
format: fmtOpenMetrics_1_0_0,
format: FmtOpenMetrics_1_0_0,
expOut: `# TYPE foo_metric unknown
foo_metric 1.234
`,
Expand Down Expand Up @@ -310,7 +310,7 @@ foo_metric 1.234

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

buff.Reset()

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

buff.Reset()

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

buff.Reset()

textEncoder := NewEncoder(&buff, fmtText)
textEncoder := NewEncoder(&buff, FmtText)
err = textEncoder.Encode(metric)
if err != nil {
t.Errorf("unexpected error during encode: %s", err.Error())
Expand Down
53 changes: 30 additions & 23 deletions expfmt/expfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,31 @@ type Format string
// it on the wire, new content-type strings will have to be agreed upon and
// added here.
const (
TextVersion = "0.0.4"
ProtoType = `application/vnd.google.protobuf`
ProtoProtocol = `io.prometheus.client.MetricFamily`
protoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
TextVersion = "0.0.4"
ProtoType = `application/vnd.google.protobuf`
ProtoProtocol = `io.prometheus.client.MetricFamily`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoCompact) instead.
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. Note that these
// values are now unexported. If code was relying on comparisons to these
// constants, instead use FormatType().
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`
// The Content-Type values for the different wire protocols. Do not do direct
// comparisons to these constants, instead use the comparison functions.
// Deprecated: Use expfmt.NewFormat(expfmt.TypeUnknown) instead.
FmtUnknown Format = `<unknown>`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeTextPlain) instead.
FmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoDelim) instead.
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoText) instead.
FmtProtoText Format = ProtoFmt + ` encoding=text`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeProtoCompact) instead.
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeOpenMetrics) instead.
FmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
// Deprecated: Use expfmt.NewFormat(expfmt.TypeOpenMetrics) instead.
FmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
)

const (
Expand Down Expand Up @@ -79,30 +86,30 @@ const (
func NewFormat(t FormatType) Format {
switch t {
case TypeProtoCompact:
return fmtProtoCompact
return FmtProtoCompact
case TypeProtoDelim:
return fmtProtoDelim
return FmtProtoDelim
case TypeProtoText:
return fmtProtoText
return FmtProtoText
case TypeTextPlain:
return fmtText
return FmtText
case TypeOpenMetrics:
return fmtOpenMetrics_1_0_0
return FmtOpenMetrics_1_0_0
default:
return fmtUnknown
return FmtUnknown
}
}

// NewOpenMetricsFormat generates a new OpenMetrics format matching the
// specified version number.
func NewOpenMetricsFormat(version string) (Format, error) {
if version == OpenMetricsVersion_0_0_1 {
return fmtOpenMetrics_0_0_1, nil
return FmtOpenMetrics_0_0_1, nil
}
if version == OpenMetricsVersion_1_0_0 {
return fmtOpenMetrics_1_0_0, nil
return FmtOpenMetrics_1_0_0, nil
}
return fmtUnknown, fmt.Errorf("unknown open metrics version string")
return FmtUnknown, fmt.Errorf("unknown open metrics version string")
}

// FormatType deduces an overall FormatType for the given format.
Expand Down
14 changes: 7 additions & 7 deletions expfmt/expfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ func TestToFormatType(t *testing.T) {
expected FormatType
}{
{
format: fmtProtoCompact,
format: FmtProtoCompact,
expected: TypeProtoCompact,
},
{
format: fmtProtoDelim,
format: FmtProtoDelim,
expected: TypeProtoDelim,
},
{
format: fmtProtoText,
format: FmtProtoText,
expected: TypeProtoText,
},
{
format: fmtOpenMetrics_1_0_0,
format: FmtOpenMetrics_1_0_0,
expected: TypeOpenMetrics,
},
{
format: fmtText,
format: FmtText,
expected: TypeTextPlain,
},
{
format: fmtOpenMetrics_0_0_1,
format: FmtOpenMetrics_0_0_1,
expected: TypeOpenMetrics,
},
{
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestToEscapingScheme(t *testing.T) {
expected model.EscapingScheme
}{
{
format: fmtProtoCompact,
format: FmtProtoCompact,
expected: model.ValueEncodingEscaping,
},
{
Expand Down