Skip to content

[receiver/sqlserver] Accept traceparent context info #39539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/sqlserver-accept-traceparent-context-info.yaml
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 2 additions & 2 deletions receiver/sqlserverreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions receiver/sqlserverreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
version: latest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resourceLogs:
- resource:
attributes:
attributes:
- key: sqlserver.computer.name
value:
stringValue: DESKTOP-GHAEGRD
Expand Down Expand Up @@ -108,12 +108,12 @@ resourceLogs:
stringValue: "307831343032313046363442373838434239"
- key: sqlserver.context_info
value:
stringValue: ""
stringValue: "307837304133423133304231303438443444"
- key: sqlserver.username
value:
stringValue: sa
body:
stringValue: sample
scope:
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver
version: latest
version: latest
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"row_count": "1",
"query_hash": "0x70A3B130B1048D4D",
"query_plan_hash": "0x140210F64B788CB9",
"context_info": "",
"context_info": "00-0af7651916cd43dd8448eb211c80319c-a7ad6a7169203331-01",
"username": "sa"
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"row_count": "a1",
"query_hash": "0x70A3B130B1048D4D",
"query_plan_hash": "0x140210F64B788CB9",
"context_info": "",
"context_info": "0x70A3B130B1048D4D",
"username": "sa"
}
]
]
Loading