Skip to content

Commit

Permalink
chore: fix handling of metrics with created suffix (#30808)
Browse files Browse the repository at this point in the history
Description:
As discussed in the OTEL Prometheus working group, to fix this case we
check If the metricfamily (which we get from the metadata) name +
_created == the series name match. Then we treat it as created
timestamp.
Link to tracking Issue:
#30309

Testing: Reproduced based on the details provided in the issue, added a
test case covering the case.

Documentation:

---------

Co-authored-by: David Ashpole <[email protected]>
  • Loading branch information
jmichalek132 and dashpole authored Feb 13, 2024
1 parent 7838f06 commit 81cfc48
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
27 changes: 27 additions & 0 deletions .chloggen/jm-fix-handling-of-metrics-with-created-suffix-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/prometheusreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: prometheusreceiver fix translation of metrics with _created suffix

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [ 30309 ]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
4 changes: 2 additions & 2 deletions receiver/prometheusreceiver/internal/metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (mf *metricFamily) addSeries(seriesRef uint64, metricName string, ls labels
mg.ts = t
mg.count = v
mg.hasCount = true
case strings.HasSuffix(metricName, metricSuffixCreated):
case metricName == mf.metadata.Metric+metricSuffixCreated:
mg.created = v
default:
boundary, err := getBoundary(mf.mtype, ls)
Expand All @@ -297,7 +297,7 @@ func (mf *metricFamily) addSeries(seriesRef uint64, metricName string, ls labels
mg.complexValue = append(mg.complexValue, &dataPoint{value: v, boundary: boundary})
}
case pmetric.MetricTypeSum:
if strings.HasSuffix(metricName, metricSuffixCreated) {
if metricName == mf.metadata.Metric+metricSuffixCreated {
mg.created = v
} else {
mg.value = v
Expand Down
33 changes: 31 additions & 2 deletions receiver/prometheusreceiver/internal/metricfamily_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ var mc = testMetadataStore{
Help: "This is some help for a counter",
Unit: "By",
},
"counter_created": scrape.MetricMetadata{
Metric: "counter",
Type: textparse.MetricTypeCounter,
Help: "This is some help for a counter",
Unit: "By",
},
"gauge": scrape.MetricMetadata{
Metric: "ge",
Type: textparse.MetricTypeGauge,
Expand All @@ -59,7 +65,7 @@ var mc = testMetadataStore{
Unit: "ms",
},
"histogram_with_created": scrape.MetricMetadata{
Metric: "hg",
Metric: "histogram_with_created",
Type: textparse.MetricTypeHistogram,
Help: "This is some help for a histogram",
Unit: "ms",
Expand All @@ -77,7 +83,7 @@ var mc = testMetadataStore{
Unit: "ms",
},
"summary_with_created": scrape.MetricMetadata{
Metric: "s",
Metric: "summary_with_created",
Type: textparse.MetricTypeSummary,
Help: "This is some help for a summary",
Unit: "ms",
Expand Down Expand Up @@ -598,6 +604,29 @@ func TestMetricGroupData_toNumberDataUnitTest(t *testing.T) {
{at: 13, value: 33.7, metric: "value"},
{at: 13, value: 150, metric: "value_created"},
},
want: func() pmetric.NumberDataPoint {
point := pmetric.NewNumberDataPoint()
point.SetDoubleValue(150)

// the time in milliseconds -> nanoseconds.
point.SetTimestamp(pcommon.Timestamp(13 * time.Millisecond))
point.SetStartTimestamp(pcommon.Timestamp(13 * time.Millisecond))

attributes := point.Attributes()
attributes.PutStr("a", "A")
attributes.PutStr("b", "B")
return point
},
},
{
metricKind: "counter_created",
name: "counter:: startTimestampMs from _created",
intervalStartTimestampMs: 11,
labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
scrapes: []*scrape{
{at: 13, value: 33.7, metric: "counter"},
{at: 13, value: 150, metric: "counter_created"},
},
want: func() pmetric.NumberDataPoint {
point := pmetric.NewNumberDataPoint()
point.SetDoubleValue(33.7)
Expand Down

0 comments on commit 81cfc48

Please sign in to comment.