Asked 1 month ago by AsteroidSatellite181
How can I automatically trace MongoDB queries in my C# .NET Core app using OpenTelemetry?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by AsteroidSatellite181
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to automatically trace MongoDB queries in my C# .NET Core application using OpenTelemetry, but I'm not seeing any MongoDB activity in my Grafana Tempo dashboard.
I have set up observability for my app running in a Kubernetes cluster using a self-hosted Grafana LGTM stack (without Mimir). OpenTelemetry sends trace data to Prometheus, which is then visualized in a Grafana Tempo dashboard.
Here's what I've done so far:
kube-prometheus-stack
opentelemetry-operator
OpenTelemetry Collector
OpenTelemetry .NET AutoInstrumentation
OpenTelemetry.AutoInstrumentation
packageMongoDB.Driver.Core.Extensions.DiagnosticSource
package✅ Expectation: MongoDB traces should appear in my OpenTelemetry request trace.
❌ Reality: MongoDB activity is not visible in Grafana Tempo:
Comments 0
•Answers 2
•Views 65
Answer by TychoAI Agent • 1 month ago
Below is an overview of the steps needed to have MongoDB queries automatically traced via OpenTelemetry in your .NET Core app.
MongoDB Diagnostic Integration:
The key piece is installing the NuGet package
MongoDB.Driver.Core.Extensions.DiagnosticSource
This package hooks into the diagnostic events provided by the MongoDB driver so that when the auto instrumentation is running, the Mongo events are picked up and converted into trace spans.
Auto Instrumentation Setup:
You already have the following in place:
OpenTelemetry.AutoInstrumentation
)When you add the MongoDB.Driver.Core.Extensions.DiagnosticSource
package to your project, it subscribes to the diagnostic events emitted by the MongoDB .NET driver. Then, the .NET auto instrumentation agent automatically picks those events up as long as:
If everything is wired up correctly, each MongoDB query will produce one or more diagnostic events that the auto instrumentation converts into spans. Those spans will then be sent along with your other traces (such as incoming HTTP requests) to your OpenTelemetry collector, which is later viewed in Grafana Tempo.
If MongoDB activity doesn’t appear in your Tempo dashboard despite the setup:
Ensure Package Versions and Order:
Verify that you’re using a MongoDB driver version that supports diagnostic source events and that the MongoDB.Driver.Core.Extensions.DiagnosticSource
package is loaded in your app before any MongoDB operations occur.
Environment Variables & Startup Hooks:
Make sure that the .NET auto instrumentation startup hooks (set via environment variables like DOTNET_STARTUP_HOOKS
) are active. They are responsible for enabling automatic instrumentation.
Check Initialization:
Confirm that the auto instrumentation configuration isn’t overridden and that the diagnostic source from MongoDB is enabled by default.
Logs and Diagnostics:
Enable verbose logging for the auto instrumentation to see if it reports subscribing to diagnostic events for MongoDB. This can help determine if the instrumentation is active but not forwarding spans.
While no code change is required to “switch on” the diagnostic-based tracing, your project file should include the following package reference:
XML<ItemGroup> <PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSource" Version="YOUR_VERSION_HERE" /> </ItemGroup>
And ensure that your application's startup logic does nothing that might delay the loading of these packages before any MongoDB operations are performed.
To automatically trace MongoDB queries:
• Install the MongoDB.Driver.Core.Extensions.DiagnosticSource
package.
• Ensure that your MongoDB driver version supports diagnostic source events and that it’s initialized early in the app’s lifecycle.
• With OpenTelemetry’s .NET auto instrumentation in place (and properly configured via startup hooks/environment settings), the MongoDB driver’s events are captured and converted into trace spans, which will then be visible in your Grafana Tempo dashboard.
If after verifying these points your MongoDB queries still aren’t showing, double-check your auto instrumentation logs and configuration settings to ensure that nothing is blocking or filtering out the MongoDB diagnostic events.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by CosmicHunter603 • 1 month ago
Have you configured it properly?
CSHARPvar clientSettings = MongoClientSettings.FromUrl(mongoUrl); var options = new InstrumentationOptions { CaptureCommandText = true }; clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber(options)); var mongoClient = new MongoClient(clientSettings);
No comments yet.
No comments yet.