DEV Community

Cover image for Set up real-time error notifications for your python applications in 15 minutes or less
Kyle Mistele
Kyle Mistele

Posted on

Set up real-time error notifications for your python applications in 15 minutes or less

What happens when things go bump in the night?

What happens when your cloud-based python application runs into an error that affects your users? Do you get notified? If you want your users to stay your users and not become your competitor's users, it's crucial to have a monitoring and notification system in place. Today I'm going to show you how to set up a solution for just that in 15 minutes or less.

Cloud technologies have plenty of advantages, but they have plenty of disadvantages too. One particular issue that I have spent more time on than I care to admit is hunting down errors. The distributed and decentralized nature of today's cloud-based applications can make identifying errors hard, and finding the root cause even harder.

A friend and I were using python with serverless tech to build microservices, and I frequently found myself having to dig through a dozen different logs before I could figure out where the error actually happened—if I even noticed that it had happened at all.

That's why we built codelighthouse.io - to help developers find and fix errors faster.

Today I'm going to show you how you can use codelighthouse.io to get real-time application error notifications sent straight to developers.

Getting Started

CodeLighthouse works by plugging a Python SDK into your code that automatically catches uncaught exceptions. The SDK also provides some other neat functionalities that I'll review more in-depth.

Installing the CodeLighthouse SDK

Our SDK is hosted on PyPi, and you can find detailed documentation on our docs site.

Installing the SDK with Pip couldn't be easier:

pip install codelighthouse
Enter fullscreen mode Exit fullscreen mode

If you're using Pip for your dependency management, you'll probably also want to add our package to your requirements.txt file:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Getting your API Key

To get started with the SDK, you'll need to sign up for a free account at codelighthouse.io. Once you sign up, you'll be redirected to your admin dashboard where you can find the organization name you signed up with and your API key:
admin_organization_dashboard

Go ahead and note both of these down. We recommend copying/pasting the API key straight from the admin panel to avoid typing errors, and we provided a handy link right below it to do exactly that.

Configuring the SDK

Importing and configuring the SDK is super easy:

# IMPORT CODELIGHTHOUSE
from codelighthouse import CodeLighthouse

# INSTANTIATE THE ERROR CATCHER
lighthouse = CodeLighthouse(
    organization_name="your organization name",
    x_api_key="your API Key",
    default_email="the email you signed up with",
    resource_group="serverless-applications",
    resource_name="notifications-app"
)
Enter fullscreen mode Exit fullscreen mode

Note that your organization_name and x_api_key are the values you copied down earlier. You can find them in your admin panel here.

The default_email should be the email address you signed up with.

Inviting Users

We designed CodeLighthouse with the complexity of distributed agile teams in mind, so collaboration is a key design feature. You can invite additional users to your CodeLighthouse organization via the user management page. Once they accept the invitation, you can choose to send the error notifications from the application to them by specifying their email address in default_email instead. Users in your organization can log in and configure their notification settings and view errors in the error feed.

Once you've got the SDK imported into your code, you have a couple of options on how to use it:

The Global Exception Handler

By default, CodeLighthouse will automatically catch all uncaught exceptions and unhandled promise rejections. Application error notifications will be sent to the user specified by the default_email configuration option. This can be you, or another user in your CodeLighthouse organization.

The global exception handler can be disabled by passing the keyword argument send_uncaught_exceptions=False to the CodeLighthouse configuration.

It's important to note that this might not always behave as expected if you're using frameworks like Flask since they often implicitly provide their own error handlers. For example, Flask will catch exceptions inside of routes and handle them before they reach our global exception handler. Fortunately, we have a solution.

The Error Catcher Decorator

You can use the error catcher decorator to mark function owners that should receive notifications for errors within individual functions or application routes. In the decorator, specify the email address of the user in your organization who should receive the notification.

# make sure you've imported and configured the SDK!
@lighthouse.error_catcher(email="alice@codelighthouse.io")
def some_function():
  do_some_thing()
  print("Did something!")
Enter fullscreen mode Exit fullscreen mode

From the docs:

Note that the CodeLighthouse decorator must be inside of decorators used for web framework routing (Flask, Pyramid, etc.). Alternatively, using app.add_url_rule() instead of the @app.route() decorator will work for Flask apps and blueprints.

Sending Errors Manually

Of course, we anticipate that many developers will already be performing exception handling in their code, but may want to send & receive notifications for those handled exceptions anyways. Our SDK provides an easy way for you to do this as well:

# make sure you've imported and configured the SDK!
def some_function():
    try:
        call_a_broken_function()
    except NameError as e:
        lighthouse.error(e, email="bob@codelighthouse.io")
Enter fullscreen mode Exit fullscreen mode

As the example demonstrates, you can either send notifications to the default user specified in the SDK configuration, or to another user in your CodeLighthouse organization. You can view and invite additional users to your CodeLighthouse organization on the user management page in your dashboard.

Still want to know more?

Do you have any questions? Are you looking for tech support, support for another language, or a plan tailored specifically to your organization's needs? Check out our documentation page, reach out to us at hello@codelighthouse.io, or visit our contact page!

I look forward to hearing what y'all think in the comments below!

Top comments (0)