SentryJames CrosswellTutorial
MongoDB Query Tracing in .NET with Sentry + OTLP
Summary
Measuring MongoDB performance in a .NET application requires command details, durations, and enough context to distinguish isolated spikes from recurring query patterns. The post presents an end-to-end setup that uses MongoDB’s built-in OpenTelemetry Activity instrumentation, the .NET OpenTelemetry SDK, Sentry.OpenTelemetry.Exporter, and OTLP ingestion rather than a dedicated Sentry.MongoDB integration. Configuration initializes Sentry with UseOtlp(), subscribes to MongoTelemetry.ActivitySourceName, adds MongoDB instrumentation, and exports spans with AddSentryOtlp(...), while avoiding duplicate spans from overlapping hooks. Query context appears in Sentry traces, span samples, and the Queries experience, with Relay generating parameterized values for aggregation but retaining raw db.query.text per span. The post also demonstrates client-side redaction and describes server-side scrubbing, while exporter propagation bridges preserve correlation with errors and replays.
Context
The work addresses the need to measure MongoDB performance in .NET applications, identify slow commands and collections, distinguish one-off spikes from repeated query patterns, and correlate database traces with related Sentry errors and replays. It also addresses the risk that query payloads may contain sensitive data such as identifiers, emails, tokens, or other fields.
Approach / What changed
Use MongoDB’s built-in OpenTelemetry instrumentation and the .NET OpenTelemetry SDK, then export spans to Sentry through Sentry.OpenTelemetry.Exporter and AddSentryOtlp(...). Initialize Sentry with UseOtlp() to prevent duplicate spans, subscribe to MongoTelemetry.ActivitySourceName, and optionally redact db.query.text with an Activity processor or configure Advanced Data Scrubbing rules.
Takeaways
UseOtlp() is needed when OpenTelemetry supplies trace data so Sentry’s built-in tracing hooks do not generate overlapping duplicate spans.
Relay creates a parameterized query copy for grouping and display in Queries, but the original db.query.text remains stored and viewable in individual span or exemplar details.
Sensitive MongoDB query data can be omitted, redacted client-side with a BaseProcessor, or targeted with server-side Advanced Data Scrubbing rules.
Source and freshness
- Source
- blog.sentry.io
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.