Quickstart for Stackdriver Debugger

This Quickstart shows how to use the Stackdriver Debugger to inspect the state of a simple Python app running on App Engine Standard Environment. It demonstrates how to capture the call stack and local variables using debug snapshots and how to inject logging into the running app using debug logpoints.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Make sure that the following software is installed on your local system:

Deploy to App Engine

Start by deploying a Python 2.7 app to App Engine Standard.

  1. Clone the project to a local repository.

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples
    
  2. Go to the directory that contains the sample code.

    cd python-docs-samples/appengine/standard/hello_world
    
  3. Initialize the Google Cloud SDK with your project ID.

    gcloud config set project [your-project-id]
    
  4. Deploy your application.

    gcloud app deploy --version=v1
    
  5. Try your app by sending it a request.

    gcloud app browse
    

And the response should contain a Hello World message. If you have any issues in these steps, refer to the Quickstart for Python App Engine Standard Environment.

View Deployed Sources

Once you deploy your Python app, you can view the deployed sources on the Google Cloud Platform Console Debug page.

  1. Navigate to the Debug page in the Google Cloud Platform Console.

  2. Make sure you have the correct project selected.

  3. Make sure default - v1 (100%) is selected in the menu at the top. This indicates which service/version of your app you will be debugging.

  4. Make sure Deployed Files is selected in the menu at the left. This panel displays the source code of the Python app you just deployed.

  5. Select main.py from the list of deployed files to see its contents.

You are now ready to take debug snapshots and inject debug logpoints.

Take a Debug Snapshot

A debug snapshot captures the local variables and call stack at a specific line location in your source code. Select the Snapshot tab in the panel on the right to take debug snapshots.

  1. Click the line number for the first line inside the get function in the MainPage class. A blue arrow appears, indicating that a snapshot location is set. The results panel at the right displays "Waiting for snapshot to hit".

    A snapshot location set on line 20

  2. Select the Snapshot History panel at the bottom of the page to see that your snapshot has been set successfully.

    A snapshot has been set

  3. Send a request to your app to trigger your snapshot.

    gcloud app browse
    
  4. The results panel displays the call stack and the values of local variables in scope at that point in the code and at that point in time.

    Results panel with local variables

  5. Add the following expression and set another snapshot.

    self.request.environ['HTTP_USER_AGENT']
    
  6. Send a new request to trigger the new snaps

    gcloud app browse
    
  7. The results panel now also shows the result of your expression. In this case, the value is:

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
    AppleWebKit/537.36 (KHTML, like Gecko)
    Chrome/62.0.3202.94 Safari/537.36
    

Inject a Debug Logpoint

A debug logpoint enables you to inject logging into your running app without restarting it. Select the Logpoint tab in the panel on the right to inject debug logpoints.

  1. Click the line number for the first line inside the get function in the MainPage class.

  2. An inline text box appears. To inject a logpoint, populate it as follows and hit Enter:

    if (true) logpoint("Browser = {self.request.environ['HTTP_USER_AGENT']}")
    

    Debug panel with injected logpoint

  3. Select the Logpoint History panel at the bottom of the page to see that your logpoint has been injected successfully.

    A logpoint has been set

  4. Send a request to your app to trigger the logpoint.

    gcloud app browse
    
  5. Select the Logs panel at the bottom of the page to see the logs printed by your logpoint.

    Logs from logpoint injected to line 20

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Navigate to the Projects page of the GCP Console.

  2. Click the trash can icon () next to your project.

  3. Type your project name to confirm the shutdown.

What's next

Send feedback about...

Stackdriver Debugger Documentation