python

Instrumenting Redis in Python with AppSignal

Camilo Reyes

Camilo Reyes on

Instrumenting Redis in Python with AppSignal

Using AppSignal, you can monitor the performance of your Redis calls and get insights into how your application uses Redis. The AppSignal Python agent supports instrumenting Redis calls out of the box, so you can start monitoring your usage with just a few lines of code.

In this post, we will cover how to instrument Redis in a Python application using AppSignal. We will build a simple URL shortener that stores data in a local Redis instance and uses AppSignal to monitor performance. Latencies, errors, and other metrics will be available in your dashboard in AppSignal, so you can keep an eye on how your application uses Redis.

Ready? Let’s get started!

Spin Up a Python Project

We will assume you have a Redis instance running locally and a basic understanding of Python. You can follow the instructions on the Redis website to install Redis on your machine. If you are using Windows, you can use the Windows Subsystem for Linux (WSL) to run Redis.

First, let’s check that the Redis service is on your machine by running the following command in your terminal:

Shell
> redis-cli ping PONG > lsof -i :6379

If PONG is the response and the lsof command doesn't output anything, then Redis is running on your machine.

Next, let’s create a new Python project:

Shell
> mkdir url-shortener > cd url-shortener

Now, let’s build the requirements.txt file that will contain the dependencies we need for the project:

txt
appsignal opentelemetry-instrumentation-redis opentelemetry-instrumentation-flask redis pyshorteners Flask flask-restful

Here's an explanation of each dependency:

  • appsignal: The AppSignal Python agent that helps us monitor our application's performance.
  • opentelemetry-instrumentation-redis: The OpenTelemetry instrumentation for Redis to instrument our Redis calls.
  • opentelemetry-instrumentation-flask: The OpenTelemetry instrumentation for Flask to instrument our Flask application.
  • redis: The supported Redis client for Python that we will use to interact with Redis.
  • pyshorteners: A library that helps us shorten URLs.
  • Flask: The Flask web framework that we will use to build the URL shortener.
  • flask-restful: A library that helps us build RESTful APIs with Flask.

Note: Keep in mind that the AppSignal Python agent does not work on Windows. If you are using Windows, you can use the Windows Subsystem for Linux (WSL) to run the AppSignal agent.

Now, let’s install the dependencies:

Shell
> pip install -r requirements.txt

The AppSignal agent needs an API key to connect to the AppSignal service. You can get your API key by signing up for an AppSignal account — there's a free trial available.

Once you have an account, you can configure the AppSignal agent via this command:

Shell
> python -m appsignal install

A series of prompts will ask for your API key and application name. You can use the default values or provide your own. This install command will create an __appsignal__.py file in your project with the configuration for the AppSignal agent.

You can now inspect the __appsignal__.py file that was created in your project.

Alternatively, you can create the __appsignal__.py file manually with the following content:

Python
# __appsignal__.py from appsignal import Appsignal appsignal = Appsignal( active=True, name="url-shortener", push_api_key="YOUR_API_KEY", )

All set! Now, let’s instrument Redis in our application.

Instrumenting Redis

With the AppSignal agent installed and configured, we can now instrument our application.

Create a shorten-url.py file with the following instructions:

Python
# shorten-url.py import appsignal from openTelemetry.instrumentation.redis import RedisInstrumentor # open telemetry instrumentation appsignal.start() RedisInstrumentor().instrument()

The appsignal.start() function will start the AppSignal agent and configure it with the settings from the __appsignal__.py file. Be sure to put these lines at the top of your Python file to ensure the AppSignal agent starts before any other code is executed.

Once the AppSignal agent starts, we can use the RedisInstrumentor class from the opentelemetry-instrumentation-redis package to instrument our Redis calls. The instrument() method will configure the Redis client and start collecting metrics.

Note: If you do not see any metrics in the AppSignal dashboard, it is likely because either the AppSignal agent is not running or the Redis instrumentation is not working.

To troubleshoot the instrumentation, you can use the following command:

Shell
> python -m appsignal diagnose

This command will check the configuration of the AppSignal agent and the instrumentation of the Redis client. If there are any issues, the command will output an error message that you can use to fix the problem.

Alternatively, you can submit your report to the AppSignal support team for help. The report will be made available via a web link you can share with the support team.

Next, we will build an app that shortens URLs using this instrumentation.

Building the URL Shortener

We will use the pyshorteners library to shorten URLs and the redis library to store the data in Redis. In the shorten-url.py file, simply add the following code:

Python
# shorten-url.py import appsignal from openTelemetry.instrumentation.redis import RedisInstrumentor # open telemetry instrumentation appsignal.start() RedisInstrumentor().instrument() import redis from flask import Flask, request from flask_restful import Resource, Api from urllib.parse import urlparse import pyshorteners app = Flask(__name__) api = Api(app) class ApiResource(Resource): def post(self): data = request.get_json()['url'] def is_valid_url(url): result = urlparse(data) return all([result.scheme, result.netloc]) if is_valid_url(data) is False: raise Exception('Invalid URL.') type_tiny = pyshorteners.Shortener() client = redis.StrictRedis(host='localhost', port=6379) if client.exists(data): return {'url': client.get(data).decode('utf-8')}, 200 else: if 'tinyurl.com' in data: raise Exception('URL is already shortened.') short_url = type_tiny.tinyurl.short(data) client.set(short_url, data) return {'ur': (short_url)} api.add_resource(ApiResource, '/') if __name__ == '__main__': app.run(debug=True)

We do validation checks to ensure the URL is legit and not already shortened. If everything checks out, we shorten the URL and store the data in Redis. If the URL already exists in Redis, we return the shortened URL.

The instrumentation we added earlier will monitor the performance of the Redis calls and provide insights. Every Redis call is monitored and we can see the latencies, errors, and other metrics in our dashboard in AppSignal.

To test this out, you can run the following CURL command:

Shell
> curl -i -X POST -H 'Content-Type: application/json' -d '{"url": "https://docs.appsignal.com/python/instrumentations/redis.html"}' http://127.0.0.1:5000 {"url": "https://tinyurl.com/22ynqkyf"} > curl -i -X POST -H 'Content-Type: application/json' -d '{"url": "https://tinyurl.com/22ynqkyf"}' http://127.0.0.1:5000 {"url": "https://docs.appsignal.com/python/instrumentations/redis.html"}

Feel free to repeat the command with different URLs to see how the URL shortener works. You can also check the Redis database to see the data stored there:

Shell
> redis-cli GET https://tinyurl.com/22ynqkyf "https://docs.appsignal.com/python/instrumentations/redis.html"

That’s it! You have successfully instrumented Redis in a Python application using AppSignal.

You can now monitor the performance of your Redis calls and get insights, which we will cover in the next section.

Check Latencies in AppSignal

If you go to your AppSignal dashboard, you will see the performance metrics for API calls under Performance:

Performance dashboard

There is an Actions section that shows API call latencies. You can keep track of P95s, P90s, and other metrics to ensure your application is performing well. You can also set up alerts to notify you if the latencies are too high.

API latencies

To check for Redis latencies, you can go to Slow queries under Performance. There, you will see the latencies of the Redis calls listed by command.

Redis latencies

The entire event is captured, including the command, the duration, and the backtrace.

Redis event

To set up alerts, go to the anomaly detection section of AppSignal:

Anomaly detection

There, you can configure triggers for latencies in Redis calls. You can set up alerts for P95s and P90s to notify you if the latencies exceed a certain threshold.

Simply click on the Add Trigger button and configure the trigger:

Add trigger

You can set a threshold, duration, and notification method for an alert.

Read more about AppSignal's instrumentation for Redis.

And that's that!

Wrapping Up

In this tutorial, we learned how to instrument Redis in a Python application using AppSignal. We created a simple URL shortener that stores data in a local instance of Redis and monitored the performance of the Redis calls in our dashboard in AppSignal. We also learned how to check Redis call latencies and set up alerts.

Happy coding!

Wondering what you can do next?

Finished this article? Here are a few more things you can do:

  • Share this article on social media
Camilo Reyes

Camilo Reyes

Our guest author Camilo is a Software Engineer from Houston, Texas. He’s passionate about JavaScript and clean code that runs without drama. When not coding, he loves to cook and work on random home projects.

All articles by Camilo Reyes

Become our next author!

Find out more

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps