Getting Started

Learn how to add and initialize the Sentry SDK for a sample backend application.

In this tutorial, you will import the backend app source code into your local development environment, add the Sentry SDK, and initialize it.

If you're using your own source code, you can skip this tutorial and either:

  1. The demo app source code requires a Python development environment to build, install, and run the application. Make sure that you have the following in place:

  2. To start monitoring errors in your application you'll need to create a new project in your Sentry account. Check out our frontend tutorial to learn more about how to create a project and define alert rules.

  1. Open the sample code repository on GitHub.

  2. Click on "Fork" and select the target GitHub account you wish this repository to be forked in to.

  3. Once the fork is complete, click on "Clone or download" and copy the repository URL. You can do this using HTTPS, SSH or via GitHub CLI.

    Clone Repository

  4. Clone the forked repository to your local environment:

    Copied
    > git clone <repository HTTPS or SSH url>
    
  5. Now that the sample code is available locally, open the backend-monitoring project in your preferred code editor.

Sentry can help you resolve your errors faster by suggesting a suspect commit that might have introduced the error into your codebase. This is enabled by configuring commit tracking. Integrating your source code management solution and adding your code repositories is required to enable commit tracking. Learn more in our releases documentation.

  1. Open your Sentry account and navigate to Settings > Integrations to enable the GitHub integration and add your backend-monitoring repository. For more detailed information, follow the steps described in our GitHub documentation.

Sentry captures data by using a platform-specific SDK within your application runtime. To use the SDK, import, initialize, and configure it in your source code.

  1. To start working with the SDK in our Django app, we install the sentry-sdk by defining the dependency in the requirements.txt file. The SDK documentation and release information are available in the Sentry SDK GitHub repository.

  2. Open the settings.py file (located under _./backend-monitoring/myproject/settings.py). This is where we initialize and configure the Sentry SDK in our application.

  3. After importing the Sentry SDK to the app, it is important to import the Sentry Django integration as well. Integrations extend the functionality of the SDK for some common frameworks and libraries.

    Copied
     import sentry_sdk
     from sentry_sdk.integrations.django import DjangoIntegration
    
  4. In the Sentry SDK configuration, enter the DSN key value you copied from the project created in the project creation tutorial.

    Copied
    sentry_sdk.init(
        dsn="YOUR_DSN",
        integrations=[DjangoIntegration()],
    )
    

To build and run the demo application on your localhost:

  1. Open a shell terminal and change directory to the backend-monitoring project root folder.

  2. If you haven't installed Python3, do so by running the following:

    Copied
     brew install python3
    
  3. Install virtualenv and virtualenvwrapper:

    Copied
     pip3 install virtualenv virtualenvwrapper
     echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
     exec bash
    
  4. Install Sentry's command-line tool to use release tracking and GitHub integration for commit data:

    Copied
     npm install -g @sentry/cli
    
  5. Setup and activate a Python 3 virtual environment in the project root.

    Copied
     mkvirtualenv --python=python3 sentry-demo-django
    

    You can name the virtual environment whatever you need for your project. In our case, we named it "sentry-demo-django".

  6. To activate the virtual environment, run:

    Copied
     workon sentry-demo-django
    
  7. Open the Makefile included in the project's root folder. The file is used here to mimic a CI/CD flow.

  8. Follow the deploy target execution flow.

    Notice that in addition to installing Python requirements and running the server, we also use the sentry-cli to create a new Sentry release, and associate commits to that release. Sentry will lookup through those commits when suggesting a suspect commit for your project issues. Commands mentioned within the Makefile will be explained in detail in the next part of the tutorial, Configuration Options.

  9. To execute the sentry-cli commands, follow the instructions described here to obtain the values for your SENTRY_AUTH_TOKEN, SENTRY_ORG, and SENTRY_PROJECT environment variables.

    Makefile Config

    The sentry-cli can be configured by providing these values either through environment variables or through a dedicated configuration file. Learn more in Sentry CLI > Configuration and Authentication.

  10. Run the following command to install the required Python libraries, set up the Sentry Release, and run the Django server:

    Copied
    make deploy
    

    Deploy & Serve

    In the terminal, notice that a new release is created and commits are associated with it. Once the deploy finishes successfully, you'll see the confirmation in your terminal

Configuration Options

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").