Over a million developers have joined DZone.

Automating API Monitoring Using MuleSoft Flow Designer

DZone 's Guide to

Automating API Monitoring Using MuleSoft Flow Designer

Learn how you can easily set up automated API monitoring and URL pinging with the MuleSoft Flow Designer in this detailed tutorial.

· Performance Zone ·
Free Resource

1.0 Overview

I have created a Mule application that pings URLs to ensure that they are up, and if the URL is down for some reason, the Mule application is capable of sending out an email notification. At the time of this writing, the Mule application could send out email notifications, and I plan to incorporate more types of notification as I continue to improve it.

The source code is fully obtainable via GitHub.

Figure 1.0.

Figure 1.0 shows an abstract diagram of what the API monitor expects. Users would submit a JSON payload consisting of an array of target URLs to be pinged by the API monitor. In conjunction with the target URL, users would also need to specify notification interval; the notification interval is useful in that users won't be spammed with notification messages if the ping is very frequent, e.g. 5 seconds. For instance, if the notification interval is set to 1 hour, and users are trying to ping a target URL that is down for every minute, and if the target URL is down for 2 hours, then users would essentially only receive 2 notification emails (instead of getting one email every minute the target URL is down). Figure 1.0a shows an example of an input payload.

Figure 1.0a.

If the URL is pingable, then the API monitor will set isUp to true (green); otherwise, it would set isUp to false and send out a notification to the user. The output payload from the API monitor would contain enriched data like the timestamp on which the URL is pinged, and also the status of the URL.

The returned notification timestamp would be the same as the previous notification timestamp if the notification interval has not expired. If the notification interval has expired, then it would be updated with a new timestamp.

The output JSON payload would also have the latest timestamp on the current ping attempt. Figure 1.0b shows an example of a JSON output payload.

Figure 1.0b.

2.0 Using Flow Designer to Automate Pinging

Now that you have an API monitor that is capable of pinging URLs and sending out notifications if the URL is down, the next step is to automate the pinging. We can easily achieve this via the new Design Center’s Mule Flow (assuming that you will be deploying the API monitor into CloudHub). Let's step through how we can achieve this. First, from CloudHub, you have to select “Design Center” (depicted in Figure 2.0).

Figure 2.0.

When in Design Center, click on the "create" button on your right (Figure 2.0a), and select Mule Application.

Figure 2.0a.

You will then be prompted with a pop-up dialog. Key in the same text as per depicted in Figure 2.0b

Figure 2.0b.

Select "scheduler" as the first component and set it to 10 seconds, as per Figure 2.0c

Figure 2.0c.

Next, select an HTTP requester component, and key in the URL as per figure 2.0d.

Figure 2.0d.

Then, if you see a text box marked with f(x) on its right-hand side like the one you see at Figure 2.0d, it means that it accepts Dataweave expressions. Flow Designer Mule Applications run on Mule Runtime 4.0; to learn more about it, go to the following links:

All the links I have shared above are for Mule 4.0.

You need to click on the map button beside the f(x) sign (Figure 2.0d). When you click on the map button, you will see the following pop up dialog in Figure 2.0e.

Figure 2.0e.

Click on the script tab and cut and paste your desired payload into the middle pane, and this will be the payload that will be submitted to the API manager every 10 seconds to ping the list of URLs you have specified in your payload.

When you finish your Flow Designer configuration, it should look like the following Figure 2.0f.

Figure 2.0f.

You need to click on the deploy button on the top right of the screen shown in Figure 2.0f. Once the application is deployed, you will see the small green marking on all the message processors, indicating that they have been executed successfully, as shown in figure 2.0g.

Figure 2.0g.

3.0 Running the Flow Designer Application

Right after you click "deploy" at figure 2.0f, CloudHub will deploy an instance of your Flow Designer Mule application at the runtime manager. If you go to your runtime manager in CloudHub, you will see your application at Figure 3.0a.

Figure 3.0a.

If you click on it and browse to the Logs Menu at Figure 3.0b, you will see the output payload returned by the API monitor application created earlier.

Figure 3.0b.

4.0 Conclusion

If you followed all the setup steps in the earlier sections, your Flow Designer application is actually running on the Design environment. You could download and version your application via the download icon at the top right of the CloudHub screen:You could also promote it to other environments by clicking on the promote icon:This is how you can easily automate URL/API monitoring.

mulesoft ,monitoring ,api monitoring ,automation ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}