How to monitor the slowness of your Angular web app’s HTTP requests and API calls

The simple-pimple guide to expose slow HTTP requests that are frustrating users and no doubt causing revenue loss.

Some web apps act like a dying cow.

And would you believe it? These silly-jiggy-jaggy-dancing spinners seem to think they’ll hypnotize us into patiently waiting those extra 6.8 seconds!

It’s like waiting on a turtle for a horse ride.

Say, can’t we do better?

How can we find the HTTP requests in our Angular application that are slowing the show? Once we’ve uncovered the bottle-necks we can fix them and make your HTTP requests zip.

We could then connect our Angular app to a monitoring service like Application Insights and get alerts for turtle-performance.

Why just imagine it! About the time your boss shows up saying that users are complaining of slowness you’d tell him “I just got done fixing it!” and he’d be so surprised he’d swallow his gum.

Nope, I’m not selling you snake oil. Read on to learn this stuff for yourself my friend.

Create an interceptor

We’ll begin by adding a good ole HTTP Intercepter to our Angular project.

Open a terminal in your Angular project and whip it up with the Angular CLI like so.

And then we’ll import it into our module file by adding it to the providers array.

Harness the interceptor

Next we’ll open the new monitor.interceptor.ts file. It should look something like this.

And we’ll want to modify it to look like this.

Parting thoughts…

And that’s it! We’ve built an HTTP interceptor that will monitor every outgoing HTTP request and tell us how long it took.

Over to you buster. Now you can take that logRequestTime function and jazz it up with whatever kind of functionality you want.

Simple pimple? Well… I told you so.

Questions or comments? Please don’t hesitate to contact me.

Originally published at https://danielk.tech.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store