From 806681646a2c4a952b82e2b5c684717a37002526 Mon Sep 17 00:00:00 2001 From: Chao Date: Tue, 22 Apr 2025 21:19:07 +0800 Subject: [PATCH 1/4] [receiver/sqlserver] Accept traceparent context_info --- ...erver-accept-traceparent-context-info.yaml | 27 +++++++++++++++++++ receiver/sqlserverreceiver/scraper.go | 25 ++++++++++++----- .../expectedRecordDatabaseSampleQuery.yaml | 7 +++-- ...ordDatabaseSampleQueryWithInvalidData.yaml | 6 ++--- .../recordDatabaseSampleQueryData.txt | 4 +-- .../recordInvalidDatabaseSampleQueryData.txt | 4 +-- 6 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 .chloggen/sqlserver-accept-traceparent-context-info.yaml diff --git a/.chloggen/sqlserver-accept-traceparent-context-info.yaml b/.chloggen/sqlserver-accept-traceparent-context-info.yaml new file mode 100644 index 0000000000000..0b2707a944f2e --- /dev/null +++ b/.chloggen/sqlserver-accept-traceparent-context-info.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: + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [] + +# (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: [] diff --git a/receiver/sqlserverreceiver/scraper.go b/receiver/sqlserverreceiver/scraper.go index 777ffa7b73346..f475c2db50ef3 100644 --- a/receiver/sqlserverreceiver/scraper.go +++ b/receiver/sqlserverreceiver/scraper.go @@ -21,6 +21,8 @@ import ( "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/scraper" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/sqlquery" @@ -915,10 +917,11 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) var errs []error resourcesAdded := false + propagator := propagation.TraceContext{} + for _, row := range rows { queryHashVal := hex.EncodeToString([]byte(row[queryHash])) queryPlanHashVal := hex.EncodeToString([]byte(row[queryPlanHash])) - contextInfoVal := hex.EncodeToString([]byte(row[contextInfo])) record := plog.NewLogRecord() record.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) @@ -975,11 +978,6 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) valueRetriever: vanillaRetriever, valueSetter: setString, }, - { - key: dbPrefix + contextInfo, - valueRetriever: defaultValueRetriever(contextInfoVal), - valueSetter: setString, - }, { key: dbPrefix + cpuTimeMillisecond, columnName: cpuTimeMillisecond, @@ -1128,6 +1126,21 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context) }, } + spanContext := trace.SpanContextFromContext(propagator.Extract(context.Background(), propagation.MapCarrier{ + "traceparent": row[contextInfo], + })) + + if spanContext.IsValid() { + record.SetTraceID(pcommon.TraceID(spanContext.TraceID())) + record.SetSpanID(pcommon.SpanID(spanContext.SpanID())) + } else { + attributes = append(attributes, internalAttribute{ + key: dbPrefix + contextInfo, + valueRetriever: defaultValueRetriever(hex.EncodeToString([]byte(row[contextInfo]))), + valueSetter: setString, + }) + } + for _, attr := range attributes { value, err := attr.valueRetriever(row, attr.columnName) if err != nil { diff --git a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml index db2499bd15635..00fb282460435 100644 --- a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml +++ b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml @@ -106,14 +106,13 @@ resourceLogs: - key: sqlserver.query_plan_hash value: stringValue: "307831343032313046363442373838434239" - - key: sqlserver.context_info - value: - stringValue: "" - key: sqlserver.username value: stringValue: sa body: stringValue: sample + spanId: a7ad6a7169203331 + traceId: 0af7651916cd43dd8448eb211c80319c scope: name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver - version: latest \ No newline at end of file + version: latest diff --git a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml index 80ea9bb81eaa1..2a6f70018f7d4 100644 --- a/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml +++ b/receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml @@ -1,6 +1,6 @@ resourceLogs: - resource: - attributes: + attributes: - key: sqlserver.computer.name value: stringValue: DESKTOP-GHAEGRD @@ -108,7 +108,7 @@ resourceLogs: stringValue: "307831343032313046363442373838434239" - key: sqlserver.context_info value: - stringValue: "" + stringValue: "307837304133423133304231303438443444" - key: sqlserver.username value: stringValue: sa @@ -116,4 +116,4 @@ resourceLogs: stringValue: sample scope: name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver - version: latest \ No newline at end of file + version: latest diff --git a/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt b/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt index d7e73ca5f7310..6bbe7953d79e2 100644 --- a/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt +++ b/receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt @@ -31,7 +31,7 @@ "row_count": "1", "query_hash": "0x70A3B130B1048D4D", "query_plan_hash": "0x140210F64B788CB9", - "context_info": "", + "context_info": "00-0af7651916cd43dd8448eb211c80319c-a7ad6a7169203331-01", "username": "sa" } -] \ No newline at end of file +] diff --git a/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt b/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt index a7c5b26fdbcfe..16456692b3328 100644 --- a/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt +++ b/receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt @@ -31,7 +31,7 @@ "row_count": "a1", "query_hash": "0x70A3B130B1048D4D", "query_plan_hash": "0x140210F64B788CB9", - "context_info": "", + "context_info": "0x70A3B130B1048D4D", "username": "sa" } -] \ No newline at end of file +] From 8d0122d3969e3980024d32033e21659942b38558 Mon Sep 17 00:00:00 2001 From: Chao Date: Tue, 22 Apr 2025 21:19:53 +0800 Subject: [PATCH 2/4] update --- .../sqlserver-accept-traceparent-context-info.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.chloggen/sqlserver-accept-traceparent-context-info.yaml b/.chloggen/sqlserver-accept-traceparent-context-info.yaml index 0b2707a944f2e..8f13ea5cb3e41 100644 --- a/.chloggen/sqlserver-accept-traceparent-context-info.yaml +++ b/.chloggen/sqlserver-accept-traceparent-context-info.yaml @@ -1,16 +1,16 @@ # Use this changelog template to create an entry for release notes. # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' -change_type: +change_type: enhancement # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) -component: +component: sqlserverreceiver # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: +note: Accept `context_info` with `traceparent` format in query sample collection, setting log record with correct traceId and spanId. # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. -issues: [] +issues: [39535] # (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. @@ -24,4 +24,4 @@ subtext: # 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: [] +change_logs: [user] From bcdcfa8295903c623e78bfd0d5b86c29d703bfc5 Mon Sep 17 00:00:00 2001 From: Chao Date: Tue, 22 Apr 2025 21:37:53 +0800 Subject: [PATCH 3/4] fix --- .chloggen/sqlserver-accept-traceparent-context-info.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/sqlserver-accept-traceparent-context-info.yaml b/.chloggen/sqlserver-accept-traceparent-context-info.yaml index 8f13ea5cb3e41..8b41390638eb7 100644 --- a/.chloggen/sqlserver-accept-traceparent-context-info.yaml +++ b/.chloggen/sqlserver-accept-traceparent-context-info.yaml @@ -10,7 +10,7 @@ component: sqlserverreceiver note: Accept `context_info` with `traceparent` format in query sample collection, setting log record with correct traceId and spanId. # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. -issues: [39535] +issues: [39539] # (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. From 998183ffb3c5d03b861b70cdc4a5c85aba76cd61 Mon Sep 17 00:00:00 2001 From: Chao Date: Tue, 22 Apr 2025 21:55:12 +0800 Subject: [PATCH 4/4] fix --- receiver/sqlserverreceiver/go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/receiver/sqlserverreceiver/go.mod b/receiver/sqlserverreceiver/go.mod index a6ea32527125f..69b3a7486167c 100644 --- a/receiver/sqlserverreceiver/go.mod +++ b/receiver/sqlserverreceiver/go.mod @@ -24,6 +24,8 @@ require ( go.opentelemetry.io/collector/receiver/receivertest v0.124.0 go.opentelemetry.io/collector/scraper v0.124.0 go.opentelemetry.io/collector/scraper/scraperhelper v0.124.0 + go.opentelemetry.io/otel v1.35.0 + go.opentelemetry.io/otel/trace v1.35.0 go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.27.0 @@ -120,12 +122,10 @@ require ( go.opentelemetry.io/collector/receiver/receiverhelper v0.124.0 // indirect go.opentelemetry.io/collector/receiver/xreceiver v0.124.0 // indirect go.opentelemetry.io/contrib/bridges/otelzap v0.10.0 // indirect - go.opentelemetry.io/otel v1.35.0 // indirect go.opentelemetry.io/otel/log v0.11.0 // indirect go.opentelemetry.io/otel/metric v1.35.0 // indirect go.opentelemetry.io/otel/sdk v1.35.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.35.0 // indirect - go.opentelemetry.io/otel/trace v1.35.0 // indirect go.uber.org/atomic v1.11.0 // indirect golang.org/x/crypto v0.36.0 // indirect golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect