RabbitMQ
Application Insights
End-to-End Transactions
Message Queuing
Tracking Transactions

How can I show a end-to-end transaction over RabbitMQ in Application Insights?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

RabbitMQ is a widely used open-source message broker that helps in decoupling systems or components of a system by providing a reliable message passing mechanism. When integrating RabbitMQ with distributed applications, it becomes essential to enable end-to-end tracing to ensure the robustness and efficiency of interactions, especially when troubleshooting issues in production. Microsoft's Application Insights provides powerful tools for monitoring applications, detecting performance anomalies, and tracking custom events, thus making it a suitable choice for monitoring applications that utilize RabbitMQ for message passing.

Understanding Application Insights and RabbitMQ

Before detailing the steps to track end-to-end transactions via RabbitMQ in Application Insights, it's important to understand the typical architecture and flow:

  • RabbitMQ: Acts as a message broker, handling the delivery of messages between different parts of an application. It supports various messaging protocols and patterns.
  • Application Insights: A feature of Azure Monitor, it is an extensible Application Performance Management (APM) service for developers and DevOps professionals. It monitors live applications, automatically detecting performance anomalies, and includes powerful analytics tools.

Instrumenting RabbitMQ with Application Insights

To successfully monitor and visualize RabbitMQ transactions in Application Insights, you need to instrument your applications to send telemetry data. Here are detailed steps and considerations:

1. Configure Application Insights in Your Application

First, ensure Application Insights is configured in your application. For .NET applications, you typically add the Application Insights SDK through NuGet:

bash
Install-Package Microsoft.ApplicationInsights -Version <version>

Configure the InstrumentationKey in your appsettings.json or through the environment:

json
1{
2  "ApplicationInsights": {
3    "InstrumentationKey": "your-instrumentation-key-here"
4  }
5}

2. Instrument RabbitMQ Messages

Application Insights doesn’t automatically capture custom dependencies like RabbitMQ messages. You have to manually track these dependencies.

Sending Side:

When sending a message, you can start an operation that represents this outgoing operation. Use the TelemetryClient to start and stop an operation:

csharp
1var telemetryClient = new TelemetryClient();
2using (var operation = telemetryClient.StartOperation<DependencyTelemetry>("RabbitMQ Send")) {
3  // code to send message
4  client.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
5
6  // You can set success or failure
7  operation.Telemetry.Success = true;
8  telemetryClient.StopOperation(operation);
9}
Receiving Side:

Each time a message is received and processed, track it as a separate dependency:

csharp
1var telemetryClient = new TelemetryClient();
2using (var operation = telemetryClient.StartOperation<DependencyTelemetry>("RabbitMQ Receive")) {
3  // code to process message
4  // Example: var body = ea.Body.ToArray(); (process the message from 'body')
5
6  // Set operation status
7  operation.Telemetry.Success = true;
8  telemetryClient.StopOperation(operation);
9}

3. Correlation Across Services

To trace transactions end-to-end across different services or components, correlate the telemetry:

  • Use headers or message properties in RabbitMQ to pass the Operation Id and Parent Operation Id.
  • Ensure each participating component in the service chain logs these IDs.

4. Visualization and Query in Application Insights

After setting up telemetry, use Application Insights to query and visualize data. You can query using the Kusto Query Language (KQL) in the Log Analytics workspace:

kusto
dependencies
where type == "RabbitMQ"summarize avg(duration) by bin(timestamp, 1h), operation_Name
DependenceManual instrumentation needed
Key ComponentsTelemetryClient, DependencyTelemetry
Data PropagationUse headers for correlation IDs
VisualizationQuery with KQL in Application Insights Logs

Conclusion

Instrumenting RabbitMQ with Application Insights involves configuring telemetry collectors appropriately, sending custom telemetry for RabbitMQ messages, and correlating messages across services. This setup provides comprehensive visibility into the interactions and performance metrics, aiding in superior monitoring, fault investigation, and overall system health analysis.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.