How to Track Events Across Multiple Services
Tags: doctordroid, events, developers
Published: 2023-04-20 08:33:00


In this article, we will see the various challenges in microservice architecture and we will then explore one major challenge that many developers and businesses face: how to track the events flowing across these services.

Tracking events are required for various business use cases such as audit trails, fraud detection, [missing API callbacks](https://notes.drdroid.io/api-callback-webhooks-monitoring), raising alarms on unusual patterns, and delays and failure in services.



###Microservices Architecture:<a name="chapter-1"></a>

If you're well-versed in microservices, you can directly jump to the next section from the table of contents above.

Microservices have been popularised since 2011 when it was introduced in an architecture [workshop in Venice](https://martinfowler.com/articles/microservices.html). Since then, most developers have adopted the microservices architectural style to develop their products.

```
Microservices is an approach that enables teams to work independently on different services, deploy changes more frequently, and scale services independently. The impact of microservices has been significant, enabling organiations to build and scale complex applications faster and with more agility.
```

Microservices offer several benefits for software development teams. The architecture provides greater agility by allowing faster development cycles and independent deployment of individual services, resulting in faster time-to-market.

Other benefits include better fault isolation, improved reliability, and uptime.

## Challenges in Microservices<a name="chapter-2"></a>

- Microservices bring significant challenges such as distributed ownership of entities. As the number of microservices grows, it becomes difficult to track and understand the impact of decisions, leading to a lack of visibility and isolation in the decision-making process.
- Identifying failures or issues across services can become challenging due to distributed nature of transactions and data flow. Also, failures in one service can have cascading effects on other services.
- Managing data in a microservices architecture can become complex due to each microservice having its own data storage and retrieval requirements.
- Integration testing becomes more complex.
- Ensuring data privacy, having an audit trail, and implementing consistent security across multiple services can be challenging.

##How to Implement Event Tracking Across Services?<a name="chapter-3"></a>

Event tracking refers to the process of capturing and monitoring user interactions or events within an application, website, or system. Events are user actions or system-generated events such as clicks, taps, searches, logs, and more. For example, say we have an e-commerce platform where customers can place orders for products. When a customer places an order, the order is created in the Checkout Service, and the order details are stored in a database. The order is then processed by various other microservices, such as Inventory Service, Payment Service, and Delivery Service.

One important metric for the platform is the time taken for an order to be delivered. However, tracking this metric is difficult because the order passes through multiple services, and none of the services has context outside their own.

Below are the various ways to implement event tracking:

###1. Adding Logs to ELK/Logging Tools<a name="chapter-3.1"></a>

To facilitate issue detection across microservices, logs can be added in different services and sent to ELK or the logging system with a unique key to join events. However, this approach has cons such as complex querying in the logging system and non-trivial searching of events going into different indexes/streams.


###2. Building an In-House System<a name="chapter-3.2"></a>

Implementing event tracking in-house involves building a custom system to track and store events generated by the application. To achieve this we can embed code to capture events in business logic code and push the events to a database. This can also be combined with log files to create a custom report or visualization to identify anomalies or insights.

However, it can be complex and time-consuming to build and maintain such systems, so it is only recommended for applications with specific requirements that cannot be met by existing solutions. It may be more efficient to use a third-party event tracking tool instead such as Dr Droid.


###3. Building ELT Pipelines:<a name="chapter-3.3"></a>

We can also use Extract-Load-Transform (ELT) techniques. This involves capturing data from multiple sources such as web analytics, mobile apps, and CRM systems, and then transforming and loading it into a centralized data warehouse for analysis. Inherently they don't provide any intelligence on top of it. We can use a BI tool and create complex queries on the data and extract value from it.

Let’s consider an example:
<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wybce6n6smozpa8hpfja.png">
  <figcaption>
Figure 1: A simplified ETL pipeline
</figcaption>
</figure>



Companies like Uber have [created entire teams to build this scalably internally](https://www.uber.com/en-US/blog/charon/).

To get insights on any metric like the order_delivered time, ELT pipeline can be created, with any OLAP database (in this case, Snowflake) as the sink. Kafka queues are being used to stream the data from the Checkout and Delivery services to the Snowflake database.

Checkout and Delivery services will both send Order fact data (created_at and delivered_at timestamps) to snowflake tables which can be monitored via a BI tool generating hourly reports of the trending average service level agreements (SLAs) at the platform level.

However, setting up and maintaining a mix of tools or ELT can be complex and require significant effort. It may also require specialized skills and resources to build intelligence from the captured data.

##How Dr Droid Simplifies Event Tracking.<a name="chapter-4"></a>
<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wx40dqv8kru6dgys5mc.jpg">
</figure>


To simplify the process of tracking events in microservices and correlating them to metrics, tools like Dr Droid can be used. This eliminates the need for managing a shared infrastructure and cron jobs. Dr Droid allows us to send events from microservices and set up alerts for individual orders or aggregated delayed orders. By sending events such as `order_created` and `order_delivered` to Dr Droid, it becomes easier to track metrics and set up alerts.

###Dr Droid Set up Walkthrough<a name="chapter-5"></a>

We are using the [API specs](https://docs.drdroid.io/reference/events) and showing how to interact with Dr Droid using the REST API. An event called order_created is created.

 <figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhqfbhx16higiytsr0te.png">
  <figcaption>Figure 2: Generating order_created event</figcaption>
</figure>


After creating the event `order_created`, you can notice in the dashboard:

 <figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1zrh8fkgg063ak1f60g.png">
  <figcaption>Figure 3: `order_created` event on the dashboard</figcaption>
</figure>


Following that, we created another event `order_delivered`:


 <figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4mseelqvoy8yamhhbik.png">
  <figcaption>Figure 4: `order_delivered` event generation from API specs</figcaption>
</figure>

 <figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6rb20vkaxc7e1f2pket.png">
  <figcaption>Figure 5: order_delivered event at the dashboard
</figcaption>
</figure>

We can then set up monitoring in the console to track the time taken for each order to be delivered.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwtxi6ivis06xkgjjpgb.png">
  <figcaption>
Figure 6: Monitor dashboard
</figcaption>
</figure>

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukndpovn51yp7g61tk5w.png">
  <figcaption>
Figure 7: Selecting primary and secondary events
</figcaption>
</figure>


Configuring a notification to be triggered if there is a delay in the occurrence of the secondary event despite the primary event being initiated.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/835wxlldcmqsua3sjh16.png">
  <figcaption>
Figure 8: Setting up an alert
</figcaption>
</figure>

There are two options available for setting up notifications, and I am choosing to enable email notifications.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmqorkuzlnveswnetrfr.png">
  <figcaption>
Figure 9: Notification options
</figcaption>
</figure>

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryytvjsmdhzzay9s1s68.png">
  <figcaption>
Figure 10: Setting up email notification
</figcaption>
</figure>

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xax7nap1bbd5n5a43wkn.png">
  <figcaption>
Figure 11: Monitoring set up is done
</figcaption>
</figure>


We are now ready to perform a test. We will proceed by creating a new event called `order_created`.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe0hz0iq17lm7m806fmu.png">
  <figcaption>
Figure 12: A new event created `order_created`
</figcaption>
</figure>


The primary event, which is currently active, can be viewed in the console.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9botv50ygh78drnwaja3.png">
  <figcaption>
Figure 13: Monitoring dashboard
</figcaption>
</figure>


As we have not yet created the secondary event, `order_delivered`, an alert notification has been received.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n3ehiem0ifmbiexijk9.png">
  <figcaption>
Figure 14: Monitoring dashboard alerts
</figcaption>
</figure>


An email notification was received stating that the secondary event, `order_delivered`, has not been created within a timeframe of 10 seconds.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnr4l66b1etyv34oh8rs.png">
  <figcaption>
Figure 15: Email notification
</figcaption>
</figure>


<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mp7dpd1n4sz460yg5f3k.png">
  <figcaption>
Figure 16: Notification on the dashboard
</figcaption>
</figure>


In this way, we can simply post the events to the Dr Droid over REST API to capture them in a single place and make it easy to audit, monitor and trigger alerts as per the business requirements.


##Summary<a name="chapter-6"></a>
I hope you have learned various use cases of event tracking and why it is vital in the microservices architecture. We also quickly learned various ways to implement event tracking including using Dr Droid.

Do let us know through comments, how you use event tracking and try Dr Droid. Our team will be happy to learn about your use cases.

[Sign up](https://app.drdroid.io/signup?ref=blog) and try Dr Droid.