Skip to content

Commit

Permalink
expfmt: Add WithEscapingScheme to help construct Formats (#688)
Browse files Browse the repository at this point in the history
expfmt: Add WithEscapingScheme to help construct Formats

Signed-off-by: Owen Williams <[email protected]>

---------

Signed-off-by: Owen Williams <[email protected]>
  • Loading branch information
ywwg committed Sep 4, 2024
1 parent b1880d0 commit 4f8e8f4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
23 changes: 23 additions & 0 deletions expfmt/expfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), ";")
Expand Down
47 changes: 41 additions & 6 deletions expfmt/expfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"testing"

"github.com/prometheus/common/model"

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

// Test Format to Escapting Scheme conversion
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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)))
}
}

0 comments on commit 4f8e8f4

Please sign in to comment.