diff --git a/.chloggen/sqlserver-accept-traceparent-context-info.yaml b/.chloggen/sqlserver-accept-traceparent-context-info.yaml new file mode 100644 index 0000000000000..8b41390638eb7 --- /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: enhancement + +# 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: 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: [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. +# 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/sqlserverreceiver/go.mod b/receiver/sqlserverreceiver/go.mod index 1572a5a9047b5..93249db495543 100644 --- a/receiver/sqlserverreceiver/go.mod +++ b/receiver/sqlserverreceiver/go.mod @@ -24,6 +24,8 @@ require ( go.opentelemetry.io/collector/receiver/receivertest v0.124.1-0.20250422165940-c47951a8bf71 go.opentelemetry.io/collector/scraper v0.124.1-0.20250422165940-c47951a8bf71 go.opentelemetry.io/collector/scraper/scraperhelper v0.124.1-0.20250422165940-c47951a8bf71 + 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.1-0.20250422165940-c47951a8bf71 // indirect go.opentelemetry.io/collector/receiver/xreceiver v0.124.1-0.20250422165940-c47951a8bf71 // 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.37.0 // indirect golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect 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 +]