Skip to content

Commit a7244c2

Browse files
committed
expfmt/text: optionally include empty MetricFamilies
When parsing text format it is sometimes useful to keep all the MetricFamilies around, even the ones that don't have any timeseries. This is the case for the testutil library in client_golang where we need to distinguish registered metrics from erronous inputs. Signed-off-by: Damien Grisonnet <[email protected]>
1 parent dae848d commit a7244c2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

expfmt/text_parse.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type TextParser struct {
7878
// These indicate if the metric name from the current line being parsed is inside
7979
// braces and if that metric name was found respectively.
8080
currentMetricIsInsideBraces, currentMetricInsideBracesIsPresent bool
81+
// Include all MetricFamilies when parsing an input, even the ones for which
82+
// the metric is registered but it doesn't have any timeseries.
83+
includeEmptyMetricFamilies bool
8184
}
8285

8386
// TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
@@ -109,9 +112,11 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF
109112
// Magic happens here...
110113
}
111114
// Get rid of empty metric families.
112-
for k, mf := range p.metricFamiliesByName {
113-
if len(mf.GetMetric()) == 0 {
114-
delete(p.metricFamiliesByName, k)
115+
if !p.includeEmptyMetricFamilies {
116+
for k, mf := range p.metricFamiliesByName {
117+
if len(mf.GetMetric()) == 0 {
118+
delete(p.metricFamiliesByName, k)
119+
}
115120
}
116121
}
117122
// If p.err is io.EOF now, we have run into a premature end of the input
@@ -124,6 +129,10 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF
124129
return p.metricFamiliesByName, p.err
125130
}
126131

132+
func (p *TextParser) IncludeEmptyMetricFamilies(b bool) {
133+
p.includeEmptyMetricFamilies = b
134+
}
135+
127136
func (p *TextParser) reset(in io.Reader) {
128137
p.metricFamiliesByName = map[string]*dto.MetricFamily{}
129138
if p.buf == nil {

0 commit comments

Comments
 (0)