Skip to content

Fix OpenTelemetry: db.name reports the configured database, not the database sent with the request - #687

Open
polyglotAI-bot wants to merge 1 commit into
mainfrom
polyglot/otel-db-name-query-database
Open

polyglotAI-bot wants to merge 1 commit into
mainfrom
polyglot/otel-db-name-query-database

Conversation

@polyglotAI-bot

Copy link
Copy Markdown
Collaborator

Summary

Fixes #685.

Root cause. ActivitySourceHelper.StartActivity set db.name from client.Settings.Database. It did not get the per-query options. The request itself uses CreateUriBuilder, which sends queryOverride?.Database ?? Settings.Database. So a query sent with QueryOptions.Database / InsertOptions.Database reported the configured database, or "" when the connection string has no Database. This also applied to ClickHouseCommand after ClickHouseConnection.ChangeDatabase, because the command forwards the selected database as QueryOptions.Database. Only the trace was wrong: the query ran in the correct database.

Fix. PostSqlQueryAsync and the private PostStreamAsync (the only two span sites) now build the URI first and pass uriBuilder.Database to StartActivity. The tag therefore comes from the same value the request sends, so the two cannot drift. All Execute* methods, ClickHouseCommand, the InsertBinaryAsync schema lookup and batches, InsertRawStreamAsync, and the public PostStreamAsync overloads go through these two sites.

Changes

  • Diagnostic/ActivitySourceHelper.cs: StartActivity gets an optional database argument. When the argument is null, the tag stays the client's configured database.
  • ClickHouseClient.cs: both span sites pass the resolved database.
  • docs/http.mdx: the db.name row now says which database is reported.
  • changelog.d/685-otel-db-name.fixes.md: changelog fragment.

Behavior

Configured Database QueryOptions.Database db.name before db.name after
"" not set "" "" (unchanged)
system not set system system (unchanged)
"" system "" system
system default system default
system "" system "" (the request sends no database, so the server uses the user's default database)

Not changed, for a maintainer decision (raised in the issue): when neither the connection string nor the query sets a database, the tag is still "". The client does not know the user's server-side default database, so leaving the tag out may be better.

Tests

New tests in ActivitySourceHelperTests go through the real entry points against a live server:

  • ExecuteScalarAsync_ConfiguredAndQueryOptionsDatabase_SetsDbNameToDatabaseSentWithRequest: one parametrized test for the five rows above. Each case uses its own client with an explicit configured database. The two no-override rows pin the unchanged behavior.
  • InsertBinaryAsync_WithInsertOptionsDatabase_SetsDbNameToInsertDatabaseOnEverySpan: both spans (the schema lookup PostSqlQueryAsync and the PostStreamAsync batch) report InsertOptions.Database.
  • ExecuteScalarAsync_AfterChangeDatabase_SetsDbNameToSelectedDatabase: after ChangeDatabase("system"), the tag equals the server's currentDatabase().

On unpatched main, 5 of the new cases fail with the wrong tag and the 2 no-override cases pass. With the fix, all pass. The full ClickHouse.Driver.Tests suite passes on net10.0 against ClickHouse 26.9 (14823 passed, 0 failed, 142 skipped). All target frameworks build with no new analyzer warnings.

Related: #684 (ClickHouseConnection.PostStreamAsync and ClickHouseBulkCopy ignore ChangeDatabase). It does not overlap with this change. When #684 sends the selected database on those paths, their spans report it automatically, because the tag now comes from the URI builder.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • Deterministic repro on main; the new tests fail without the fix and pass with it
  • No existing tests changed
  • Docs updated (docs/http.mdx); no public API change (StartActivity is internal)

🤖 Generated with Claude Code

The HTTP client set the db.name span tag from ClickHouseClientSettings.Database,
so a query sent to another database (QueryOptions.Database,
InsertOptions.Database, or ClickHouseConnection.ChangeDatabase for commands)
still reported the configured database. PostSqlQueryAsync and PostStreamAsync
now build the URI first and pass its resolved Database to StartActivity.

Fixes: #685

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 07:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow fix reuses existing database resolution, covers both span sites, and includes regression tests and matching documentation.

0 open findings

What changed in this PR

Fixes #685 in the HTTP client by making OpenTelemetry db.name match the database sent with each request, without changing query behavior.

Changes:

  • Pass the resolved request database to both span creation sites.
  • Add regression tests for query overrides, inserts, and ChangeDatabase.
  • Document the corrected behavior and add a changelog fragment.
File Description
docs/​http.mdx Clarifies how db.name is resolved.
ClickHouse.Driver/​Diagnostic/​ActivitySourceHelper.cs Accepts the resolved database for span tagging.
ClickHouse.Driver/​ClickHouseClient.cs Supplies the request database at both span sites.
ClickHouse.Driver.Tests/​ActivitySourceHelperTests.cs Adds database-tag regression coverage.
changelog.d/​685-otel-db-name.fixes.md Records the telemetry correction.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenTelemetry: db.name span tag reports the configured database, not the database the query runs in

2 participants