· 3 min read

Ensuring Smooth Redirects with Dockerized Monitoring

Learn how to monitor redirects with Python, Docker, and Checkson.

Learn how to monitor redirects with Python, Docker, and Checkson.

Sometimes it becomes necessary to transition to a new domain, e.g. because you rebranded your product. This comes with the crucial task of ensuring a seamless user experience during the migration process. One vital aspect is to set up redirects from the old domain to the new one, directing all visitors to the new website. To ensure the redirect works flawlessly, we can leverage Python, Docker, and a helpful tool called Checkson. In this blog post, we’ll walk you through the process of monitoring the redirect using a Dockerized environment.

Python Code for Redirect Monitoring

First, let’s take a look at the Python code snippet responsible for monitoring the redirect. This code will be used to check if the redirect itself works correctly and to ensure other important details, for example that the TLS certificate is not expired.

import requests
import sys

EXPECTED_TARGET_URL = 'https://www.newcompanyname.com'

response = requests.get('https://www.oldcompanyname.com', allow_redirects=False)

if response.status_code != 301:
    print('Not redirected')
    sys.exit(1)

target_url = response.headers['Location']
if target_url != EXPECTED_TARGET_URL:
    print(f'Incorrect redirect location: {target_url}')
    sys.exit(1)

print('Redirected successfully')
sys.exit(0)

You can test this Python code locally to ensure it functions as expected:

python3 main.py

You can check the exit code of the last command like this:

echo $?

If the redirect works, this will return a 0 (success!) or 1 (failure).

Dockerizing the Redirect Monitoring

To package our monitoring process into a Docker image and push it to a Docker registry (e.g., Docker Hub), we need to create a Dockerfile to define the necessary configuration and dependencies for our Docker image.

FROM python:3.10-slim

COPY ../introduction /app
WORKDIR /app
RUN pip install -r requirements.txt

ENTRYPOINT ["python", "app.py"]

Now, let’s build the Docker image using the following bash command:

docker build . -t myuser/redirect-check:1.0.0

For a final check before deployment, run the Docker image locally with the following command:

docker run --rm -ti myuser/redirect-check:1.0.0

Setting up Monitoring with Checkson

Now that we have our Dockerized monitoring image, it’s time to set up the redirect check using Checkson. You can install the Checkson command line tool by following the instructions on GitHub. As an alternative to the CLI, you can also use the web UI.

After installation of the CLI, you can configure the check via the command line using the following commands:

checkson create redirect-check \\
  --docker-image myuser/redirect-check:1.0.0 \\
  --email me@example.com \\
  --failure-threshold 2 \\
  --check-interval 10

Let’s break this down:

  1. Creating the Check: We create a new check named redirect-check.
  2. Configuring the Docker Image: The --docker-image flag specifies the Docker image we built earlier to use for monitoring the redirect.
  3. Notification Channel: We implicitly configure an Email notification channel and associate it with the check. This way, if the redirect check fails, notifications will be sent to the designated recipients.
  4. Failure Threshold: The parameter --failure-threshold indicates that there must be two consecutive runs of the check with an exit code other than 0 for the check to be considered unsuccessful and trigger notifications. Using a failure threshold helps reduce false positives, ensuring you receive notifications only when necessary.
  5. Check Interval: The parameter --check-interval instructs Checkson to execute this check every 10 minutes in its cloud environment.

By following these steps and utilizing the power of Checkson, you can seamlessly monitor your redirect during the rebranding process, ensuring a smooth transition for your users.

In conclusion, when making significant changes like rebranding and domain transitions, diligent monitoring becomes essential. Leveraging Python and Docker to create a monitoring solution, along with Checkson, gives you the ability to detect any redirect issues promptly. With this reliable monitoring in place, you can confidently proceed with your rebranding efforts, providing an smooth user experience throughout the process.

Note: A working example of this check can be found on GitHub

Back to Blog