---
title: Install the Python agent in GAE flexible environment
source: https://docs.newrelic.com/docs/apm/agents/python-agent/hosting-services/install-python-agent-gae-flexible-environment
---

With the [Python agent](https://docs.newrelic.com/docs/agents/python-agent/getting-started/introduction-new-relic-python), you can monitor applications that reside in the [Google App Engine (GAE) flexible environment](https://cloud.google.com/appengine/docs/flexible/python/). Adding agent data to your GAE flex app gives you insight into the health and performance of your app and extends GAE with metrics you can view in the New Relic UI.

This document explains how to add agent data to your GAE flex app using either of these methods:

-   Google App Engine's ["native mode"](https://docs.newrelic.com/docs/accounts-partnerships/partnerships/google-cloud-platform-gcp/google-app-engine-environment#native-mode) installation with a standard GAE runtime
-   Docker installation using a [custom runtime](https://cloud.google.com/appengine/docs/flexible/custom-runtimes/about-custom-runtimes)

## Deploy using GAE's native support [#native]

When using Google App Engine ["native mode"](https://docs.newrelic.com/docs/accounts-partnerships/partnerships/google-cloud-platform-gcp/google-app-engine-environment#native-mode) installation, you provide your app code and an `app.yaml` file. Google App Engine then deploys to a standard prebuilt Docker image.

For example, to deploy with native support for a Flask/Django app:

1.  Follow standard procedures to [install the Python agent](https://docs.newrelic.com/docs/agents/python-agent/getting-started/python-agent-quick-start#install), including your license key.
2.  Set the [`NEW_RELIC_CONFIG_FILE`](https://docs.newrelic.com/docs/agents/python-agent/installation-configuration/python-agent-configuration#agent-configuration-file) as an environment variable pointing to `newrelic.ini`.

Once the agent and configuration file have been installed, the Python agent can automatically monitor applications that reside in the GAE flexible environment. Wait until the deployment completes, then view your GAE flex app data in the [APM Summary page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/apm-overview-page).

## Build a custom runtime using Docker [#build-runtime]

See [Google's documentation for building custom runtimes](https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build). This example describes how to add agent data to your GAE flex app by building a custom runtime for Docker.

For more information about deploying and configuring your Node.js app in the GAE flexible environment, see:

-   [Our GAE flex examples on Github](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/flexible) for Python
-   [Google App Engine's documentation](https://cloud.google.com/appengine/docs/flexible/python/) for Python
-   [Google App Engine's tutorials](https://cloud.google.com/appengine/docs/flexible/python/tutorials) to deploy a Python app

**1. Set up the GAE project and install dependencies**

When building a custom runtime using Docker, set the [`NEW_RELIC_CONFIG_FILE`](https://docs.newrelic.com/docs/agents/python-agent/installation-configuration/python-agent-configuration#agent-configuration-file) as an environment variable pointing to the Dockerfile instead of to your Python app's `newrelic.ini`.

1.  Follow standard procedures to [install the Python agent](https://docs.newrelic.com/docs/agents/python-agent/getting-started/python-agent-quick-start#install), including your license key.
2.  Follow Google App Engine procedures Python to create a [Google Cloud Platform project](https://cloud.google.com/appengine/docs/flexible/python/managing-projects-apps-billing#create), create an App Engine application, and complete other prerequisites for the [Google Cloud SDK](http://cloud.google.com/appengine/docs/flexible/python/download).

    The Google Cloud SDK also provides the `gcloud` command line tool to manage and deploy GAE apps.

**2. Configure your app.yaml**

The `app.yaml` configuration file is required for a GAE flexible environment app with a custom runtime. At a minimum, make sure it contains:

````yml
env: flex
runtime: custom
```

````

**3. Configure a Dockerfile**

The [Dockerfile](http://docs.docker.com/engine/reference/builder/) defines the Docker image to be built and is required for a GAE flexible environment app. The following Dockerfile example shows the Python agent installed for an application served with gunicorn.

These procedures are similar to the [Python quick start guide](https://docs.newrelic.com/docs/agents/python-agent/getting-started/python-agent-quick-start). The Dockerfile will contain customer-specific code, including Python version, installation requirements, etc).

````dockerfile
# [START dockerfile]
FROM gcr.io/google_appengine/python

# Install the fortunes binary from the debian repositories.
RUN apt-get update && apt-get install -y fortunes

# Optional: Change the -p argument to use a particular version of Python.
RUN virtualenv /env -p python3.9

# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

CMD NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program gunicorn -b :$PORT main:app
# [END dockerfile]
```

````

**4. Deploy Docker image to initialized GAE flexible environment**

1.  To deploy your Docker image to your [initialized GAE flexible environment](https://cloud.google.com/sdk/docs/initializing), run the following command:

    ```sh
    gcloud app deploy
    ```
2.  Wait until the deployment completes.
3.  To open the app in the browser, run the following command:

    ```sh
    gcloud app browse
    ```
4.  To view your GAE flex app data, go to the [APM Summary page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/apm-overview-page).

## Recommendation: Disable health checks [#health-checks]

Google App Engine sends [periodic health check requests](https://cloud.google.com/appengine/docs/flexible/python/how-instances-are-managed) to confirm that an instance has been successfully deployed, and to check that a running instance maintains a healthy status. A health check is an HTTP request to the URL `/_ah/health`.

If you create a custom runtime, your app must be able to handle a large number of health check requests. Otherwise, your app data may not display correctly in APM.

Recommendation: Configure your `app.yaml` to [disable health checks](https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml#health_checks) by adding:

```yml
health_check:
  enable_health_check: False
```

## Get agent troubleshooting logs from GAE [#agent-logs]

Use these resources to troubleshoot your GAE flex environment app:

-   To connect to the GAE instance and start a shell in the Docker container running your code, see [Debugging an instance](https://cloud.google.com/appengine/docs/flexible/python/debugging-an-instance).
-   To redirect Python agent logs to [Stackdriver](http://cloud.google.com/logging/docs/view/logs_viewer_v2) in the [Cloud Platform Console](https://cloud.google.com/compute/docs/console), add the following statement to the `newrelic.ini` configuration:

    ```ini
    log_file = stderr
    ```
-   To view the logs, use the [Cloud Platform Console's Log Viewer](https://cloud.google.com/appengine/docs/flexible/php/writing-application-logs).
