From 017b3d4b87928942688a99133b61fecdbfb91671 Mon Sep 17 00:00:00 2001 From: Sam DeHaan Date: Wed, 18 Dec 2024 18:15:19 -0500 Subject: [PATCH] [receiver/sqlqueryreceiver] Add instrumentation scope to sqlqueryreceiver metrics & logs (#36812) #### Description Add instrumentation scope to the metrics and logs emitted by the sqlqueryreceiver. #### Link to tracking issue Fixes https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/31028 --- .chloggen/sqlqueryreceiver-scope.yaml | 27 +++++++++ internal/sqlquery/scraper.go | 39 +++++++------ internal/sqlquery/scraper_test.go | 56 ++++++++++++------- receiver/sqlqueryreceiver/integration_test.go | 2 - receiver/sqlqueryreceiver/logs_receiver.go | 4 +- receiver/sqlqueryreceiver/receiver.go | 5 +- .../testdata/integration/mysql/expected.yaml | 6 +- .../testdata/integration/oracle/expected.yaml | 3 +- .../integration/postgresql/expected.yaml | 6 +- 9 files changed, 102 insertions(+), 46 deletions(-) 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..f3c95de6d8ff --- /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 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] + +# (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/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/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", 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/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() 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) 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