From d0f6b486eafa9fc6cc321227426ffec882ac85aa Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Thu, 12 Dec 2024 14:41:25 -0500 Subject: [PATCH 1/5] Add instrumentation scope to sqlqueryreceiver --- internal/sqlquery/scraper.go | 39 ++++++++++++++------------- receiver/sqlqueryreceiver/receiver.go | 5 +++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/internal/sqlquery/scraper.go b/internal/sqlquery/scraper.go index b8314f6a286d..dabe32993d8a 100644 --- a/internal/sqlquery/scraper.go +++ b/internal/sqlquery/scraper.go @@ -26,29 +26,31 @@ type DbProviderFunc func() (*sql.DB, error) type ClientProviderFunc func(Db, string, *zap.Logger, TelemetryConfig) DbClient type Scraper struct { - id component.ID - Query Query - ScrapeCfg scraperhelper.ControllerConfig - StartTime pcommon.Timestamp - ClientProviderFunc ClientProviderFunc - DbProviderFunc DbProviderFunc - Logger *zap.Logger - Telemetry TelemetryConfig - Client DbClient - Db *sql.DB + id component.ID + Query Query + ScrapeCfg scraperhelper.ControllerConfig + StartTime pcommon.Timestamp + ClientProviderFunc ClientProviderFunc + DbProviderFunc DbProviderFunc + Logger *zap.Logger + Telemetry TelemetryConfig + Client DbClient + Db *sql.DB + InstrumentationScope pcommon.InstrumentationScope } var _ scraper.Metrics = (*Scraper)(nil) -func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc) *Scraper { +func NewScraper(id component.ID, query Query, scrapeCfg scraperhelper.ControllerConfig, logger *zap.Logger, telemetry TelemetryConfig, dbProviderFunc DbProviderFunc, clientProviderFunc ClientProviderFunc, instrumentationScope pcommon.InstrumentationScope) *Scraper { return &Scraper{ - id: id, - Query: query, - ScrapeCfg: scrapeCfg, - Logger: logger, - Telemetry: telemetry, - DbProviderFunc: dbProviderFunc, - ClientProviderFunc: clientProviderFunc, + id: id, + Query: query, + ScrapeCfg: scrapeCfg, + Logger: logger, + Telemetry: telemetry, + DbProviderFunc: dbProviderFunc, + ClientProviderFunc: clientProviderFunc, + InstrumentationScope: instrumentationScope, } } @@ -83,6 +85,7 @@ func (s *Scraper) ScrapeMetrics(ctx context.Context) (pmetric.Metrics, error) { rm := rms.AppendEmpty() sms := rm.ScopeMetrics() sm := sms.AppendEmpty() + s.InstrumentationScope.CopyTo(sm.Scope()) ms := sm.Metrics() var errs []error for _, metricCfg := range s.Query.Metrics { diff --git a/receiver/sqlqueryreceiver/receiver.go b/receiver/sqlqueryreceiver/receiver.go index 637ef1b2928d..23508680a54a 100644 --- a/receiver/sqlqueryreceiver/receiver.go +++ b/receiver/sqlqueryreceiver/receiver.go @@ -10,6 +10,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/receiver/scraperhelper" @@ -46,7 +47,9 @@ func createMetricsReceiverFunc(sqlOpenerFunc sqlquery.SQLOpenerFunc, clientProvi dbProviderFunc := func() (*sql.DB, error) { return sqlOpenerFunc(sqlCfg.Driver, sqlCfg.DataSource) } - mp := sqlquery.NewScraper(id, query, sqlCfg.ControllerConfig, settings.TelemetrySettings.Logger, sqlCfg.Config.Telemetry, dbProviderFunc, clientProviderFunc) + scope := pcommon.NewInstrumentationScope() + scope.SetName(metadata.ScopeName) + mp := sqlquery.NewScraper(id, query, sqlCfg.ControllerConfig, settings.TelemetrySettings.Logger, sqlCfg.Config.Telemetry, dbProviderFunc, clientProviderFunc, scope) opt := scraperhelper.AddScraper(metadata.Type, mp) opts = append(opts, opt) From 2498b137b57e26df5e36ffbc6ab325994a189cf5 Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Thu, 12 Dec 2024 14:43:37 -0500 Subject: [PATCH 2/5] Add changelog --- .chloggen/sqlqueryreceiver-scope.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/sqlqueryreceiver-scope.yaml diff --git a/.chloggen/sqlqueryreceiver-scope.yaml b/.chloggen/sqlqueryreceiver-scope.yaml new file mode 100644 index 000000000000..e6340238eb35 --- /dev/null +++ b/.chloggen/sqlqueryreceiver-scope.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: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: sqlqueryreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add instrumentation scope to SQL query receiver metrics" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31028] + +# (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] From c29fc1bd9a3db6c904a00660bc6c6f6eb92120c1 Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Thu, 12 Dec 2024 14:46:51 -0500 Subject: [PATCH 3/5] Add scope to sqlqueryreceiver logs, too --- .chloggen/sqlqueryreceiver-scope.yaml | 2 +- receiver/sqlqueryreceiver/logs_receiver.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.chloggen/sqlqueryreceiver-scope.yaml b/.chloggen/sqlqueryreceiver-scope.yaml index e6340238eb35..f3c95de6d8ff 100644 --- a/.chloggen/sqlqueryreceiver-scope.yaml +++ b/.chloggen/sqlqueryreceiver-scope.yaml @@ -7,7 +7,7 @@ change_type: enhancement component: sqlqueryreceiver # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "Add instrumentation scope to SQL query receiver metrics" +note: "Add instrumentation scope to SQL query receiver metrics and logs" # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [31028] diff --git a/receiver/sqlqueryreceiver/logs_receiver.go b/receiver/sqlqueryreceiver/logs_receiver.go index 06ae82fc3fbc..c090f963a646 100644 --- a/receiver/sqlqueryreceiver/logs_receiver.go +++ b/receiver/sqlqueryreceiver/logs_receiver.go @@ -285,7 +285,9 @@ func (queryReceiver *logsQueryReceiver) collect(ctx context.Context) (plog.Logs, } var errs []error - scopeLogs := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() + scope := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() + scope.Scope().SetName(metadata.ScopeName) + scopeLogs := scope.LogRecords() for logsConfigIndex, logsConfig := range queryReceiver.query.Logs { for _, row := range rows { logRecord := scopeLogs.AppendEmpty() From 61146a152eed8b7e7ea1092ba1c54484ab15bdec Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Thu, 12 Dec 2024 14:54:01 -0500 Subject: [PATCH 4/5] update integration test output --- receiver/sqlqueryreceiver/integration_test.go | 2 -- .../testdata/integration/mysql/expected.yaml | 6 ++++-- .../testdata/integration/oracle/expected.yaml | 3 ++- .../testdata/integration/postgresql/expected.yaml | 6 ++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/receiver/sqlqueryreceiver/integration_test.go b/receiver/sqlqueryreceiver/integration_test.go index 25752a819150..d1326ca7b1b3 100644 --- a/receiver/sqlqueryreceiver/integration_test.go +++ b/receiver/sqlqueryreceiver/integration_test.go @@ -1,8 +1,6 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -//go:build integration - package sqlqueryreceiver import ( diff --git a/receiver/sqlqueryreceiver/testdata/integration/mysql/expected.yaml b/receiver/sqlqueryreceiver/testdata/integration/mysql/expected.yaml index a56a336852a2..c9728f86c1ac 100644 --- a/receiver/sqlqueryreceiver/testdata/integration/mysql/expected.yaml +++ b/receiver/sqlqueryreceiver/testdata/integration/mysql/expected.yaml @@ -38,7 +38,8 @@ resourceMetrics: stringValue: Action timeUnixNano: "1684614725494431000" name: genre.imdb - scope: {} + scope: + name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver - resource: {} scopeMetrics: - metrics: @@ -72,4 +73,5 @@ resourceMetrics: - asDouble: 3.4 timeUnixNano: "1684614725499638000" name: f - scope: {} + scope: + name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver diff --git a/receiver/sqlqueryreceiver/testdata/integration/oracle/expected.yaml b/receiver/sqlqueryreceiver/testdata/integration/oracle/expected.yaml index 908bf87e68a8..5fcc3933a409 100644 --- a/receiver/sqlqueryreceiver/testdata/integration/oracle/expected.yaml +++ b/receiver/sqlqueryreceiver/testdata/integration/oracle/expected.yaml @@ -38,4 +38,5 @@ resourceMetrics: stringValue: Action timeUnixNano: "1684632870521947236" name: genre.imdb - scope: {} + scope: + name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver diff --git a/receiver/sqlqueryreceiver/testdata/integration/postgresql/expected.yaml b/receiver/sqlqueryreceiver/testdata/integration/postgresql/expected.yaml index ff1ad65e577c..cae3f7b000e2 100644 --- a/receiver/sqlqueryreceiver/testdata/integration/postgresql/expected.yaml +++ b/receiver/sqlqueryreceiver/testdata/integration/postgresql/expected.yaml @@ -38,7 +38,8 @@ resourceMetrics: stringValue: SciFi timeUnixNano: "1684613308016320000" name: genre.imdb - scope: {} + scope: + name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver - resource: {} scopeMetrics: - metrics: @@ -81,4 +82,5 @@ resourceMetrics: dataPoints: - timeUnixNano: "1684613308030553000" name: h - scope: {} + scope: + name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver From 045f69569bb3b1de13a6163f3ac90d639a0b6309 Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Thu, 12 Dec 2024 15:31:21 -0500 Subject: [PATCH 5/5] Fix scraper tests --- internal/sqlquery/scraper_test.go | 56 ++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/internal/sqlquery/scraper_test.go b/internal/sqlquery/scraper_test.go index 4cb80117e3be..3c265566c0a1 100644 --- a/internal/sqlquery/scraper_test.go +++ b/internal/sqlquery/scraper_test.go @@ -20,6 +20,7 @@ import ( func TestScraper_ErrorOnStart(t *testing.T) { scrpr := Scraper{ + InstrumentationScope: pcommon.NewInstrumentationScope(), DbProviderFunc: func() (*sql.DB, error) { return nil, errors.New("oops") }, @@ -33,7 +34,8 @@ func TestScraper_ClientErrorOnScrape(t *testing.T) { Err: errors.New("oops"), } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, } _, err := scrpr.ScrapeMetrics(context.Background()) require.Error(t, err) @@ -46,7 +48,8 @@ func TestScraper_RowToMetricErrorOnScrape_Float(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.float", @@ -68,7 +71,8 @@ func TestScraper_RowToMetricErrorOnScrape_Int(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.int", @@ -91,7 +95,8 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.col", @@ -108,6 +113,7 @@ func TestScraper_RowToMetricMultiErrorsOnScrape(t *testing.T) { func TestScraper_SingleRow_MultiMetrics(t *testing.T) { scrpr := Scraper{ + InstrumentationScope: pcommon.NewInstrumentationScope(), Client: &FakeDBClient{ StringMaps: [][]StringMap{{{ "count": "42", @@ -191,7 +197,8 @@ func TestScraper_MultiRow(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{ { @@ -231,7 +238,8 @@ func TestScraper_MultiResults_CumulativeSum(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "transaction.count", @@ -254,7 +262,8 @@ func TestScraper_MultiResults_DeltaSum(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "transaction.count", @@ -290,7 +299,8 @@ func TestScraper_Float(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.float", @@ -314,7 +324,8 @@ func TestScraper_DescriptionAndUnit(t *testing.T) { }, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name", @@ -335,8 +346,9 @@ func TestScraper_FakeDB_Warnings(t *testing.T) { db := fakeDB{rowVals: [][]any{{42, nil}}} logger := zap.NewNop() scrpr := Scraper{ - Client: NewDbClient(db, "", logger, TelemetryConfig{}), - Logger: logger, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: NewDbClient(db, "", logger, TelemetryConfig{}), + Logger: logger, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name", @@ -354,8 +366,9 @@ func TestScraper_FakeDB_MultiRows_Warnings(t *testing.T) { db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}} logger := zap.NewNop() scrpr := Scraper{ - Client: NewDbClient(db, "", logger, TelemetryConfig{}), - Logger: logger, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: NewDbClient(db, "", logger, TelemetryConfig{}), + Logger: logger, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.col.0", @@ -375,8 +388,9 @@ func TestScraper_FakeDB_MultiRows_Error(t *testing.T) { db := fakeDB{rowVals: [][]any{{42, nil}, {43, nil}}} logger := zap.NewNop() scrpr := Scraper{ - Client: NewDbClient(db, "", logger, TelemetryConfig{}), - Logger: logger, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: NewDbClient(db, "", logger, TelemetryConfig{}), + Logger: logger, Query: Query{ Metrics: []MetricCfg{ { @@ -412,7 +426,8 @@ func TestScraper_StartAndTSColumn(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name", @@ -441,7 +456,8 @@ func TestScraper_StartAndTS_ErrorOnColumnNotFound(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name", @@ -466,7 +482,8 @@ func TestScraper_CollectRowToMetricsErrors(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name", @@ -496,7 +513,8 @@ func TestScraper_StartAndTS_ErrorOnParse(t *testing.T) { }}, } scrpr := Scraper{ - Client: client, + InstrumentationScope: pcommon.NewInstrumentationScope(), + Client: client, Query: Query{ Metrics: []MetricCfg{{ MetricName: "my.name",