feat(mongodb): add OpenTelemetry tracing and metrics - #15
Merged
Conversation
Implement MongoDB instrumentation using the mongo-driver v1 event.CommandMonitor API, replacing the deprecated otelmongo contrib package. Provides two APIs: - NewClient(Config) for new connections - Instrument(*options.ClientOptions) for existing option chains Includes OTel semantic convention attributes (db.system, db.name, db.operation, db.mongodb.collection, server.address, server.port), operation metrics (count, errors, duration histogram), command filtering for housekeeping commands, and monitor chaining to preserve user-set CommandMonitors. Resolves: FDE-48 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix duration precision: use float64(nanoseconds)/1e6 instead of float64(Milliseconds()) to preserve sub-millisecond accuracy in the duration histogram - Add nil-guard in connectWithMonitor: skip instrumentation gracefully if monitor creation fails, preventing potential nil-pointer dereference - Make NewClient error handling consistent with Instrument: warn and continue (instead of hard-failing) when the agent fails to start, matching the Redis integration pattern Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
Author
Code reviewFound 1 issue:
go-agent/integrations/mongodb/mongodb.go Lines 171 to 193 in 9b058b7 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
Add MongoDB integration documentation covering both NewClient and Instrument APIs, traced operations, skipped commands, span attributes, and automatic metrics. Update table of contents, supported frameworks table, and metrics sections. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Return the newMonitor error from connectWithMonitor instead of swallowing it. The client is still returned and usable (matching the Redis integration pattern), but callers now get programmatic notice when instrumentation setup partially failed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ashishl9
approved these changes
Feb 23, 2026
ashishl9
left a comment
Contributor
There was a problem hiding this comment.
LGTM!
Excellent MongoDB instrumentation implementation:
- Replaces deprecated otelmongo with native CommandMonitor approach
- Complete observability: spans, metrics (operations, errors, duration), proper OTel semconv
- Smart housekeeping command filtering reduces noise
- Proper context propagation with correct span nesting
- Flexible APIs: NewClient() and Instrument() patterns
- Monitor chaining respects existing configurations
- 7 comprehensive integration tests covering CRUD, aggregation, errors
Well-executed following established Redis/Kafka integration patterns.
Contributor
|
@prathamesh-sonpatki PR #16 has been merged. This PR needs to be updated with the latest main branch. Please update the branch and I'll merge it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
event.CommandMonitorfrom mongo-driver v1, replacing the deprecatedotelmongocontrib packageNewClient(Config)andInstrument(*options.ClientOptions)APIs consistent with existing Redis/Kafka integration patternsDetails
Tracing: Spans are created per MongoDB command with names following OTel DB semconv (
"insert users","find orders"). Housekeeping commands (hello,isMaster,ping,saslStart, etc.) are filtered out. Spans carrydb.system,db.name,db.operation,db.mongodb.collection,server.address, andserver.portattributes.Metrics: Three instruments —
db.mongodb.operations(counter),db.mongodb.errors(counter),db.mongodb.operation.duration(histogram in ms).Context propagation: Spans correctly parent under application spans via the driver's context forwarding.
Monitor chaining: If the caller has already set a
CommandMonitoron their options, both monitors are called.Test plan
TestMongoDB_InsertFind— verifies insert/find spans with correct attributesTestMongoDB_UpdateDelete— verifies update/delete span attributesTestMongoDB_ContextPropagation— verifies child spans share parent trace IDTestMongoDB_Aggregate— verifies aggregation pipeline spansTestMongoDB_SkippedCommands— verifies housekeeping commands produce no spansTestMongoDB_DuplicateKeyStillTraced— verifies both insert spans exist even when one returns a duplicate key errorTestMongoDB_InstrumentAPI— verifiesInstrument()API path produces traced spansAll 7 integration tests pass against a
mongo:7testcontainer.Resolves: FDE-48
🤖 Generated with Claude Code