Skip to content

feat(pymongo): introduce db.operation, refactor db.statement, refactor span name #3606

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

smoke
Copy link
Contributor

@smoke smoke commented Jul 2, 2025

Description

This change updates the opentelemetry-instrumentation-pymongo to better align with OpenTelemetry semantic conventions and the behavior of other language instrumentations (e.g. NodeJS, Ruby, etc.).

see https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

The key changes are:

This provides more consistent and conventional telemetry data for MongoDB operations.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

The existing unit tests in instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py have been updated to reflect these changes.

Specifically, the tests now verify:

  • db.operation is correctly set with the command name.
  • db.statement is not present on the span by default.
  • When capture_statement = True is enabled, db.statement is correctly populated with the full command statement.
  • The span name is correctly formatted.

All tests were run and passed using the project's tox configuration:

tox -e test-pymongo

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

This change updates the opentelemetry-instrumentation-pymongo to
better align with OpenTelemetry semantic conventions and
the behavior of other language instrumentations (e.g. NodeJS, Ruby, etc.).

see https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

The key changes are:
 - The `db.operation` attribute is now used to record the MongoDB command name (e.g., find, insert).
 - The `db.statement` attribute is no longer populated by default. It is only recorded if the `capture_statement = True` option is passed to the instrumentor.
 - The span name has been made more specific, changing from `{db_name}.{command}` to `{db_name}.{collection}.{command}` when a collection is present.

This provides more consistent and conventional telemetry data for MongoDB operations.
@smoke smoke requested a review from a team as a code owner July 2, 2025 08:09
@smoke smoke force-pushed the feat/instrumentation-pymongo-db-operation-attr branch from 377d3b2 to d0fea95 Compare July 2, 2025 08:11
@smoke smoke force-pushed the feat/instrumentation-pymongo-db-operation-attr branch from 9270a69 to c759426 Compare July 2, 2025 08:33
@smoke smoke force-pushed the feat/instrumentation-pymongo-db-operation-attr branch from c759426 to 487e811 Compare July 2, 2025 08:36
@smoke smoke force-pushed the feat/instrumentation-pymongo-db-operation-attr branch from c9fad50 to 498a8ac Compare July 2, 2025 11:36
@tammy-baylis-swi
Copy link
Contributor

Thanks @smoke for working on improving adherence to semconv!

I think this should set db.operation.name rather than db.operation because SpanAttributes is deprecated. Meanwhile, I don't think db.operation.name is a direct replacement db.statement. Eventually, deprecated db.statement should be replaced by db.query.text and the value shouldn't completely go away.

Both attribute names could be updated similarly to the PRs that address this issue as a hard switch away from SpanAttributes: #3475. I'd suggest for a first PR for pymongo that we still keep db.statement and switch to db.query.text another time, but switch to the _incubating version instead of the SpanAttributes version like this asyncpg instrumentor PR. What do you think?

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.

pymongo Invalid type in attribute sequence when using sessions
2 participants