{"id":23330435,"url":"https://github.com/iopipe/iopipe-python","last_synced_at":"2025-08-10T12:14:02.775Z","repository":{"id":10208569,"uuid":"64508423","full_name":"iopipe/iopipe-python","owner":"iopipe","description":"Python agent for AWS Lambda metrics, tracing, profiling \u0026 analytics","archived":false,"fork":false,"pushed_at":"2022-09-08T09:26:43.000Z","size":1245,"stargazers_count":79,"open_issues_count":9,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-09T06:17:38.576Z","etag":null,"topics":["analytics","aws","aws-lambda","iopipe","iopipe-agent","lambda","monitoring","profiling","python","serverless","telemetry","tracing"],"latest_commit_sha":null,"homepage":"https://www.iopipe.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iopipe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-29T20:25:53.000Z","updated_at":"2024-06-26T19:57:10.000Z","dependencies_parsed_at":"2022-08-19T15:20:45.337Z","dependency_job_id":null,"html_url":"https://github.com/iopipe/iopipe-python","commit_stats":null,"previous_names":[],"tags_count":64,"template":false,"template_full_name":null,"purl":"pkg:github/iopipe/iopipe-python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iopipe%2Fiopipe-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iopipe%2Fiopipe-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iopipe%2Fiopipe-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iopipe%2Fiopipe-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iopipe","download_url":"https://codeload.github.com/iopipe/iopipe-python/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iopipe%2Fiopipe-python/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269721133,"owners_count":24464447,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","aws","aws-lambda","iopipe","iopipe-agent","lambda","monitoring","profiling","python","serverless","telemetry","tracing"],"created_at":"2024-12-20T22:18:03.592Z","updated_at":"2025-08-10T12:14:02.704Z","avatar_url":"https://github.com/iopipe.png","language":"Python","readme":"**✨Special Announcement: We've Joined New Relic Serverless!**\n**Get ready to function faster with full visibility into your serverless applications—and everything else. [Read our founders' note to learn more.](https://read.iopipe.com/founders-note-iopipe-new-relic-acquisition-c15eeda47151)**\n\n# IOpipe Analytics \u0026 Distributed Tracing Agent for Python\n\n[![Build Status][1]][2] [![Code Coverage][3]][4] [![PyPI Version][5]][6] [![Apache 2.0][7]][8] [![Slack][9]][10]\n\n[1]: https://circleci.com/gh/iopipe/iopipe-python.svg?style=svg\n[2]: https://circleci.com/gh/iopipe/iopipe-python\n[3]: https://codecov.io/gh/iopipe/iopipe-python/branch/master/graph/badge.svg\n[4]: https://codecov.io/gh/iopipe/iopipe-python\n[5]: https://badge.fury.io/py/iopipe.svg\n[6]: https://badge.fury.io/py/iopipe\n[7]: https://img.shields.io/badge/License-Apache%202.0-blue.svg\n[8]: https://github.com/iopipe/iopipe-python/blob/master/LICENSE\n[9]: https://img.shields.io/badge/chat-slack-ff69b4.svg\n[10]: https://iopipe.now.sh\n\nThis package provides analytics and distributed tracing for event-driven applications running on AWS Lambda.\n\n- [Installation](https://github.com/iopipe/iopipe-python#installation)\n- [Usage](https://github.com/iopipe/iopipe-python#usage)\n  - [Configuration](https://github.com/iopipe/iopipe-python#configuration)\n  - [Reporting Exceptions](https://github.com/iopipe/iopipe-python#reporting-exceptions)\n  - [Custom Metrics](https://github.com/iopipe/iopipe-python#custom-metrics)\n  - [Labels](https://github.com/iopipe/iopipe-python#labels)\n  - [Core Agent](https://github.com/iopipe/iopipe-python#core-agent)\n  - [Disabling Reporting](https://github.com/iopipe/iopipe-python#disabling-reporting)\n  - [Step Functions](https://github.com/iopipe/iopipe-python#step-functions)\n- [Plugins](https://github.com/iopipe/iopipe-python#plugins)\n  - [Event Info Plugin](https://github.com/iopipe/iopipe-python#event-info-plugin)\n  - [Logger Plugin](https://github.com/iopipe/iopipe-python#logger-plugin)\n  - [Profiler Plugin](https://github.com/iopipe/iopipe-python#profiler-plugin)\n  - [Trace Plugin](https://github.com/iopipe/iopipe-python#trace-plugin)\n    - [Auto DB Tracing](https://github.com/iopipe/iopipe-python#auto-db-tracing)\n    - [Auto HTTP Tracing](https://github.com/iopipe/iopipe-python#auto-http-tracing)\n  - [Creating Plugins](https://github.com/iopipe/iopipe-python#creating-plugins)\n- [Supported Python Versions](https://github.com/iopipe/iopipe-python#supported-python-versions)\n- [Lambda Layers](https://github.com/iopipe/iopipe-python#lambda-layers)\n- [Framework Integration](https://github.com/iopipe/iopipe-python#framework-integration)\n  - [Chalice](https://github.com/iopipe/iopipe-python#chalice)\n  - [Serverless](https://github.com/iopipe/iopipe-python#serverless)\n  - [Zappa](https://github.com/iopipe/iopipe-python#zappa)\n  - [Accessing Context](https://github.com/iopipe/iopipe-python#accessing-context)\n\n- [Contributing](https://github.com/iopipe/iopipe-python#contributing)\n- [Running Tests](https://github.com/iopipe/iopipe-python#running-tests)\n- [License](https://github.com/iopipe/iopipe-python#license)\n\n## Installation\n\nWe expect you will import this library into an existing (or new) Python project intended to be run on AWS Lambda.  On Lambda, functions are expected to include module dependencies within their project paths, thus we use `-t $PWD`. Users building projects with a requirements.txt file may simply add `iopipe` to their dependencies.\n\nFrom your project directory:\n\n```bash\n$ pip install iopipe -t .\n\n# If running locally or in other environments _besides_ AWS Lambda:\n$ pip install iopipe[local] -t .\n```\n\nYour folder structure for the function should look similar to:\n\n```\nindex.py # contains your lambda handler\n  /iopipe\n    - __init__.py\n    - iopipe.py\n  /requests\n    - __init__.py\n    - api.py\n    - ...\n```\n\nInstallation of the requests library is necessary for local dev/test, but not when running on AWS Lambda as this library is part of the default environment via the botocore library.\n\nMore details about lambda deployments are available in the [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html).\n\n## Usage\n\nSimply use our decorator to report metrics:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe('your project token here')\n\n@iopipe\ndef handler(event, context):\n    pass\n```\n\n The agent comes preloaded with the [Event Info](https://github.com/iopipe/iopipe-python#event-info-plugin), [Profiler](https://github.com/iopipe/iopipe-python#profiler-plugin) and [Trace](https://github.com/iopipe/iopipe-python#trace-plugin) plugins. See the relevant plugin sections for usage.\n\n### Configuration\n\nThe following may be set as kwargs to the IOpipe class initializer:\n\n#### `token` (string: required)\n\nYour IOpipe project token. If not supplied, the environment variable `$IOPIPE_TOKEN` will be used if present. [Find your project token](https://dashboard.iopipe.com/install)\n\n#### `debug` (bool: optional = False)\n\nDebug mode will log all data sent to IOpipe servers. This is also a good way to evaluate the sort of data that IOpipe is receiving from your application. If not supplied, the environment variable `IOPIPE_DEBUG` will be used if present.\n\n#### `enabled` (bool: optional = True)\n\nConditionally enable/disable the agent. For example, you will likely want to disabled the agent during development. The environment variable `$IOPIPE_ENABLED` will also be checked.\n\n#### `network_timeout` (int: optional = 5000)\n\nThe number of milliseconds IOpipe will wait while sending a report before timing out. If not supplied, the environment variable `$IOPIPE_NETWORK_TIMEOUT` will be used if present.\n\n#### `timeout_window` (int: optional = 150)\n\nBy default, IOpipe will capture timeouts by exiting your function 150 milliseconds early from the AWS configured timeout, to allow time for reporting. You can disable this feature by setting `timeout_window` to `0` in your configuration. If not supplied, the environment variable `$IOPIPE_TIMEOUT_WINDOW` will be used if present.\n\n### Reporting Exceptions\n\nThe IOpipe decorator will automatically catch, trace and reraise any uncaught exceptions in your function. If you want to trace exceptions raised in your case, you can use the `.error(exception)` method. This will add the exception to the current report.\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n# Example 1: uncaught exceptions\n\n@iopipe\ndef handler(event, context):\n    raise Exception('This exception will be added to the IOpipe report automatically')\n\n# Example 2: caught exceptions\n\n@iopipe\ndef handler(event, context):\n    try:\n        raise Exception('This exception is being caught by your function')\n    except Exception as e:\n        # Makes sure the exception is added to the report\n        context.iopipe.error(e)\n```\n\nIt is important to note that a report is sent to IOpipe when `error()` is called. So you should only record exceptions this way for failure states. For caught exceptions that are not a failure state, it is recommended to use custom metrics (see below).\n\n### Custom Metrics\n\nYou can log custom values in the data sent upstream to IOpipe using the following syntax:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe\ndef handler(event, context):\n    # the name of the metric must be a string\n    # numerical (int, long, float) and string types supported for values\n    context.iopipe.metric('my_metric', 42)\n```\n\nMetric key names are limited to 128 characters, and string values are limited to 1024 characters.\n\n### Labels\n\nLabel invocations sent to IOpipe by calling the `label` method with a string (limit of 128 characters):\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe\ndef handler(event, context):\n    # the name of the label must be a string\n    context.iopipe.label('this-invocation-is-special')\n```\n\n### Core Agent\n\nBy default, the IOpipe agent comes pre-loaded with all the bundled plugins in `iopipe.contrib.*`. If you prefer to run the agent without plugins or configure which plugins are used, you can use `IOpipeCore`:\n\n```python\nfrom iopipe import IOpipeCore\nfrom iopipe.contrib.trace import TracePlugin\n\n# Load IOpipe with only the trace plugin\niopipe = IOpipeCore(plugins=[TracePlugin()])\n\n@iopipe\ndef handler(event, context):\n    pass\n```\n\n### Disabling Reporting\n\nYou can programmatically disable IOpipe reporting for a single invocation using the `disable` method:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe\ndef handler(event, context):\n    if some_condition:\n        context.iopipe.disable()\n```\n\nReporting will be re-enabled on the next invocation.\n\n### Step Functions\n\nIOpipe is compatible with AWS Lambda step functions. To enable step function tracing:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe.step\ndef handler(event, context):\n    pass\n```\n\nThe `@iopipe.step` decorator will enable step function mode, which will collect additional meta data about your step functions.\n\n## Plugins\n\nIOpipe's functionality can be extended through plugins. Plugins hook into the agent lifecycle to allow you to perform additional analytics.\n\n### Event Info Plugin\n\nThe IOpipe agent comes bundled with an event info plugin that automatically extracts useful information from the `event`\nobject and creates custom metrics for them.\n\nHere's an example of how to use the event info plugin:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.eventinfo import EventInfoPlugin\n\niopipe = IOpipe(plugins=[EventInfoPlugin()])\n\n@iopipe\ndef handler(event, context):\n    # do something here\n```\n\nWhen this plugin is installed, custom metrics will be created automatically for the following event source data:\n\n* API Gateway\n* ALB\n* Alexa Skill Kit\n* CloudFront\n* Kinesis\n* Kinesis Firehose\n* S3\n* SES\n* SNS\n* SQS\n* Scheduled Events\n\nNow in your IOpipe invocation view you will see useful event information.\n\n### Logger Plugin\n\n**Note:** This plugin is in beta. Want to give it a try? Find us on [![Slack](https://img.shields.io/badge/chat-slack-ff69b4.svg)](https://iopipe.now.sh).\n\nThe IOpipe agent comes bundled with a logger plugin that allows you to attach IOpipe to the `logging` module so that you can see your log messages in the IOpipe dashboard.\n\nHere's an example of how to use the logger plugin:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.logger import LoggerPlugin\n\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True)])\n\n@iopipe\ndef handler(event, context):\n    context.iopipe.log.info('Handler has started execution')\n```\n\nSince this plugin adds a handler to the `logging` module, you can use `logging` directly as well:\n\n```python\nimport logging\n\nfrom iopipe import IOpipe\nfrom iopipe.contrib.logger import LoggerPlugin\n\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True)])\nlogger = logging.getLogger()\n\n@iopipe\ndef handler(event, context):\n    logger.error('Uh oh')\n```\n\nYou can also specify a log name, such as if you only wanted to log messages for `mymodule`:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.logger import LoggerPlugin\n\niopipe = IOpipe(plugins=[LoggerPlugin('mymodule', enabled=True)])\n```\n\nThis would be equivalent to `logging.getLogger('mymodule')`.\n\nBy default the logger plugin is disabled. You must explicitly set `enabled=True` when instantiating or use the `IOPIPE_LOGGER_ENABLED` environment variable to enable it.\n\nThe default logger plugin log level is `logging.INFO`, but it can be set like this:\n\n```python\nimport logging\n\nfrom iopipe import IOpipe\nfrom iopipe.contrib.logger import LoggerPlugin\n\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True, level=logging.DEBUG)])\n```\n\nPutting IOpipe into `debug` mode also sets the log level to `logging.DEBUG`.\n\nThe logger plugin also redirects stdout by default, so you can do the following:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.logger import LoggerPlugin\n\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True)])\n\n@iopipe\ndef handler(event, context):\n    print('I will be logged')\n```\n\nNow in your IOpipe invocation view you will see log messages for that invocation.\n\nIf you prefer your print statements not to be logged, you can disable this by setting `redirect_stdout` to `False`:\n\n```python\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True, redirect_stdout=False)])\n```\n\n**Note:** Due to a change to the way the python3.7 runtime configures logging, stdout redirection is disabled for this runtime. Use `context.iopipe.log.*` instead.\n\nBy default the logger plugin will log messages to an in-memory buffer. If you prefer to log messages to your Lambda function's `/tmp` directory:\n\n```python\niopipe = IOpipe(plugins=[LoggerPlugin(enabled=True, use_tmp=True)])\n```\n\nWith `use_tmp` enabled, the plugin will automatically delete log files written to `/tmp` after each invocation.\n\n### Profiler Plugin\n\nThe IOpipe agent comes bundled with a profiler plugin that allows you to profile your functions with [cProfile](https://docs.python.org/3/library/profile.html#module-cProfile).\n\nHere's an example of how to use the profiler plugin:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.profiler import ProfilerPlugin\n\niopipe = IOpipe(plugins=[ProfilerPlugin()])\n\n@iopipe\ndef handler(event, context):\n    # do something here\n```\n\nBy default the plugin will be disabled and can be enabled at runtime by setting the `IOPIPE_PROFILER_ENABLED` environment variable to `true`/`True`.\n\nIf you want to enable the plugin for all invocations:\n\n```python\niopipe = IOpipe(plugins=[ProfilerPlugin(enabled=True)])\n```\n\nNow in your IOpipe invocation view you will see a \"Profiling\" section where you can download your profiling report.\n\nOnce you download the report you can open it using pstat's interactive browser with this command:\n\n```bash\npython -m pstats \u003cfile here\u003e\n```\n\nWithin the pstats browser you can sort and restrict the report in a number of ways, enter the `help` command for details. Refer to the [pstats Documentation](https://docs.python.org/3/library/profile.html#module-pstats).\n\n### Trace Plugin\n\nThe IOpipe agent comes bundled with a trace plugin that allows you to perform tracing.\n\nHere's an example of how to use the trace plugin:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.trace import TracePlugin\n\niopipe = IOpipe(plugins=[TracePlugin()])\n\n@iopipe\ndef handler(event, context):\n    context.iopipe.mark.start('expensive operation')\n    # do something here\n    context.iopipe.mark.end('expensive operation')\n```\n\nOr you can use it as a context manager:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe\ndef handler(event, context):\n    with context.iopipe.mark('expensive operation'):\n        # do something here\n```\n\nOr you can use it as a decorator:\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n@iopipe\ndef handler(event, context):\n    @context.iopipe.mark.decorator('expensive operation'):\n    def expensive_operation():\n        # do something here\n\n    expensive_operation()\n```\n\nAny block of code wrapped with `start` and `end` or using the context manager or decorator will be traced and the data collected will be available on your IOpipe dashboard.\n\nBy default, the trace plugin will auto-measure any trace you make. But you can disable this by setting `auto_measure` to `False`:\n\n```python\nfrom iopipe import IOpipe\nfrom iopipe.contrib.trace import TracePlugin\n\niopipe = IOpipe(plugins=[TracePlugin(auto_measure=False)])\n\n@iopipe\ndef handler(event, context):\n    with context.iopipe.mark('expensive operation'):\n        # do something here\n\n    # Manually measure the trace\n    context.iopipe.mark.measure('expensive operation')\n```\n\n#### Auto DB Tracing\n\nThe trace plugin can trace your database requests automatically. To enable this feature, set `auto_db` to `True` or set the `IOPIPE_TRACE_AUTO_DB_ENABLED` environment variable. For example:\n\n```python\niopipe = IOpipe(plugins=[TracePlugin(auto_db=True)])\n```\n\nWith `auto_db` enabled, you will see traces for any database requests you make within your function on your IOpipe dashboard. Currently this feature supports [MongoDB](https://github.com/mongodb/mongo-python-driver), [MySQL](https://github.com/PyMySQL), [PostgreSQL](https://github.com/psycopg/psycopg2) and [Redis](https://github.com/andymccurdy/redis-py).\n\nMongoDB example:\n\n```python\nfrom iopipe import IOpipe\nfrom pymongo import MongoClient\n\niopipe = IOpipe(plugins=[TracePlugin(auto_db=True)])\n\n@iopipe\ndef handler(event, context):\n    client = MongoClient(\"your-host-here\", 27017)\n\n    db = client.test\n    db.my_collection.insert_one({\"x\": 10})\n    db.my_collection.find_one()\n```\n\nRedis example:\n\n```python\nfrom iopipe import IOpipe\nfrom redis import Redis\n\niopipe = IOpipe(plugins=[TracePlugin(auto_db=True)])\n\n@iopipe\ndef handler(event, context):\n    r = redis.Redis(host=\"your-host-here\", port=6379, db=0)\n    r.set(\"foo\", \"bar\")\n    r.get(\"foo\")\n```\n\n#### Auto HTTP Tracing\n\nThe trace plugin can trace your HTTP/HTTPS requests automatically. To enable this feature, set `auto_http` to `True` or set the `IOPIPE_TRACE_AUTO_HTTP_ENABLED` environment variable. For example:\n\n```python\niopipe = IOpipe(plugins=[TracePlugin(auto_http=True)])\n```\n\nWith `auto_http` enabled, you will see traces for any HTTP/HTTPS requests you make within your function on your IOpipe dashboard. Currently this feature only supports the `requests` library, including `boto3` and `botocore` support.\n\nTo filter which HTTP requests are traced use `http_filter`:\n\n```python\ndef http_filter(request, response):\n    if request['url'].startswith('https://www.iopipe.com'):\n        # Exceptions raised will delete the trace\n        raise Exception(Do not trace this URL')\n    # You can also remove data from the trace\n    response['headers'].pop('Content-Type', None)\n    return request, response\n\niopipe = IOpipe(plugins=[TracePlugin(auto_http=True, http_filter=http_filter)])\n```\n\nTo add additional HTTP headers to your ttrace data use `http_headers`:\n\n```python\nhttp_headers = ['Cache-Control', 'Etag']\n\niopipe = IOpipe(plugins=[TracePlugin(auto_http=True, http_headers=http_headers)\n```\n\n## Plugins\n\n### Creating Plugins\n\nTo create an IOpipe plugin you must implement the `iopipe.plugins.Plugin` interface.\n\nHere is a minimal example:\n\n```python\nfrom iopipe.plugins import Plugin\n\n\nclass MyPlugin(Plugin):\n    name = 'my-plugin'\n    version = '0.1.0'\n    homepage = 'https://github.com/iopipe/my-plugin/'\n    enabled = True\n\n    def pre_setup(self, iopipe):\n        pass\n\n    def post_setup(self, iopipe):\n        pass\n\n    def pre_invoke(self, event, context):\n        pass\n\n    def post_invoke(self, event, context):\n        pass\n\n    def post_response(self, response):\n        pass\n\n    def pre_report(self, report):\n        pass\n\n    def post_report(self):\n        pass\n```\n\nAs you can see, this plugin doesn't do much. If you want to see a functioning example of a plugin check out the trace plugin at `iopipe.contrib.trace.plugin.TracePlugin`.\n\n#### Plugin Properties\n\nA plugin has the following properties defined:\n\n- `name`: The name of the plugin, must be a string.\n- `version`: The version of the plugin, must be a string.\n- `homepage`: The URL of the plugin's homepage, must be a string.\n- `enabled`: Whether or not the plugin is enabled, must be a boolean.\n\n#### Plugin Methods\n\nA plugin has the following methods defined:\n\n- `pre_setup`: Is called once prior to the agent initialization; is passed the `iopipe` instance.\n- `post_setup`: Is called once after the agent is initialized; is passed the `iopipe` instance.\n- `pre_invoke`: Is called prior to each invocation; is passed the `event` and `context` of the invocation.\n- `post_invoke`: Is called after each invocation; is passed the `event` and `context` of the invocation.\n- `post_response`: Is called after the invocation response; is passed the `response`value.\n- `pre_report`: Is called prior to each report being sent; is passed the `report` instance.\n- `post_report`: Is called after each report is sent; is passed the `report` instance.\n\n## Supported Python Versions\n\nThis package supports Python 2.7, 3.6 and 3.7, the runtimes supported by AWS Lambda.\n\n## Lambda Layers\n\nIOpipe publishes [AWS Lambda Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html) which are publicly available on AWS. Using a framework that supports lambda layers (such as SAM or Serverless), you can use the following ARNs for your runtime:\n\n* python3.6, python3.7: `arn:aws:lambda:$REGION:146318645305:layer:IOpipePython:$VERSION_NUMBER`\n* python2.7: `arn:aws:lambda:$REGION:146318645305:layer:IOpipePython27:$VERSION_NUMBER`\n\nWhere `$REGION` is your AWS region and `$VERSION_NUMBER` is an integer representing the IOpipe release. You can get the version number via the [Releases](https://github.com/iopipe/iopipe-python/releases) page.\n\nThen in your SAM template (for example), you can add:\n\n```yaml\nGlobals:\n  Function:\n    Layers:\n        - arn:aws:lambda:us-east-1:146318645305:layer:IOpipePython:1\n```\n\nAnd the IOpipe library will be included in your function automatically.\n\nYou can also wrap your IOpipe functions without a code change using layers. For example, in your SAM template you can do the following:\n\n```yaml\nResources:\n  YourFunctionere:\n    Type: 'AWS::Serverless::Function'\n    Properties:\n      CodeUri: path/to/your/code\n      # Automatically wraps the handler with IOpipe\n      Handler: iopipe.handler.wrapper\n      Runtime: python3.6\n      Environment:\n        Variables:\n          # Specifies which handler IOpipe should run\n          IOPIPE_HANDLER: path.to.your.handler\n          IOPIPE_TOKEN: 'your token here'\n```\n\nWe also have an [example app](https://github.com/iopipe/iopipe-python/tree/master/acceptance/serverless-layers) using layers with Serverless. It also demonstrates how to use layers without a code change.\n\n**NEW:** We have also released a [Serverless Plugin](https://www.github.com/iopipe/serverless-iopipe-layers) to do all this for you automatically.\n\n## Framework Integration\n\nIOpipe integrates with popular serverless frameworks. See below for examples. If you don't see a framework you'd like to see supported, please create an issue.\n\n### Chalice\n\nUsing IOpipe with the [Chalice](https://github.com/aws/chalice) framework is easy. Wrap your `app` like so:\n\n```python\nfrom chalice import Chalice\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\napp = Chalice(app_name='helloworld')\n\n@app.route('/')\ndef index():\n    return {'hello': 'world'}\n\n# Do this after defining your routes\napp = iopipe(app)\n```\n\n### Serverless\n\nUsing IOpipe with [Serverless](https://github.com/serverless/serverless) is easy.\n\nFirst, we highly recommend the [serverless-python-requirements](https://github.com/UnitedIncome/serverless-python-requirements) plugin:\n\n```bash\n$ npm install --save-dev serverless-python-requirements\n```\n\nThis plugin will add `requirements.txt` support to Serverless. Once installed, add the following to your `serverless.yml`:\n\n```yaml\nplugins:\n  - serverless-python-requirements\n```\n\nThen add `iopipe `to your `requirements.txt`:\n\n```bash\n$ echo \"iopipe\" \u003e\u003e requirements.txt\n```\n\nNow Serverless will `pip install -r requirements.txt` when packaging your functions.\n\nKeep in mind you still need to add the `@iopipe` decorator to your functions. See [Usage](https://github.com/iopipe/iopipe-python#usage) for details.\n\nBe sure to check out the [serverless-python-requirements](https://github.com/UnitedIncome/serverless-python-requirements) `README` as the plugin has a number of useful features for compiling AWS Lambda compatible Python packages.\n\nIf you're using the [serverless-wsgi](https://github.com/logandk/serverless-wsgi) plugin, you will need to wrap the wsgi handler it bundles with your function.\n\nThe easiest way to do this is to create a `wsgi_wrapper.py` module in your project's root with the following:\n\n```python\nimport imp\n\nfrom iopipe import IOpipe\n\nwsgi = imp.load_source('wsgi', 'wsgi.py')\n\niopipe = IOpipe()\nhandler = iopipe(wsgi.handler)\n```\n\nThen in your `serverless.yml`, instead of this:\n\n```yaml\nfunctions:\n  api:\n    handler: wsgi.handler\n    ...\n```\n\nUse this:\n\n```yaml\nfunctions:\n  api:\n    handler: wsgi_wrapper.handler\n    ...\n```\n\n### Zappa\n\nUsing IOpipe with [Zappa](https://github.com/Miserlou/Zappa) is easy. In your project add the following:\n\n```python\nfrom iopipe import IOpipe\nfrom zappa.handler import lambda_handler\n\niopipe = IOpipe()\nlambda_handler = iopipe(lambda_handler)\n```\n\nThen in your `zappa_settings.json` file include the following:\n\n```js\n{\n  \"lambda_handler\": \"module.path.to.lambda_handler\"\n}\n```\n\nWhere `module-path.to.lambda_handler` is the Python module path to the `lambda_handler` you created above. For example, if you put it in `myproject/__init__.py` the path would be `myproject.lambda_handler`.\n\n### Accessing Context\n\nIf the framework you're using makes it non-trivial to access the Lamba context, you can use `iopipe.context`. The `iopipe.context` is `None` if outside of an invocation.\n\n```python\nfrom iopipe import IOpipe\n\niopipe = IOpipe()\n\n# Be sure to check, can be None\nif iopipe.context:\n    # do something with context\n```\n\n## Contributing\n\nContributions are welcome. We use the [black](https://github.com/ambv/black) code formatter.\n\n```bash\npip install black\n```\n\nWe recommend using it with [pre-commit](https://pre-commit.com/#install):\n\n```bash\npip install pre-commit\npre-commit install\n```\n\nUsing these together will auto format your git commits.\n\n## Running Tests\n\nIf you have `tox` installed, you can run the Python 2.7 and 3.6 tests with:\n\n```bash\ntox\n```\n\nIf you don't have `tox` installed you can also run:\n\n```bash\npython setup.py test\n```\n\nWe also have make tasks to run tests in `lambci/lambda:build-python` Docker containers:\n\n```bash\nmake test\n```\n\n## License\n\nApache 2.0\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiopipe%2Fiopipe-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiopipe%2Fiopipe-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiopipe%2Fiopipe-python/lists"}