Skip to content

Commit 51a6ef4

Browse files
committed
Improve tests, adjust sample metric name logic, improve variable naming and tidy up comments
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 46862a5 commit 51a6ef4

File tree

2 files changed

+52
-81
lines changed

2 files changed

+52
-81
lines changed

expfmt/encode_test.go

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func TestEncode(t *testing.T) {
107107
metric1 := &dto.MetricFamily{
108108
Name: proto.String("foo_metric"),
109109
Type: dto.MetricType_UNTYPED.Enum(),
110+
Unit: proto.String("seconds"),
110111
Metric: []*dto.Metric{
111112
{
112113
Untyped: &dto.Untyped{
@@ -126,19 +127,16 @@ func TestEncode(t *testing.T) {
126127
{
127128
metric: metric1,
128129
format: FmtProtoDelim,
129-
// expOut: `\x1b\n\nfoo_metric\x18\x03\"\v*\t\tX9\xb4\xc8v\xbe\xf3?`, // does this look like the desired output?
130130
},
131131
// 2: Untyped FmtProtoCompact
132132
{
133133
metric: metric1,
134134
format: FmtProtoCompact,
135-
// expOut: `name:\"foo_metric\" type:UNTYPED metric:{untyped:{value:1.234}}\n`, // does this look like the desired output?
136135
},
137136
// 3: Untyped FmtProtoText
138137
{
139138
metric: metric1,
140139
format: FmtProtoText,
141-
// expOut: `name: \"foo_metric\"\ntype: UNTYPED\nmetric: {\n untyped: {\n value: 1.234\n }\n}\n\n`, // does this look like the desired output?
142140
},
143141
// 4: Untyped FmtText
144142
{
@@ -166,45 +164,20 @@ foo_metric 1.234
166164
},
167165
// 7: Simple Counter FmtOpenMetrics_0_0_1 unit opted in
168166
{
169-
metric: &dto.MetricFamily{
170-
Name: proto.String("foos_duration_seconds_total"),
171-
Help: proto.String("Duration of foos in seconds."),
172-
Type: dto.MetricType_COUNTER.Enum(),
173-
Unit: proto.String("seconds"),
174-
Metric: []*dto.Metric{
175-
{
176-
Counter: &dto.Counter{
177-
Value: proto.Float64(420),
178-
},
179-
},
180-
},
181-
},
167+
metric: metric1,
182168
format: FmtOpenMetrics_0_0_1,
183169
withUnit: true,
184-
expOut: `# HELP foos_duration_seconds Duration of foos in seconds.
185-
# TYPE foos_duration_seconds counter
186-
# UNIT foos_duration_seconds seconds
187-
foos_duration_seconds_total 420.0
170+
expOut: `# TYPE foo_metric_seconds unknown
171+
# UNIT foo_metric_seconds seconds
172+
foo_metric_seconds 1.234
188173
`,
189174
},
190175
// 8: Simple Counter FmtOpenMetrics_1_0_0 unit opted out
191176
{
192-
metric: &dto.MetricFamily{
193-
Name: proto.String("foos_duration_seconds_total"),
194-
Help: proto.String("Duration of foos in seconds."),
195-
Type: dto.MetricType_COUNTER.Enum(),
196-
Metric: []*dto.Metric{
197-
{
198-
Counter: &dto.Counter{
199-
Value: proto.Float64(420),
200-
},
201-
},
202-
},
203-
},
177+
metric: metric1,
204178
format: FmtOpenMetrics_1_0_0,
205-
expOut: `# HELP foos_duration_seconds Duration of foos in seconds.
206-
# TYPE foos_duration_seconds counter
207-
foos_duration_seconds_total 420.0
179+
expOut: `# TYPE foo_metric unknown
180+
foo_metric 1.234
208181
`,
209182
},
210183
}
@@ -220,22 +193,20 @@ foos_duration_seconds_total 420.0
220193
t.Errorf("%d. error: %s", i, err)
221194
continue
222195
}
223-
// N.B. For the first three cases, I'm maintaining the same checks as they were before, i. e. the expected output is
224-
// not declared ergo no comparison with the actual output is ever performed, expect for its length being bigger than zero.
225-
if i > 2 {
226-
if expected, got := len(scenario.expOut), len(out.Bytes()); expected != got {
227-
t.Errorf(
228-
"%d. expected %d bytes written, got %d",
229-
i, expected, got,
230-
)
231-
}
232-
if expected, got := scenario.expOut, out.String(); expected != got {
233-
t.Errorf(
234-
"%d. expected out=%q, got %q",
235-
i, expected, got,
236-
)
237-
}
196+
197+
if expected, got := len(scenario.expOut), len(out.Bytes()); expected != 0 && expected != got {
198+
t.Errorf(
199+
"%d. expected %d bytes written, got %d",
200+
i, expected, got,
201+
)
238202
}
203+
if expected, got := scenario.expOut, out.String(); expected != "" && expected != got {
204+
t.Errorf(
205+
"%d. expected out=%q, got %q",
206+
i, expected, got,
207+
)
208+
}
209+
239210
if len(out.Bytes()) == 0 {
240211
t.Errorf(
241212
"%d. expected output not to be empty",

expfmt/openmetrics_create.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type ToOpenMetrics struct {
3535
type ToOpenMetricsOption = func(*ToOpenMetrics)
3636

3737
// ToOpenMetricsWithUnit is meant to be called as an optional argument in the MetricFamilyToOpenMetrics
38-
// function. It enables a set unit to be written to the output and to be added to the metric name, (if
39-
// it's not there already), as a suffix. Without opting in this way, the unit will not be added to the
38+
// function. It enables a set unit to be written to the output and to be added to the metric name, if
39+
// it's not there already, as a suffix. Without opting in this way, the unit will not be added to the
4040
// metric name and, on top of that, the unit will not be passed onto the output, even if it were declared
4141
// in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
4242
func ToOpenMetricsWithUnit() ToOpenMetricsOption {
@@ -114,15 +114,15 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
114114
}
115115

116116
var (
117-
n int
118-
metricType = in.GetType()
119-
shortName = name
117+
n int
118+
metricType = in.GetType()
119+
compliantName = name
120120
)
121-
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(shortName, "_total") {
122-
shortName = name[:len(name)-6]
121+
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(compliantName, "_total") {
122+
compliantName = name[:len(name)-6]
123123
}
124-
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(shortName, fmt.Sprintf("_%s", *in.Unit)) {
125-
shortName = shortName + fmt.Sprintf("_%s", *in.Unit)
124+
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, fmt.Sprintf("_%s", *in.Unit)) {
125+
compliantName = compliantName + fmt.Sprintf("_%s", *in.Unit)
126126
}
127127

128128
// Comments, first HELP, then TYPE.
@@ -132,7 +132,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
132132
if err != nil {
133133
return
134134
}
135-
n, err = w.WriteString(shortName)
135+
n, err = w.WriteString(compliantName)
136136
written += n
137137
if err != nil {
138138
return
@@ -158,7 +158,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
158158
if err != nil {
159159
return
160160
}
161-
n, err = w.WriteString(shortName)
161+
n, err = w.WriteString(compliantName)
162162
written += n
163163
if err != nil {
164164
return
@@ -191,7 +191,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
191191
if err != nil {
192192
return
193193
}
194-
n, err = w.WriteString(shortName)
194+
n, err = w.WriteString(compliantName)
195195
written += n
196196
if err != nil {
197197
return
@@ -218,50 +218,50 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
218218
for _, metric := range in.Metric {
219219
switch metricType {
220220
case dto.MetricType_COUNTER:
221+
if strings.HasSuffix(name, "_total") {
222+
compliantName = compliantName + "_total"
223+
}
221224
if metric.Counter == nil {
222225
return written, fmt.Errorf(
223-
"expected counter in metric %s %s", name, metric,
226+
"expected counter in metric %s %s", compliantName, metric,
224227
)
225228
}
226-
// Note that we have ensured above that either the name
227-
// ends on `_total` or that the rendered type is
228-
// `unknown`. Therefore, no `_total` must be added here.
229229
n, err = writeOpenMetricsSample(
230-
w, name, "", metric, "", 0,
230+
w, compliantName, "", metric, "", 0,
231231
metric.Counter.GetValue(), 0, false,
232232
metric.Counter.Exemplar,
233233
)
234234
case dto.MetricType_GAUGE:
235235
if metric.Gauge == nil {
236236
return written, fmt.Errorf(
237-
"expected gauge in metric %s %s", name, metric,
237+
"expected gauge in metric %s %s", compliantName, metric,
238238
)
239239
}
240240
n, err = writeOpenMetricsSample(
241-
w, name, "", metric, "", 0,
241+
w, compliantName, "", metric, "", 0,
242242
metric.Gauge.GetValue(), 0, false,
243243
nil,
244244
)
245245
case dto.MetricType_UNTYPED:
246246
if metric.Untyped == nil {
247247
return written, fmt.Errorf(
248-
"expected untyped in metric %s %s", name, metric,
248+
"expected untyped in metric %s %s", compliantName, metric,
249249
)
250250
}
251251
n, err = writeOpenMetricsSample(
252-
w, name, "", metric, "", 0,
252+
w, compliantName, "", metric, "", 0,
253253
metric.Untyped.GetValue(), 0, false,
254254
nil,
255255
)
256256
case dto.MetricType_SUMMARY:
257257
if metric.Summary == nil {
258258
return written, fmt.Errorf(
259-
"expected summary in metric %s %s", name, metric,
259+
"expected summary in metric %s %s", compliantName, metric,
260260
)
261261
}
262262
for _, q := range metric.Summary.Quantile {
263263
n, err = writeOpenMetricsSample(
264-
w, name, "", metric,
264+
w, compliantName, "", metric,
265265
model.QuantileLabel, q.GetQuantile(),
266266
q.GetValue(), 0, false,
267267
nil,
@@ -272,7 +272,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
272272
}
273273
}
274274
n, err = writeOpenMetricsSample(
275-
w, name, "_sum", metric, "", 0,
275+
w, compliantName, "_sum", metric, "", 0,
276276
metric.Summary.GetSampleSum(), 0, false,
277277
nil,
278278
)
@@ -281,20 +281,20 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
281281
return
282282
}
283283
n, err = writeOpenMetricsSample(
284-
w, name, "_count", metric, "", 0,
284+
w, compliantName, "_count", metric, "", 0,
285285
0, metric.Summary.GetSampleCount(), true,
286286
nil,
287287
)
288288
case dto.MetricType_HISTOGRAM:
289289
if metric.Histogram == nil {
290290
return written, fmt.Errorf(
291-
"expected histogram in metric %s %s", name, metric,
291+
"expected histogram in metric %s %s", compliantName, metric,
292292
)
293293
}
294294
infSeen := false
295295
for _, b := range metric.Histogram.Bucket {
296296
n, err = writeOpenMetricsSample(
297-
w, name, "_bucket", metric,
297+
w, compliantName, "_bucket", metric,
298298
model.BucketLabel, b.GetUpperBound(),
299299
0, b.GetCumulativeCount(), true,
300300
b.Exemplar,
@@ -309,7 +309,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
309309
}
310310
if !infSeen {
311311
n, err = writeOpenMetricsSample(
312-
w, name, "_bucket", metric,
312+
w, compliantName, "_bucket", metric,
313313
model.BucketLabel, math.Inf(+1),
314314
0, metric.Histogram.GetSampleCount(), true,
315315
nil,
@@ -320,7 +320,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
320320
}
321321
}
322322
n, err = writeOpenMetricsSample(
323-
w, name, "_sum", metric, "", 0,
323+
w, compliantName, "_sum", metric, "", 0,
324324
metric.Histogram.GetSampleSum(), 0, false,
325325
nil,
326326
)
@@ -329,13 +329,13 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
329329
return
330330
}
331331
n, err = writeOpenMetricsSample(
332-
w, name, "_count", metric, "", 0,
332+
w, compliantName, "_count", metric, "", 0,
333333
0, metric.Histogram.GetSampleCount(), true,
334334
nil,
335335
)
336336
default:
337337
return written, fmt.Errorf(
338-
"unexpected type in metric %s %s", name, metric,
338+
"unexpected type in metric %s %s", compliantName, metric,
339339
)
340340
}
341341
written += n

0 commit comments

Comments
 (0)