diff --git a/.chloggen/db-propagate-context-info.yaml b/.chloggen/db-propagate-context-info.yaml new file mode 100644 index 0000000000..99ad2a5fe0 --- /dev/null +++ b/.chloggen/db-propagate-context-info.yaml @@ -0,0 +1,22 @@ +# Use this changelog template to create an entry for release notes. +# +# If your change doesn't affect end users you should instead start +# your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the area of concern in the attributes-registry, (e.g. http, cloud, db) +component: db + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add doc for context propagation + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +# The values here must be integers. +issues: [2162] + +# (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: diff --git a/docs/database/database-spans.md b/docs/database/database-spans.md index cb522120d0..88b622a796 100644 --- a/docs/database/database-spans.md +++ b/docs/database/database-spans.md @@ -13,6 +13,8 @@ linkTitle: Spans - [Notes and well-known identifiers for `db.system.name`](#notes-and-well-known-identifiers-for-dbsystemname) - [Sanitization of `db.query.text`](#sanitization-of-dbquerytext) - [Generating a summary of the query](#generating-a-summary-of-the-query) +- [Context Propagation](#context-propagation) + - [SQL commenter](#sql-commenter) - [Semantic conventions for specific database technologies](#semantic-conventions-for-specific-database-technologies) @@ -478,6 +480,28 @@ Semantic conventions for individual database systems or specialized instrumentat MAY specify a different `db.query.summary` format as long as produced summary remains relatively short and its cardinality remains low comparing to the `db.query.text`. +## Context Propagation + +**Status**: [Development][DocumentStatus] + +### SQL commenter + +Instrumentations SHOULD propagate the context information to the SQL queries following [SQL commenter](https://google.github.io/sqlcommenter/spec/). The instrumentation implementation MAY choose to either **append** the comment to the end of the query or **prepend** the comment at the beginning of the query, depending on the specific database system's requirements or preferences. + +| Attribute | Type | Description | Examples | Require level | Stability | +|------------------------|--------|---------------------------------------|----------|-------------------|----------------------------------------------------------------| +| [`service.name`](/docs/registry/attributes/service.md) | string | Logical name of the service [22] | `shoppingcart` | `Recommended` | ![Development](https://img.shields.io/badge/-development-blue) | + +**[22] `service.name`:** Instrumentations MAY use [SDK-provided resource detectors](https://opentelemetry.io/docs/specs/semconv/resource/#semantic-attributes-with-sdk-provided-default-value) to set the default value for this attribute. + +**Examples:** + +- For a query `SELECT * FROM songs` where `service.name` is `shoppingcart`: + + ```sql + SELECT * FROM songs /*service.name='shoppingcart'*/ + ``` + ## Semantic conventions for specific database technologies More specific Semantic Conventions are defined for the following database technologies: diff --git a/docs/database/sql-server.md b/docs/database/sql-server.md index c10011b0b5..f0fd563047 100644 --- a/docs/database/sql-server.md +++ b/docs/database/sql-server.md @@ -9,6 +9,9 @@ linkTitle: SQL Server - [Spans](#spans) +- [Context Propagation](#context-propagation) + - [SQL commenter](#sql-commenter) + - [SET CONTEXT_INFO](#set-context_info) - [Metrics](#metrics) @@ -152,6 +155,41 @@ and SHOULD be provided **at span creation time** (if provided at all): +## Context Propagation + +**Status**: [Development][DocumentStatus] + +### SQL commenter + +Instrumentations SHOULD **append** the comment to the end of the query. + +### SET CONTEXT_INFO + +Instrumentations MAY make use of [SET CONTEXT_INFO](https://learn.microsoft.com/en-us/sql/t-sql/statements/set-context-info-transact-sql?view=sql-server-ver16) to add text format of [`traceparent`](https://www.w3.org/TR/trace-context/#traceparent-header) before executing a query. This is an opt-in behavior that should be explicitly enabled by the user. + +`SET CONTEXT_INFO` must be executed on the same physical connection as the SQL statement (or reuse its transaction). + +Note that `SET CONTEXT_INFO` requires binary input according to its syntax: `SET CONTEXT_INFO { binary_str | @binary_var }` and has a maximum size of 128 bytes. + +Example: + +For a query `SELECT * FROM songs` where `traceparent` is `00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01`: + +Run the following command on the same physical connection as the SQL statement: + +```sql +-- The binary conversion may be done by the application or the driver. +DECLARE @BinVar varbinary(55); +SET @BinVar = CAST('00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01' AS varbinary(55)); +SET CONTEXT_INFO @BinVar; +``` + +Then run the query: + +```sql +SELECT * FROM songs; +``` + ## Metrics Microsoft SQL Server client instrumentations SHOULD collect metrics according to the general