-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Prometheus] Add unit tests for parsing metric families #36669
Conversation
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
@@ -351,7 +352,7 @@ func isBucket(name string) bool { | |||
return strings.HasSuffix(name, suffixBucket) | |||
} | |||
|
|||
func summaryMetricName(name string, s float64, qv string, lbls string, t *int64, summariesByName map[string]map[string]*OpenMetric) (string, *OpenMetric) { | |||
func summaryMetricName(name string, s float64, qv string, lbls string, summariesByName map[string]map[string]*OpenMetric) (string, *OpenMetric) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter t
wasn't being used, that is why it is removed.
@@ -653,8 +658,6 @@ func ParseMetricFamilies(b []byte, contentType string, ts time.Time) ([]*MetricF | |||
metricFamiliesByName[lookupMetricName] = fam | |||
} | |||
|
|||
fam.Name = &metricName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line needed to be removed, because it was causing errors.
Example: if we had this metric:
# HELP cc_seconds A counter
# TYPE cc_seconds counter
# UNIT cc_seconds seconds
cc_seconds_total 1.0
cc_seconds_created 123.456
The metric would be renamed to cc_seconds_total
and after to cc_seconds_created
. When in reality, the metric name is cc_seconds
(without the suffix). And then the metric.Metric
is a list of metrics, where both are included.
This test case is in the test file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm far from being an expert in this bit of the codebase, but LGTM
I will close this PR for now while we resolve the naming conflicts. |
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
// Remove the two possible suffixes, _created and _total | ||
if isTotal(metricName) { | ||
lookupMetricName = strings.TrimSuffix(metricName, suffixTotal) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else { | |
} isCreated(metricName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of this condition above if contentType == OpenMetricsType && !isTotal(metricName) && !isCreated(metricName)
we already know that if we are in the else
, the suffix is _created
@gizas
@@ -97,8 +97,8 @@ metrics_one_count_total{name="john",surname="williams"} 2 | |||
metrics_one_count_total{name="jahn",surname="baldwin",age="30"} 3 | |||
` | |||
|
|||
openMetricsCounterKeyLabelWithNaNInf = `# TYPE metrics_one_count_errors counter | |||
metrics_one_count_errors{name="jane",surname="foster"} 1 | |||
openMetricsCounterKeyLabelWithNaNInf = `# TYPE metrics_one_count_errors_total counter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to this, counter types MUST have the suffix _total
. I had to fix this to pass the test.
The changes on OpenMetrics sample files are the result from running |
# TYPE metric_info info | ||
metric_info 2 | ||
# TYPE metric_without_suffix info | ||
metric_without_suffix 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dropped right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
require.ElementsMatch(t, expected, result) | ||
} | ||
|
||
func TestHistogramPrometheus(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dummy question: Why we need both this test and TestHistogramOpenMetrics?
When this will be called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For open metrics, we use a parser that is different than the one we use for prometheus metrics. That is decided by the content we give to the parse metrics function. So we are just checking the expected results for the two possible parsers: the PromParser
and the OpenMetricsParser
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constanca make sure that the link https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md
is added in the description of this PR, to have it handy for future reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes lgtm.
However since this changes significantly the core functionality I would feel more confident if we had a manual testing phase in place as a double check.
* Add tests for parsing metrics. * Remove _info suffix. * Add unit tests * Add unit tests * Add unit tests * Add license header * Fix goimports. * Fix openmetrics tests.
What does this PR do?
The previous naming we were using for metrics, especially for open metrics, had a few problems. Since there were no unit tests checking the way we were parsing the metrics, we did not see this before. So this PR does 2 things:
The specification for OpenMetrics can be found here.
Naming: metric family name and metrics names
For OpenMetric Parser (see suffixes here):
_total
or_created
, then the metric is ignored. These are the only possible options.For Prometheus Parser:
Please check the
textparse_test
file to see clear examples of this.Related issues
Relates to #36537.