Skip to content

RUBY-3612 OpenTelemetry#2957

Merged
comandeo-mongo merged 32 commits intomongodb:masterfrom
comandeo:3612-otel
Dec 19, 2025
Merged

RUBY-3612 OpenTelemetry#2957
comandeo-mongo merged 32 commits intomongodb:masterfrom
comandeo:3612-otel

Conversation

@comandeo
Copy link
Copy Markdown
Contributor

@comandeo comandeo commented Nov 3, 2025

This PR introduces native support for OpenTelemetry tracing.

Summary

When the feature is enabled, Ruby driver traces driver operation and the corresponding server commands nested under the operation. Tracing can be enabled per Mongo::Client instance (by setting the tracing option to true), or globally via the OTEL_RUBY_INSTRUMENTATION_MONGODB_ENABLED environment variable.

@comandeo-mongo comandeo-mongo marked this pull request as ready for review December 12, 2025 08:54
@comandeo-mongo comandeo-mongo requested a review from a team as a code owner December 12, 2025 08:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds OpenTelemetry tracing support to the MongoDB Ruby driver, enabling distributed tracing capabilities for MongoDB operations and commands following the OpenTelemetry semantic conventions for database instrumentation.

Key changes:

  • New tracing infrastructure with Mongo::Tracing::OpenTelemetry::Tracer, OperationTracer, and CommandTracer classes
  • Integration of tracing into client, collection, database, and session operations
  • Support for transaction span tracking and cursor context management
  • Environment variable configuration for enabling tracing and query text capture
  • Comprehensive test suite including unified spec tests for OpenTelemetry

Reviewed changes

Copilot reviewed 62 out of 62 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
lib/mongo/tracing.rb Main tracing module with factory method for creating tracers
lib/mongo/tracing/open_telemetry/tracer.rb Core tracer implementation managing operation and command tracing
lib/mongo/tracing/open_telemetry/operation_tracer.rb Tracer for driver-level operations with span attribute management
lib/mongo/tracing/open_telemetry/command_tracer.rb Tracer for server commands with query text capture support
lib/mongo/client.rb Client integration for tracer initialization and configuration
lib/mongo/session.rb Transaction span lifecycle management
lib/mongo/operation/shared/executable.rb Command tracing integration in operation execution
lib/mongo/collection.rb Operation tracing for collection methods
lib/mongo/collection/view/*.rb Tracing integration for view operations
lib/mongo/index/view.rb Index operation tracing
spec/support/tracing.rb Mock tracing infrastructure for testing
spec/spec_tests/open_telemetry_spec.rb Unified spec test runner for OpenTelemetry
spec/spec_tests/data/open_telemetry/**/*.yml Comprehensive test specifications

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

end
end

gem 'opentelemetry-api'
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.

Is this necessary? It looks like opentelemetry-sdk declares opentelemtry-api as a dependency (here)

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.

Good point, thank you. Fixed.

#
# @return [ String | nil ] the collection name, or nil if not applicable.
def collection_name(message)
case message.documents.first.keys.first
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.

Will this be stable? Are we guaranteed that the first key will always be the name of the command? Though, honestly, I don't have any better ideas for how to extract the command name here...

Also, perhaps it would be clearer to use the #command_name method here, which has the same logic but also does #to_s.

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.

I could not find a concrete statement that the first key is always the command name; however, this is so for all the documented command.

EXCLUDED_KEYS = %w[lsid $db $clusterTime signature].freeze

# Ellipsis for truncated query text.
ELLIPSES = '...'
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.

"Ellipsis" (with an 'i') is a single set of three dots, e.g. '...'

"Ellipses" (with an 'e') is the plural, referring to multiple sets of three dots. E.g. "I really like ellipses... I use them everywhere..."

Suggested change
ELLIPSES = '...'
ELLIPSIS = '...'

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.

Done

@comandeo-mongo comandeo-mongo requested a review from jamis December 18, 2025 08:01
@comandeo-mongo comandeo-mongo merged commit 5e64c1d into mongodb:master Dec 19, 2025
166 of 168 checks passed
@comandeo-mongo comandeo-mongo deleted the 3612-otel branch December 19, 2025 12:35
@jamis jamis added the feature A PR for a new feature label Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A PR for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants