diff --git a/.chloggen/postgresql_query_metric_value_bug_fix.yaml b/.chloggen/postgresql_query_metric_value_bug_fix.yaml new file mode 100644 index 0000000000000..c3a9df44a7cce --- /dev/null +++ b/.chloggen/postgresql_query_metric_value_bug_fix.yaml @@ -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: postgresqlreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix for inflated metric values in query metrics collection + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [43071] + +# (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: [user] diff --git a/receiver/postgresqlreceiver/scraper.go b/receiver/postgresqlreceiver/scraper.go index 08b88f744d15a..b06a4856e0e35 100644 --- a/receiver/postgresqlreceiver/scraper.go +++ b/receiver/postgresqlreceiver/scraper.go @@ -298,7 +298,7 @@ func (p *postgreSQLScraper) collectTopQuery(ctx context.Context, clientFactory p } finalValue := float64(0) if valDelta > 0 { - p.cache.Add(queryID.(string)+columnName, valDelta) + p.cache.Add(queryID.(string)+columnName, valInAtts) finalValue = valDelta } if info.finalConverter != nil { diff --git a/receiver/postgresqlreceiver/scraper_test.go b/receiver/postgresqlreceiver/scraper_test.go index 9b84a67d5b4f2..0bef9da548912 100644 --- a/receiver/postgresqlreceiver/scraper_test.go +++ b/receiver/postgresqlreceiver/scraper_test.go @@ -513,6 +513,18 @@ func TestScrapeTopQueries(t *testing.T) { // golden.WriteLogs(t, expectedFile, actualLogs) errs := plogtest.CompareLogs(expectedLogs, actualLogs, plogtest.IgnoreTimestamp()) assert.NoError(t, errs) + + // Verify the cache has updated with latest counter + + calls, callsExists := scraper.cache.Get(queryid + callsColumnName) + assert.True(t, callsExists) + assert.Equal(t, float64(123), calls) + execTime, execTimeExists := scraper.cache.Get(queryid + totalExecTimeColumnName) + assert.True(t, execTimeExists) + assert.Equal(t, float64(11), execTime) + planTime, planTimeExists := scraper.cache.Get(queryid + totalPlanTimeColumnName) + assert.True(t, planTimeExists) + assert.Equal(t, float64(12), planTime) } type (