Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: expfmt/text: optionally include empty MetricFamilies #706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions expfmt/text_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ var (
//
// This method fulfills the type 'prometheus.encoder'.
func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err error) {
// Fail-fast checks.
if len(in.Metric) == 0 {
return 0, fmt.Errorf("MetricFamily has no metrics: %s", in)
}
Comment on lines -79 to -82
Copy link
Author

@dgrisonnet dgrisonnet Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing because of this. I guess I will need to introduce a new function just to keep MetricFamilies without timeseries around

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or variadic option 🤔 or "both", so

NewMFConverter(opts).MetricFamilyToText

then keeping MetricFamilyToText as NewMFConverter(nil).MetricFamilyToText(....)

name := in.GetName()
if name == "" {
return 0, fmt.Errorf("MetricFamily has no name: %s", in)
Expand Down
15 changes: 12 additions & 3 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type TextParser struct {
// These indicate if the metric name from the current line being parsed is inside
// braces and if that metric name was found respectively.
currentMetricIsInsideBraces, currentMetricInsideBracesIsPresent bool
// Include all MetricFamilies when parsing an input, even the ones for which
// the metric is registered but it doesn't have any timeseries.
includeEmptyMetricFamilies bool
}

// TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
Expand Down Expand Up @@ -109,9 +112,11 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF
// Magic happens here...
}
// Get rid of empty metric families.
for k, mf := range p.metricFamiliesByName {
if len(mf.GetMetric()) == 0 {
delete(p.metricFamiliesByName, k)
if !p.includeEmptyMetricFamilies {
for k, mf := range p.metricFamiliesByName {
if len(mf.GetMetric()) == 0 {
delete(p.metricFamiliesByName, k)
}
}
}
// If p.err is io.EOF now, we have run into a premature end of the input
Expand All @@ -124,6 +129,10 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF
return p.metricFamiliesByName, p.err
}

func (p *TextParser) IncludeEmptyMetricFamilies(b bool) {
p.includeEmptyMetricFamilies = b
}

func (p *TextParser) reset(in io.Reader) {
p.metricFamiliesByName = map[string]*dto.MetricFamily{}
if p.buf == nil {
Expand Down