Skip to content

Commit 588b225

Browse files
sincejuneatoulme
andauthored
[receiver/sqlserver] Accept traceparent context info (#39539)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Accept `context_info` with `traceparent` format in query sample collection, setting log record with correct traceId and spanId. Example use case: open-telemetry/opentelemetry-dotnet-contrib#2709 <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue n/a <!--Describe what testing was performed and which tests were added.--> #### Testing Updated <!--Describe the documentation added.--> #### Documentation Added <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Antoine Toulme <[email protected]>
1 parent 4ad4b1e commit 588b225

7 files changed

+58
-19
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: sqlserverreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Accept `context_info` with `traceparent` format in query sample collection, setting log record with correct traceId and spanId.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39539]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/sqlserverreceiver/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ require (
2424
go.opentelemetry.io/collector/receiver/receivertest v0.124.1-0.20250422165940-c47951a8bf71
2525
go.opentelemetry.io/collector/scraper v0.124.1-0.20250422165940-c47951a8bf71
2626
go.opentelemetry.io/collector/scraper/scraperhelper v0.124.1-0.20250422165940-c47951a8bf71
27+
go.opentelemetry.io/otel v1.35.0
28+
go.opentelemetry.io/otel/trace v1.35.0
2729
go.uber.org/goleak v1.3.0
2830
go.uber.org/multierr v1.11.0
2931
go.uber.org/zap v1.27.0
@@ -120,12 +122,10 @@ require (
120122
go.opentelemetry.io/collector/receiver/receiverhelper v0.124.1-0.20250422165940-c47951a8bf71 // indirect
121123
go.opentelemetry.io/collector/receiver/xreceiver v0.124.1-0.20250422165940-c47951a8bf71 // indirect
122124
go.opentelemetry.io/contrib/bridges/otelzap v0.10.0 // indirect
123-
go.opentelemetry.io/otel v1.35.0 // indirect
124125
go.opentelemetry.io/otel/log v0.11.0 // indirect
125126
go.opentelemetry.io/otel/metric v1.35.0 // indirect
126127
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
127128
go.opentelemetry.io/otel/sdk/metric v1.35.0 // indirect
128-
go.opentelemetry.io/otel/trace v1.35.0 // indirect
129129
go.uber.org/atomic v1.11.0 // indirect
130130
golang.org/x/crypto v0.37.0 // indirect
131131
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect

receiver/sqlserverreceiver/scraper.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"go.opentelemetry.io/collector/pdata/pmetric"
2222
"go.opentelemetry.io/collector/receiver"
2323
"go.opentelemetry.io/collector/scraper"
24+
"go.opentelemetry.io/otel/propagation"
25+
"go.opentelemetry.io/otel/trace"
2426
"go.uber.org/zap"
2527

2628
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/sqlquery"
@@ -915,10 +917,11 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context)
915917
var errs []error
916918

917919
resourcesAdded := false
920+
propagator := propagation.TraceContext{}
921+
918922
for _, row := range rows {
919923
queryHashVal := hex.EncodeToString([]byte(row[queryHash]))
920924
queryPlanHashVal := hex.EncodeToString([]byte(row[queryPlanHash]))
921-
contextInfoVal := hex.EncodeToString([]byte(row[contextInfo]))
922925

923926
record := plog.NewLogRecord()
924927
record.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
@@ -975,11 +978,6 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context)
975978
valueRetriever: vanillaRetriever,
976979
valueSetter: setString,
977980
},
978-
{
979-
key: dbPrefix + contextInfo,
980-
valueRetriever: defaultValueRetriever(contextInfoVal),
981-
valueSetter: setString,
982-
},
983981
{
984982
key: dbPrefix + cpuTimeMillisecond,
985983
columnName: cpuTimeMillisecond,
@@ -1128,6 +1126,21 @@ func (s *sqlServerScraperHelper) recordDatabaseSampleQuery(ctx context.Context)
11281126
},
11291127
}
11301128

1129+
spanContext := trace.SpanContextFromContext(propagator.Extract(context.Background(), propagation.MapCarrier{
1130+
"traceparent": row[contextInfo],
1131+
}))
1132+
1133+
if spanContext.IsValid() {
1134+
record.SetTraceID(pcommon.TraceID(spanContext.TraceID()))
1135+
record.SetSpanID(pcommon.SpanID(spanContext.SpanID()))
1136+
} else {
1137+
attributes = append(attributes, internalAttribute{
1138+
key: dbPrefix + contextInfo,
1139+
valueRetriever: defaultValueRetriever(hex.EncodeToString([]byte(row[contextInfo]))),
1140+
valueSetter: setString,
1141+
})
1142+
}
1143+
11311144
for _, attr := range attributes {
11321145
value, err := attr.valueRetriever(row, attr.columnName)
11331146
if err != nil {

receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQuery.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,13 @@ resourceLogs:
106106
- key: sqlserver.query_plan_hash
107107
value:
108108
stringValue: "307831343032313046363442373838434239"
109-
- key: sqlserver.context_info
110-
value:
111-
stringValue: ""
112109
- key: sqlserver.username
113110
value:
114111
stringValue: sa
115112
body:
116113
stringValue: sample
114+
spanId: a7ad6a7169203331
115+
traceId: 0af7651916cd43dd8448eb211c80319c
117116
scope:
118117
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver
119-
version: latest
118+
version: latest

receiver/sqlserverreceiver/testdata/expectedRecordDatabaseSampleQueryWithInvalidData.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resourceLogs:
22
- resource:
3-
attributes:
3+
attributes:
44
- key: sqlserver.computer.name
55
value:
66
stringValue: DESKTOP-GHAEGRD
@@ -108,12 +108,12 @@ resourceLogs:
108108
stringValue: "307831343032313046363442373838434239"
109109
- key: sqlserver.context_info
110110
value:
111-
stringValue: ""
111+
stringValue: "307837304133423133304231303438443444"
112112
- key: sqlserver.username
113113
value:
114114
stringValue: sa
115115
body:
116116
stringValue: sample
117117
scope:
118118
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver
119-
version: latest
119+
version: latest

receiver/sqlserverreceiver/testdata/recordDatabaseSampleQueryData.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"row_count": "1",
3232
"query_hash": "0x70A3B130B1048D4D",
3333
"query_plan_hash": "0x140210F64B788CB9",
34-
"context_info": "",
34+
"context_info": "00-0af7651916cd43dd8448eb211c80319c-a7ad6a7169203331-01",
3535
"username": "sa"
3636
}
37-
]
37+
]

receiver/sqlserverreceiver/testdata/recordInvalidDatabaseSampleQueryData.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"row_count": "a1",
3232
"query_hash": "0x70A3B130B1048D4D",
3333
"query_plan_hash": "0x140210F64B788CB9",
34-
"context_info": "",
34+
"context_info": "0x70A3B130B1048D4D",
3535
"username": "sa"
3636
}
37-
]
37+
]

0 commit comments

Comments
 (0)