

# Enabling the agent with code
<a name="python-code-change"></a>

If your application runs on a platform other than Lambda, install `codeguru_profiler_agent` through `pip`.

```
pip install codeguru_profiler_agent
```

You can configure the agent by passing different parameters to the `Profiler` object.


| Option | Constructor argument | Details | 
| --- | --- | --- | 
|  Profiling group name (required)  |  `profiling_group_name="MyProfilingGroup"`  |  The name of the profiling group to send the data into. The Profiling group must exist.  | 
|  Region  |  `region_name="eu-west-2"`  |  Use this if your application is not running in the same region as the one where the profiling group was created.  | 
|  AWS session  |  `aws_session=boto3.session.Session()`  |  The session object that should be used to target the CodeGuru Profiler backends. Use this if you want to use different credentials or region than the default one. See [boto3 session](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/session.html) for more information.  | 

Start the agent from one place in your application. We recommend you start the agent in your startup code. Only one Profiler object can be started at the time. The following is a runtime example.

```
from codeguru_profiler_agent import Profiler
from boto3.session import Session
...
custom_session = Session(profile_name='dev', region_name='us-east-1')
Profiler(profiling_group_name='MyProfilingGroup', aws_session=custom_session).start()
start_application()
...
```

You can find the sample code for the following examples in [ Amazon CodeGuru Profiler Python Demo Applications](https://github.com/aws-samples/aws-codeguru-profiler-python-demo-application).

The following is an example of a simple application that sets your profiling group name to `MyProfilingGroup`.

```
from codeguru_profiler_agent import Profiler

if __name__ == '__main__':
    Profiler(profiling_group_name='MyProfilingGroup').start()
    start_application()
```

## Supported web components
<a name="supported-languages"></a>

The following topics provide code that you can add to your application to enable the Amazon CodeGuru Profiler agent.

**Topics**
+ [Django](python-django.md)
+ [Flask](python-flask.md)
+ [WSGI servers](python-wsgi.md)

# Django
<a name="python-django"></a>

Start the profiler in your settings file. This is usually the file that you’re setting for `DJANGO_SETTINGS_MODULE`. Then, start your application as usual.

The following is an example that you can add to `settings.py`

```
from codeguru_profiler_agent import Profiler
Profiler(profiling_group_name='MyProfilingGroup').start()
```

Set the following in your `wsgi.py` file. This example is for a module named `mysite`. Your files may be in a different location.

```
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
```

Set the following in your `settings.py` file.

```
Profiler(profiling_group_name='MyProfilingGroup').start()
```

# Flask
<a name="python-flask"></a>

Start the profiler based on the configuration for your web server. For an example with gunicorn, see [WSGI servers](python-wsgi.md).

# WSGI servers
<a name="python-wsgi"></a>

Start the profiler based on the configuration for your web server.

## uWSGI
<a name="w2aac16c13c27b5"></a>

Configure CodeGuru Profiler in your `wsgi.py` file. Then, start your application by adding the `--enable-threads` and `--lazy-apps` parameters to your uWSGI startup configuration. These are required for CodeGuru Profiler to run in your uWSGI applications.

```
uwsgi --http :8000 --chdir . --wsgi-file wsgi.py --enable-threads --lazy-apps --workers=4
```

## gunicorn
<a name="w2aac16c13c27b7"></a>

Configure CodeGuru Profiler in your `post-fork` method. Then start the application as usual.

```
def post_fork(server, worker):
    server.log.info('Starting profiler for {} in {}'.format(os.getpid(), threading.get_ident()))
    worker.profiler = Profiler(profiling_group_name='MyProfilingGroup')
    worker.profiler.start()
    server.log.info('Profiler started running for worker pid {}: master pid {}.'.format(os.getpid(), worker.ppid))
```

## Apache
<a name="w2aac16c13c27b9"></a>

For Apache (httpd) with `mod_wsgi` module, use the same wsgi configuration. Make sure the `wsgi.py` file is configured to be visible in the `httpd.conf` file.

```
<Directory [to_be_replaced]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
```

Start your application with `apachectl start`.