Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: prometheus/common
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.58.0
Choose a base ref
...
head repository: prometheus/common
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.59.0
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Sep 4, 2024

  1. expfmt: Add WithEscapingScheme to help construct Formats (#688)

    expfmt: Add WithEscapingScheme to help construct Formats
    
    Signed-off-by: Owen Williams <owen.williams@grafana.com>
    
    ---------
    
    Signed-off-by: Owen Williams <owen.williams@grafana.com>
    ywwg authored Sep 4, 2024

    Verified

    This commit was created on github.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4f8e8f4 View commit details
  2. Change the default escape method to UnderscoreEscaping

    fixes #689
    
    Signed-off-by: Owen Williams <owen.williams@grafana.com>
    ywwg committed Sep 4, 2024
    Copy the full SHA
    01da226 View commit details

Commits on Sep 5, 2024

  1. Merge pull request #690 from ywwg/owilliams/default-escaping

    Change the default escape method to UnderscoreEscaping
    ywwg authored Sep 5, 2024

    Verified

    This commit was created on github.com and signed with GitHub’s verified signature.
    Copy the full SHA
    bf4843e View commit details
Showing with 77 additions and 16 deletions.
  1. +3 −3 expfmt/encode_test.go
  2. +23 −0 expfmt/expfmt.go
  3. +44 −9 expfmt/expfmt_test.go
  4. +7 −4 model/metric.go
6 changes: 3 additions & 3 deletions expfmt/encode_test.go
Original file line number Diff line number Diff line change
@@ -383,9 +383,9 @@ func TestEscapedEncode(t *testing.T) {
t.Errorf("expected the output bytes buffer to be non-empty")
}

expected := `# TYPE U__foo_2e_metric untyped
U__foo_2e_metric 1.234
U__foo_2e_metric{U__dotted_2e_label_2e_name="my.label.value"} 8
expected := `# TYPE foo_metric untyped
foo_metric 1.234
foo_metric{dotted_label_name="my.label.value"} 8
`

if string(out) != expected {
23 changes: 23 additions & 0 deletions expfmt/expfmt.go
Original file line number Diff line number Diff line change
@@ -112,6 +112,29 @@ func NewOpenMetricsFormat(version string) (Format, error) {
return FmtUnknown, fmt.Errorf("unknown open metrics version string")
}

// WithEscapingScheme returns a copy of Format with the specified escaping
// scheme appended to the end. If an escaping scheme already exists it is
// removed.
func (f Format) WithEscapingScheme(s model.EscapingScheme) Format {
var terms []string
for _, p := range strings.Split(string(f), ";") {
toks := strings.Split(p, "=")
if len(toks) != 2 {
trimmed := strings.TrimSpace(p)
if len(trimmed) > 0 {
terms = append(terms, trimmed)
}
continue
}
key := strings.TrimSpace(toks[0])
if key != model.EscapingKey {
terms = append(terms, strings.TrimSpace(p))
}
}
terms = append(terms, model.EscapingKey+"="+s.String())
return Format(strings.Join(terms, "; "))
}

// FormatType deduces an overall FormatType for the given format.
func (f Format) FormatType() FormatType {
toks := strings.Split(string(f), ";")
53 changes: 44 additions & 9 deletions expfmt/expfmt_test.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ import (
"testing"

"github.com/prometheus/common/model"

"github.com/stretchr/testify/require"
)

// Test Format to Escapting Scheme conversion
@@ -92,9 +94,7 @@ func TestToFormatType(t *testing.T) {
},
}
for _, test := range tests {
if test.format.FormatType() != test.expected {
t.Errorf("expected %v got %v", test.expected, test.format.FormatType())
}
require.Equal(t, test.expected, test.format.FormatType())
}
}

@@ -105,11 +105,11 @@ func TestToEscapingScheme(t *testing.T) {
}{
{
format: FmtProtoCompact,
expected: model.ValueEncodingEscaping,
expected: model.UnderscoreEscaping,
},
{
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores",
expected: model.UnderscoreEscaping,
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=dots",
expected: model.DotsEscaping,
},
{
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
@@ -122,8 +122,43 @@ func TestToEscapingScheme(t *testing.T) {
},
}
for _, test := range tests {
if test.format.ToEscapingScheme() != test.expected {
t.Errorf("expected %v got %v", test.expected, test.format.ToEscapingScheme())
}
require.Equal(t, test.expected, test.format.ToEscapingScheme())
}
}

func TestWithEscapingScheme(t *testing.T) {
tests := []struct {
name string
format Format
scheme model.EscapingScheme
expected string
}{
{
name: "no existing term, append one",
format: FmtProtoCompact,
scheme: model.DotsEscaping,
expected: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text; escaping=dots",
},
{
name: "existing term at end, replace",
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores",
scheme: model.ValueEncodingEscaping,
expected: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=values",
},
{
name: "existing term in middle, replace",
format: "application/openmetrics-text; escaping=dots; version=1.0.0; charset=utf-8; ",
scheme: model.NoEscaping,
expected: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
},
{
name: "multiple existing terms removed",
format: "application/openmetrics-text; escaping=dots; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
scheme: model.ValueEncodingEscaping,
expected: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=values",
},
}
for _, test := range tests {
require.Equal(t, test.expected, string(test.format.WithEscapingScheme(test.scheme)))
}
}
11 changes: 7 additions & 4 deletions model/metric.go
Original file line number Diff line number Diff line change
@@ -34,10 +34,13 @@ var (
// goroutines are started.
NameValidationScheme = LegacyValidation

// NameEscapingScheme defines the default way that names will be
// escaped when presented to systems that do not support UTF-8 names. If the
// Content-Type "escaping" term is specified, that will override this value.
NameEscapingScheme = ValueEncodingEscaping
// NameEscapingScheme defines the default way that names will be escaped when
// presented to systems that do not support UTF-8 names. If the Content-Type
// "escaping" term is specified, that will override this value.
// NameEscapingScheme should not be set to the NoEscaping value. That string
// is used in content negotiation to indicate that a system supports UTF-8 and
// has that feature enabled.
NameEscapingScheme = UnderscoreEscaping
)

// ValidationScheme is a Go enum for determining how metric and label names will