Adding Observability to a React Router v7 app
Written by Frank Fiegel on .
Glama has exploded in popularity over the last few months, and with increased popularity came performance issues.
Luckily, Sentry has a great OpenTelemetry integration that makes tracing a breeze.
Unfortunately, Sentry's OpenTelemetry integration does not cover React Router loaders.
In this post, I will show you how to instrument your React Router application with OpenTelemetry to measure the load times of your loaders.
This post assumes that you are using React Router v7 with Fastify. Refer to my previous post for instructions on how to set up a React Router v7 with Fastify.
Instrumenting React Router
Somewhere in your production code, you should have a factory that creates a React Router request handler. Something like this:
In order to instrument the loaders, we will iterate over all routes and wrap each loader
and action
in a span. We can do this by wrapping the serverBuild
object using the instrumentServerBuild
function below.
Did you ever want to get route ID in your loader or action? Notice that the routeId
is available to us when wrapping the loader
and action
in a span. You can extend this code to inject the route ID into the args
object.
This function was shared by @rossipedia on Discord.
What this function does is:
- Iterate over all routes in the server build.
- Wrap the
loader
andaction
usingSentry.startSpan
. - Return the new server build.
I use Sentry, but you could easily switch Sentry.startSpan
here with startActiveSpan
from OpenTelemetry if you prefer (that would also work with Sentry's OpenTelemetry integration).
Additionally, I personally like to use a custom span function that allows me to print execution times in the console, e.g.,
This way I can simply observe the stream of server logs to identify slow loaders.
That's it!
Conclusion
By instrumenting your React Router v7 loaders with OpenTelemetry, you gain valuable insights into your app's performance and can quickly identify bottlenecks. After adding this instrumentation, it only took me five minutes to identify the performance issue I had been tracking for weeks.
In Ross's words: Good observability is priceless
Written by Frank Fiegel (@punkpeye)