A real-time visualization tool for MongoDB connection pool events that supports multiple clients simultaneously.
This application uses a producer-consumer pattern to visualize MongoDB connection pool states and metrics:
- Consumer (MongoDb.ConnectionPoolVisualizer): Blazor web application that visualizes connection pool data
- Producer (MongoDb.ConnectionPoolMonitor): Standalone application that monitors MongoDB connection pools and sends events to the visualizer
The application allows you to monitor the following in real-time:
- Connection pool states (Ready, Clearing, Cleared, Closed)
- Connection metrics (Added, Checking Out, Checked Out, Checking In, Checked In, Removed)
- Multiple MongoDB clients with clear visual separation
- Connection pool events across different MongoDB driver implementations
This solution consists of two separate applications:
A Blazor web application that:
- Provides a real-time visualization of MongoDB connection pool states and metrics
- Receives events via a REST API endpoint from any producer
- Uses SignalR to update the UI in real-time
- No longer directly connects to MongoDB (relies on external producer applications)
A console application that:
- Creates a single shared MongoClient instance for monitoring
- Monitors MongoDB connection pool events through event subscriptions
- Forwards events to the Visualizer application via HTTP POST
- Includes a test command to generate database activity
- Uses the same MongoClient instance for both monitoring and testing
Each application has its own separate solution file and can be run independently:
-
Start the ConnectionPoolVisualizer (Consumer):
cd MongoDb.ConnectionPoolVisualizer dotnet run --launch-profile https # Or to build and run through Visual Studio: # dotnet build MongoDb.ConnectionPoolVisualizer.sln
-
Start the ConnectionPoolMonitor (Lightweight Producer):
cd MongoDb.ConnectionPoolMonitor dotnet run # Or to build and run through Visual Studio: # dotnet build MongoDb.ConnectionPoolMonitor.sln
The Monitor now uses a lightweight architecture that simply forwards events without tracking metrics locally.
-
In the ConnectionPoolMonitor console:
- Press
S
to start monitoring - Press
T
to generate test activity that will be visualized - Press
X
to stop monitoring - Press
Q
to quit the application
- Press
Both applications use appsettings.json
for configuration:
-
ConnectionPoolMonitor settings:
ConnectionStrings:MongoDB
: MongoDB connection stringVisualizerApi:BaseUrl
: URL for the Visualizer API endpointMongoDb:DatabaseName
: Database to use for test operationsMongoDb:CollectionName
: Collection to use for test operationsMongoDb:ClientId
: Identifier for this client (used by visualizer for grouping)
-
ConnectionPoolVisualizer settings:
- No MongoDB connection settings required (the visualizer is a pure consumer that receives events via API)
The ConnectionPoolVisualizer supports distinguishing between different MongoDB clients:
-
Each Monitor instance has a Client ID:
- Default value is "Primary-MongoClient" with a random suffix
- Can be configured in
appsettings.json
by adding aClientId
property under theMongoDb
section
-
The Visualizer groups connection pools by Client ID in the interface for clear visual separation
-
If using multiple monitor processes, configure each with a different Client ID in its appsettings.json file
The ConnectionPoolVisualizer is designed to be used independently of the provided C# ConnectionPoolMonitor. You can:
- Run only the ConnectionPoolVisualizer application
- Create your own custom producer in any programming language
- Send connection pool events to the Visualizer API
This is particularly useful if:
- You're using MongoDB with a different driver (Python, Node.js, Java, etc.)
- You want to integrate with an existing application
- You need custom monitoring logic specific to your use case
You can create producers in any programming language by sending POST requests to the Visualizer API:
-
Minimal Event Design:
- The API now uses a minimal design with only essential fields
- No optional fields - producers should send only what's required
- Status is inferred from event type - no need to send it
- Metrics are calculated by the Visualizer - no need to send them
-
Event Types and Required Fields:
- All events: eventType, clusterId, serverId, timestamp
- Connection events: additionally include connectionId and connectionState
- Example formats are provided in the API Specification section below
The MongoDb.ConnectionPoolMonitor application uses a shared MongoClient instance for both monitoring and test operations. This is crucial because:
- Each MongoClient instance creates its own connection pool
- To monitor a connection pool, we must use the same MongoClient instance that creates it
- Using separate MongoClient instances would result in monitoring a different pool than the one we are testing
- C# (.NET 9.0)
- Blazor Server
- MongoDB C# Driver
- SignalR for real-time updates
- RESTful API for language-agnostic integration
- .NET 9.0 SDK
- MongoDB instance (local or remote)
The ConnectionPoolVisualizer provides a RESTful API endpoint that accepts MongoDB connection pool events:
POST https://localhost:5001/api/ConnectionPoolEvents
eventType
(string): Type of the connection pool event (e.g., "PoolOpened", "ConnectionCheckedOut")clusterId
(string): Identifier for the MongoDB clusterserverId
(string): Identifier for the MongoDB server (typically the host:port)timestamp
(ISO 8601 datetime): When the event occurred
connectionId
(string): The unique identifier of the connectionconnectionState
(string): The state of the connection (e.g., "Created", "CheckedOut")
Note: The Visualizer infers pool status from the eventType, so no status field is required.
GET https://localhost:5001/api/ConnectionPoolEvents/health
Returns a simple health status response to verify the API is running.
Pool Opened Event:
{
"eventType": "PoolOpened",
"clusterId": "cluster1",
"serverId": "localhost:27017",
"timestamp": "2025-05-14T14:30:00Z"
}
Connection Checkout Event:
{
"eventType": "ConnectionCheckedOut",
"clientId": "MyMongoClient",
"clusterId": "cluster1",
"serverId": "localhost:27017",
"connectionId": "6f8a023b-4b2c-43a0-8c82-1a25890cf47e",
"connectionState": "CheckedOut",
"timestamp": "2025-05-14T14:30:05Z"
}