Skip to content

Commit ed30339

Browse files
Address feedback
Address code review comments. Signed-off-by: martincostello <martin@martincostello.com>
1 parent 3349589 commit ed30339

5 files changed

Lines changed: 296 additions & 41 deletions

File tree

expfmt/decode.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func ResponseFormat(h http.Header) Format {
8484

8585
// NewDecoder returns a new decoder based on the given input format. Metric
8686
// names are validated based on the provided Format -- if the format requires
87-
// escaping, raditional Prometheues validity checking is used. Otherwise, names
87+
// escaping, traditional Prometheus validity checking is used. Otherwise, names
8888
// are checked for UTF-8 validity. Supported formats include delimited protobuf
8989
// and the Prometheus/OpenMetrics text formats. For historical reasons, this
9090
// decoder fallbacks to classic text decoding for any other format. This decoder
@@ -100,7 +100,7 @@ func NewDecoder(r io.Reader, format Format) Decoder {
100100
case TypeProtoDelim:
101101
return &protoDecoder{r: bufio.NewReader(r), s: scheme}
102102
case TypeOpenMetrics:
103-
return &openMetricsDecoder{r: r}
103+
return &openMetricsDecoder{r: r, s: scheme}
104104
case TypeProtoText, TypeProtoCompact:
105105
return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
106106
}
@@ -156,14 +156,15 @@ func (d *errDecoder) Decode(*dto.MetricFamily) error {
156156
type openMetricsDecoder struct {
157157
r io.Reader
158158
fams map[string]*dto.MetricFamily
159+
s model.ValidationScheme
159160
err error
160161
}
161162

162163
// Decode implements the Decoder interface.
163164
func (d *openMetricsDecoder) Decode(mf *dto.MetricFamily) error {
164165
if d.err == nil {
165166
// Read all metrics in one shot.
166-
var p OpenMetricsParser
167+
p := OpenMetricsParser{scheme: d.s}
167168
d.fams, d.err = p.OpenMetricsToMetricFamilies(d.r)
168169
// If we don't get an error, store io.EOF for the end.
169170
if d.err == nil {

expfmt/decode_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ metric1_total 4
171171
require.Truef(t, reflect.DeepEqual(all, out), "output does not match")
172172
}
173173

174+
func TestOpenMetricsDecoderWithUTF8Names(t *testing.T) {
175+
dec := NewDecoder(
176+
strings.NewReader(`# TYPE "métric" gauge
177+
{"métric","labél"="value"} 1
178+
# EOF
179+
`),
180+
FmtOpenMetrics_1_0_0.WithEscapingScheme(model.NoEscaping),
181+
)
182+
183+
var mf dto.MetricFamily
184+
require.NoError(t, dec.Decode(&mf))
185+
require.Equal(t, "métric", mf.GetName())
186+
require.Len(t, mf.GetMetric(), 1)
187+
require.Len(t, mf.GetMetric()[0].GetLabel(), 1)
188+
require.Equal(t, "labél", mf.GetMetric()[0].GetLabel()[0].GetName())
189+
}
190+
174191
func TestProtoDecoder(t *testing.T) {
175192
testTime := model.Now()
176193

0 commit comments

Comments
 (0)