From d745e191848a98cf9492639ffb90c76431cf9b5d Mon Sep 17 00:00:00 2001 From: Cuichen Li Date: Tue, 15 Apr 2025 10:54:44 +0800 Subject: [PATCH 1/3] update resources attributes --- receiver/sqlserverreceiver/scraper.go | 54 +++++++++++-------- .../templates/sqlServerQuerySample.tmpl | 2 + .../expectedQueryTextAndPlanQuery.yaml | 18 ++++--- .../expectedRecordDatabaseSampleQuery.yaml | 11 +++- ...ordDatabaseSampleQueryWithInvalidData.yaml | 11 +++- .../recordDatabaseSampleQueryData.txt | 2 + .../recordInvalidDatabaseSampleQueryData.txt | 2 + .../testdata/testQuerySampleQuery.txt | 2 + 8 files changed, 72 insertions(+), 30 deletions(-) diff --git a/receiver/sqlserverreceiver/scraper.go b/receiver/sqlserverreceiver/scraper.go index 3be80c1d7dcac..777ffa7b73346 100644 --- a/receiver/sqlserverreceiver/scraper.go +++ b/receiver/sqlserverreceiver/scraper.go @@ -116,15 +116,17 @@ func (s *sqlServerScraperHelper) ScrapeMetrics(ctx context.Context) (pmetric.Met func (s *sqlServerScraperHelper) ScrapeLogs(ctx context.Context) (plog.Logs, error) { var err error + var resources pcommon.Resource switch s.sqlQuery { case getSQLServerQueryTextAndPlanQuery(): - err = s.recordDatabaseQueryTextAndPlan(ctx, s.config.TopQueryCount) + resources, err = s.recordDatabaseQueryTextAndPlan(ctx, s.config.TopQueryCount) case getSQLServerQuerySamplesQuery(): - err = s.recordDatabaseSampleQuery(ctx) + resources, err = s.recordDatabaseSampleQuery(ctx) default: return plog.Logs{}, fmt.Errorf("Attempted to get logs from unsupported query: %s", s.sqlQuery) } - return s.lb.Emit(), err + + return s.lb.Emit(metadata.WithLogsResource(resources)), err } func (s *sqlServerScraperHelper) Shutdown(_ context.Context) error { @@ -501,7 +503,7 @@ func (s *sqlServerScraperHelper) recordDatabaseStatusMetrics(ctx context.Context return errors.Join(errs...) } -func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Context, topQueryCount uint) error { +func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Context, topQueryCount uint) (pcommon.Resource, error) { // Constants are the column names of the database status const ( dbPrefix = "sqlserver." @@ -521,6 +523,8 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont totalWorkerTime = "total_worker_time" ) + resources := pcommon.NewResource() + rows, err := s.client.QueryRows( ctx, sql.Named("lookbackTime", -s.config.LookbackTime), @@ -529,7 +533,7 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont ) if err != nil { if !errors.Is(err, sqlquery.ErrNullValueWarning) { - return fmt.Errorf("sqlServerScraperHelper failed getting rows: %w", err) + return resources, fmt.Errorf("sqlServerScraperHelper failed getting rows: %w", err) } s.logger.Warn("problems encountered getting log rows", zap.Error(err)) } @@ -561,6 +565,7 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont // sort the totalElapsedTimeDiffs in descending order as well sort.Slice(totalElapsedTimeDiffsMicrosecond, func(i, j int) bool { return totalElapsedTimeDiffsMicrosecond[i] > totalElapsedTimeDiffsMicrosecond[j] }) + resourcesAdded := false timestamp := pcommon.NewTimestampFromTime(time.Now()) for i, row := range rows { // skipping the rest of the rows as totalElapsedTimeDiffs is sorted in descending order @@ -577,12 +582,6 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont record.SetEventName("top query") attributes := []internalAttribute{ - { - key: computerNameKey, - columnName: computerNameKey, - valueRetriever: vanillaRetriever, - valueSetter: setString, - }, { key: "db.query.text", columnName: queryText, @@ -655,12 +654,6 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont valueRetriever: defaultValueRetriever("microsoft.sql_server"), valueSetter: setString, }, - { - key: instanceNameKey, - columnName: instanceNameKey, - valueRetriever: vanillaRetriever, - valueSetter: setString, - }, { key: serverAddressKey, valueRetriever: defaultValueRetriever(s.config.Server), @@ -712,9 +705,17 @@ func (s *sqlServerScraperHelper) recordDatabaseQueryTextAndPlan(ctx context.Cont attr.valueSetter(record.Attributes(), attr.key, value) } } + if !resourcesAdded { + resourceAttributes := resources.Attributes() + resourceAttributes.PutStr("host.name", s.config.Server) + resourceAttributes.PutStr("sqlserver.computer.name", row[computerNameKey]) + resourceAttributes.PutStr("sqlserver.instance.name", row[instanceNameKey]) + + resourcesAdded = true + } s.lb.AppendLogRecord(record) } - return errors.Join(errs...) + return resources, errors.Join(errs...) } // cacheAndDiff store row(in int) with query hash and query plan hash variables @@ -864,7 +865,7 @@ func setDouble(attributes pcommon.Map, key string, value any) { attributes.PutDouble(key, value.(float64)) } -func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) error { +func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) (pcommon.Resource, error) { const blockingSessionID = "blocking_session_id" const clientAddress = "client_address" const clientPort = "client_port" @@ -902,9 +903,10 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) ctx, sql.Named("top", s.config.TopQueryCount), ) + resources := pcommon.NewResource() if err != nil { if !errors.Is(err, sqlquery.ErrNullValueWarning) { - return fmt.Errorf("sqlServerScraperHelper failed getting log rows: %w", err) + return resources, fmt.Errorf("sqlServerScraperHelper failed getting log rows: %w", err) } // in case the sql returned rows contains null value, we just log a warning and continue s.logger.Warn("problems encountered getting log rows", zap.Error(err)) @@ -912,6 +914,7 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) var errs []error + resourcesAdded := false for _, row := range rows { queryHashVal := hex.EncodeToString([]byte(row[queryHash])) queryPlanHashVal := hex.EncodeToString([]byte(row[queryPlanHash])) @@ -1148,6 +1151,15 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) record.Body().SetStr("sample") s.lb.AppendLogRecord(record) + + if !resourcesAdded { + resourceAttributes := resources.Attributes() + resourceAttributes.PutStr("host.name", s.config.Server) + resourceAttributes.PutStr("sqlserver.computer.name", row[computerNameKey]) + resourceAttributes.PutStr("sqlserver.instance.name", row[instanceNameKey]) + + resourcesAdded = true + } } - return errors.Join(errs...) + return resources, errors.Join(errs...) } diff --git a/receiver/sqlserverreceiver/templates/sqlServerQuerySample.tmpl b/receiver/sqlserverreceiver/templates/sqlServerQuerySample.tmpl index 3504d2d4b2710..48cd195e4fad7 100644 --- a/receiver/sqlserverreceiver/templates/sqlServerQuerySample.tmpl +++ b/receiver/sqlserverreceiver/templates/sqlServerQuerySample.tmpl @@ -1,4 +1,6 @@ SELECT TOP(@top) + REPLACE(@@SERVERNAME,'\',':') AS [sql_instance], + HOST_NAME() AS [computer_name], DB_NAME(r.database_id) AS db_name, ISNULL(c.client_net_address, '') as client_address, ISNULL(c.client_tcp_port, '') AS client_port, diff --git a/receiver/sqlserverreceiver/testdata/expectedQueryTextAndPlanQuery.yaml b/receiver/sqlserverreceiver/testdata/expectedQueryTextAndPlanQuery.yaml index 5b2714a4aeceb..77bc3490eea42 100644 --- a/receiver/sqlserverreceiver/testdata/expectedQueryTextAndPlanQuery.yaml +++ b/receiver/sqlserverreceiver/testdata/expectedQueryTextAndPlanQuery.yaml @@ -1,18 +1,22 @@ resourceLogs: - resource: - attributes: [] + attributes: + - key: sqlserver.computer.name + value: + stringValue: DESKTOP-GHAEGRD + - key: sqlserver.instance.name + value: + stringValue: sqlserver + - key: host.name + value: + stringValue: "0.0.0.0" scopeLogs: - logRecords: - attributes: - key: db.system.name value: stringValue: microsoft.sql_server - - key: computer_name - value: - stringValue: DESKTOP-GHAEGRD - - key: sql_instance - value: - stringValue: sqlserver + - key: server.address value: stringValue: 0.0.0.0 diff --git a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml index 4bb6603470416..db2499bd15635 100644 --- a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml +++ b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml @@ -1,6 +1,15 @@ resourceLogs: - resource: - attributes: [] + attributes: + - key: sqlserver.computer.name + value: + stringValue: DESKTOP-GHAEGRD + - key: sqlserver.instance.name + value: + stringValue: sqlserver + - key: host.name + value: + stringValue: "0.0.0.0" scopeLogs: - logRecords: - attributes: diff --git a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml index 6bc754329b415..80ea9bb81eaa1 100644 --- a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml +++ b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml @@ -1,6 +1,15 @@ resourceLogs: - resource: - attributes: [] + attributes: + - key: sqlserver.computer.name + value: + stringValue: DESKTOP-GHAEGRD + - key: sqlserver.instance.name + value: + stringValue: sqlserver + - key: host.name + value: + stringValue: "0.0.0.0" scopeLogs: - logRecords: - attributes: diff --git a/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt b/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt index 252103a20f3b5..d7e73ca5f7310 100644 --- a/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt +++ b/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt @@ -1,5 +1,7 @@ [ { + "computer_name": "DESKTOP-GHAEGRD", + "sql_instance": "sqlserver", "db_name": "master", "client_address": "172.19.0.1", "client_port": "59286", diff --git a/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt b/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt index 2e5c548367aa7..a7c5b26fdbcfe 100644 --- a/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt +++ b/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt @@ -1,5 +1,7 @@ [ { + "computer_name": "DESKTOP-GHAEGRD", + "sql_instance": "sqlserver", "db_name": "master", "client_address": "172.19.0.1", "client_port": "a59286", diff --git a/receiver/sqlserverreceiver/testdata/testQuerySampleQuery.txt b/receiver/sqlserverreceiver/testdata/testQuerySampleQuery.txt index 3504d2d4b2710..48cd195e4fad7 100644 --- a/receiver/sqlserverreceiver/testdata/testQuerySampleQuery.txt +++ b/receiver/sqlserverreceiver/testdata/testQuerySampleQuery.txt @@ -1,4 +1,6 @@ SELECT TOP(@top) + REPLACE(@@SERVERNAME,'\',':') AS [sql_instance], + HOST_NAME() AS [computer_name], DB_NAME(r.database_id) AS db_name, ISNULL(c.client_net_address, '') as client_address, ISNULL(c.client_tcp_port, '') AS client_port, From 28df2ecc20e56bc7585efa3ff5fdc03f8590f955 Mon Sep 17 00:00:00 2001 From: Cuichen Li Date: Wed, 16 Apr 2025 11:11:57 +0800 Subject: [PATCH 2/3] add change log --- .chloggen/update-resources-attributes.yaml | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/update-resources-attributes.yaml diff --git a/.chloggen/update-resources-attributes.yaml b/.chloggen/update-resources-attributes.yaml new file mode 100644 index 0000000000000..101aa94482fc8 --- /dev/null +++ b/.chloggen/update-resources-attributes.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: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: sqlserverreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: We used to export those attributes on the log level, this is not alignihg with the semantic convention, also, the old way will cause waste of disk resources, as we can group them to a higher level to avoid repeat them all the places. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [39449] + +# (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 8e30920d9437a814cafdd853a638e9cb93438608 Mon Sep 17 00:00:00 2001 From: Cuichen Li Date: Thu, 17 Apr 2025 09:33:58 +0800 Subject: [PATCH 3/3] update change log --- .chloggen/update-resources-attributes.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.chloggen/update-resources-attributes.yaml b/.chloggen/update-resources-attributes.yaml index 101aa94482fc8..10fe08c6770c3 100644 --- a/.chloggen/update-resources-attributes.yaml +++ b/.chloggen/update-resources-attributes.yaml @@ -7,7 +7,7 @@ change_type: breaking component: sqlserverreceiver # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: We used to export those attributes on the log level, this is not alignihg with the semantic convention, also, the old way will cause waste of disk resources, as we can group them to a higher level to avoid repeat them all the places. +note: "`host.name`, `sqlserver.computer.name`, and `sqlserver.instance.name` are now resource attributes instead of log attributes. We used to report `computer_name` and `instance_name` in the log attributes for top query collection and they are now deprecated. Now we report the three resources attributes in both top query collection and sample query collection." # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [39449] @@ -15,7 +15,7 @@ issues: [39449] # (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: +subtext: This change is only relevant for logs. # 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.