From 279f0763d6ac3a4deb498e4e9409080ca647405c Mon Sep 17 00:00:00 2001 From: SuperQ Date: Mon, 15 Jul 2024 18:36:38 +0200 Subject: [PATCH 1/2] Revert "Unexport Format strings" This reverts commit 05d7387306aee31a5a9776899b1baf2267667e81. Signed-off-by: SuperQ --- expfmt/decode.go | 14 +++++++------- expfmt/decode_test.go | 16 ++++++++-------- expfmt/encode.go | 24 ++++++++++++------------ expfmt/encode_test.go | 38 +++++++++++++++++++------------------- expfmt/expfmt.go | 39 +++++++++++++++++++-------------------- expfmt/expfmt_test.go | 14 +++++++------- 6 files changed, 72 insertions(+), 73 deletions(-) diff --git a/expfmt/decode.go b/expfmt/decode.go index 25cfaa21..1448439b 100644 --- a/expfmt/decode.go +++ b/expfmt/decode.go @@ -45,7 +45,7 @@ func ResponseFormat(h http.Header) Format { mediatype, params, err := mime.ParseMediaType(ct) if err != nil { - return fmtUnknown + return FmtUnknown } const textType = "text/plain" @@ -53,21 +53,21 @@ func ResponseFormat(h http.Header) Format { 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. diff --git a/expfmt/decode_test.go b/expfmt/decode_test.go index 19560ffc..1b261401 100644 --- a/expfmt/decode_test.go +++ b/expfmt/decode_test.go @@ -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 @@ -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, }, } @@ -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 { diff --git a/expfmt/encode.go b/expfmt/encode.go index ff5ef7a9..cf0c150c 100644 --- a/expfmt/encode.go +++ b/expfmt/encode.go @@ -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 @@ -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 diff --git a/expfmt/encode_test.go b/expfmt/encode_test.go index 448bda6a..5a462346 100644 --- a/expfmt/encode_test.go +++ b/expfmt/encode_test.go @@ -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 `, @@ -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(), @@ -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()) @@ -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()) @@ -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()) diff --git a/expfmt/expfmt.go b/expfmt/expfmt.go index 051b38cd..9e165801 100644 --- a/expfmt/expfmt.go +++ b/expfmt/expfmt.go @@ -35,21 +35,20 @@ const ( TextVersion = "0.0.4" ProtoType = `application/vnd.google.protobuf` ProtoProtocol = `io.prometheus.client.MetricFamily` - protoFmt = ProtoType + "; proto=" + ProtoProtocol + ";" + 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 = `` - 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. + FmtUnknown Format = `` + 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 ( @@ -79,17 +78,17 @@ 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 } } @@ -97,12 +96,12 @@ func NewFormat(t FormatType) Format { // 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. diff --git a/expfmt/expfmt_test.go b/expfmt/expfmt_test.go index 8ec16524..3a16f3b5 100644 --- a/expfmt/expfmt_test.go +++ b/expfmt/expfmt_test.go @@ -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, }, { @@ -104,7 +104,7 @@ func TestToEscapingScheme(t *testing.T) { expected model.EscapingScheme }{ { - format: fmtProtoCompact, + format: FmtProtoCompact, expected: model.ValueEncodingEscaping, }, { From c3e8247156eb95fd21b0d2e5bee633f39ec0fb82 Mon Sep 17 00:00:00 2001 From: SuperQ Date: Mon, 15 Jul 2024 18:42:57 +0200 Subject: [PATCH 2/2] Deprecate format strings Add deprecation notice to format strings to allow transition to new functionality. Signed-off-by: SuperQ --- expfmt/expfmt.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/expfmt/expfmt.go b/expfmt/expfmt.go index 9e165801..14034a67 100644 --- a/expfmt/expfmt.go +++ b/expfmt/expfmt.go @@ -32,9 +32,10 @@ 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` + 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" @@ -42,12 +43,19 @@ const ( // The Content-Type values for the different wire protocols. Do not do direct // comparisons to these constants, instead use the comparison functions. - FmtUnknown Format = `` - 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` + // Deprecated: Use expfmt.NewFormat(expfmt.TypeUnknown) instead. + FmtUnknown Format = `` + // 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` )