Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e08135a

Browse files
committed
expfmt: Add a way to generate different OpenMetrics Formats
Also complete test coverage of expfmt.go Signed-off-by: Owen Williams <[email protected]>
1 parent 36d0bf9 commit e08135a

File tree

2 files changed

+132
-6
lines changed

2 files changed

+132
-6
lines changed

expfmt/expfmt.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package expfmt
1616

1717
import (
18+
"fmt"
1819
"strings"
1920

2021
"github.com/prometheus/common/model"
@@ -63,7 +64,7 @@ const (
6364
type FormatType int
6465

6566
const (
66-
TypeUnknown = iota
67+
TypeUnknown FormatType = iota
6768
TypeProtoCompact
6869
TypeProtoDelim
6970
TypeProtoText
@@ -73,7 +74,8 @@ const (
7374

7475
// NewFormat generates a new Format from the type provided. Mostly used for
7576
// tests, most Formats should be generated as part of content negotiation in
76-
// encode.go.
77+
// encode.go. If a type has more than one version, the latest version will be
78+
// returned.
7779
func NewFormat(t FormatType) Format {
7880
switch t {
7981
case TypeProtoCompact:
@@ -91,13 +93,21 @@ func NewFormat(t FormatType) Format {
9193
}
9294
}
9395

96+
// NewOpenMetricsFormat generates a new OpenMetrics format matching the
97+
// specified version number.
98+
func NewOpenMetricsFormat(version string) (Format, error) {
99+
if version == OpenMetricsVersion_0_0_1 {
100+
return fmtOpenMetrics_0_0_1, nil
101+
}
102+
if version == OpenMetricsVersion_1_0_0 {
103+
return fmtOpenMetrics_1_0_0, nil
104+
}
105+
return fmtUnknown, fmt.Errorf("unknown open metrics version string")
106+
}
107+
94108
// FormatType deduces an overall FormatType for the given format.
95109
func (f Format) FormatType() FormatType {
96110
toks := strings.Split(string(f), ";")
97-
if len(toks) < 2 {
98-
return TypeUnknown
99-
}
100-
101111
params := make(map[string]string)
102112
for i, t := range toks {
103113
if i == 0 {

expfmt/expfmt_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package expfmt
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prometheus/common/model"
7+
)
8+
9+
// Test Format to Escapting Scheme conversion
10+
// Path: expfmt/expfmt_test.go
11+
// Compare this snippet from expfmt/expfmt.go:
12+
func TestToFormatType(t *testing.T) {
13+
tests := []struct {
14+
format Format
15+
expected FormatType
16+
}{
17+
{
18+
format: fmtProtoCompact,
19+
expected: TypeProtoCompact,
20+
},
21+
{
22+
format: fmtProtoDelim,
23+
expected: TypeProtoDelim,
24+
},
25+
{
26+
format: fmtProtoText,
27+
expected: TypeProtoText,
28+
},
29+
{
30+
format: fmtOpenMetrics_1_0_0,
31+
expected: TypeOpenMetrics,
32+
},
33+
{
34+
format: fmtText,
35+
expected: TypeTextPlain,
36+
},
37+
{
38+
format: fmtOpenMetrics_0_0_1,
39+
expected: TypeOpenMetrics,
40+
},
41+
{
42+
format: "application/vnd.google.protobuf; proto=BadProtocol; encoding=text",
43+
expected: TypeUnknown,
44+
},
45+
{
46+
format: "application/vnd.google.protobuf",
47+
expected: TypeUnknown,
48+
},
49+
{
50+
format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily=bad",
51+
expected: TypeUnknown,
52+
},
53+
// encoding missing
54+
{
55+
format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily",
56+
expected: TypeUnknown,
57+
},
58+
// invalid encoding
59+
{
60+
format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=textual",
61+
expected: TypeUnknown,
62+
},
63+
// bad charset, must be utf-8
64+
{
65+
format: "application/openmetrics-text; version=1.0.0; charset=ascii",
66+
expected: TypeUnknown,
67+
},
68+
{
69+
format: "text/plain",
70+
expected: TypeTextPlain,
71+
},
72+
{
73+
format: "text/plain; version=invalid",
74+
expected: TypeUnknown,
75+
},
76+
{
77+
format: "gobbledygook",
78+
expected: TypeUnknown,
79+
},
80+
}
81+
for _, test := range tests {
82+
if test.format.FormatType() != test.expected {
83+
t.Errorf("expected %v got %v", test.expected, test.format.FormatType())
84+
}
85+
}
86+
}
87+
88+
func TestToEscapingScheme(t *testing.T) {
89+
tests := []struct {
90+
format Format
91+
expected model.EscapingScheme
92+
}{
93+
{
94+
format: fmtProtoCompact,
95+
expected: model.ValueEncodingEscaping,
96+
},
97+
{
98+
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores",
99+
expected: model.UnderscoreEscaping,
100+
},
101+
{
102+
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
103+
expected: model.NoEscaping,
104+
},
105+
// error returns default
106+
{
107+
format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=invalid",
108+
expected: model.NameEscapingScheme,
109+
},
110+
}
111+
for _, test := range tests {
112+
if test.format.ToEscapingScheme() != test.expected {
113+
t.Errorf("expected %v got %v", test.expected, test.format.ToEscapingScheme())
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)