Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f09e12f
GIT-521: add support for sanic framework
harshanarayana Mar 1, 2021
f8df92a
GIT-521: add base test infra configuration
harshanarayana Mar 1, 2021
4a3a96e
GIT-521: refactor and address review comments from @ahopkins
harshanarayana Mar 1, 2021
3122e03
GIT-521: fix transaction name generation workflow
harshanarayana Mar 1, 2021
4bc656d
GIT-521: enable documentation for sanic contrib
harshanarayana Mar 6, 2021
50374e0
GIT-521: add callback info to documentation
harshanarayana Mar 6, 2021
5e8ed0e
GIT-521: cleanup invalid html file
harshanarayana Mar 6, 2021
30c24c6
GIT-521: add monkey patch for exception handler
harshanarayana Mar 6, 2021
159a483
GIT-521: mark exception as handled
harshanarayana Mar 6, 2021
908b8e2
GIT-521: cleanup exception handler setup
harshanarayana Mar 6, 2021
c9d6f3b
GIT-521: add instance check before setting up apm exception handler
harshanarayana Mar 7, 2021
9cb8645
GIT-521: move types into reusable file
harshanarayana Mar 7, 2021
3350fb0
GIT-521: cleanup unsed types
harshanarayana Mar 7, 2021
b5c71f2
GIT-521: fix header sanitization and exception tracker
harshanarayana Apr 2, 2021
819e9a3
GIT-521: add additional tests and transaction name generation
harshanarayana Apr 2, 2021
d23661a
GIT-521: fix tests client compatibility mode
harshanarayana Apr 2, 2021
beb9f09
GIT-521: fix tests for new sanic router
harshanarayana Apr 18, 2021
3e79cf8
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
beniwohli Jun 8, 2021
4f535b4
exclude sanic tests in Python 3.6
beniwohli Jun 9, 2021
bb58841
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
basepi Oct 27, 2021
1ae5422
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
basepi Nov 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .ci/.jenkins_exclude.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ exclude:
- PYTHON_VERSION: python-3.10-rc
FRAMEWORK: zerorpc-0.4
# gevent
- PYTHON_VERSION: python-3.10-rc # causes issues with PosixPath.__enter__, unsure why
- PYTHON_VERSION: python-3.10-rc # causes issues with PosixPath.__enter__, unsure why
FRAMEWORK: gevent-newest
# aiohttp client, only supported in Python 3.7+
- PYTHON_VERSION: pypy-3
Expand Down Expand Up @@ -230,9 +230,19 @@ exclude:
FRAMEWORK: asyncpg-newest
- PYTHON_VERSION: python-3.6
FRAMEWORK: asyncpg-newest
- PYTHON_VERSION: python-3.10-rc # https://github.com/MagicStack/asyncpg/issues/699
- PYTHON_VERSION: python-3.10-rc # https://github.com/MagicStack/asyncpg/issues/699
FRAMEWORK: asyncpg-newest
# sanic
- PYTHON_VERSION: pypy-3
FRAMEWORK: sanic-newest
- PYTHON_VERSION: pypy-3
FRAMEWORK: sanic-20.12
- PYTHON_VERSION: python-3.6
FRAMEWORK: sanic-20.12
- PYTHON_VERSION: python-3.6
FRAMEWORK: sanic-newest
- PYTHON_VERSION: pypy-3
# aioredis
FRAMEWORK: aioredis-newest
- PYTHON_VERSION: python-3.6
FRAMEWORK: aioredis-newest
Expand Down
1 change: 1 addition & 0 deletions .ci/.jenkins_framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ FRAMEWORK:
- httpx-newest
- httplib2-newest
- prometheus_client-newest
- sanic-newest
- aiomysql-newest
2 changes: 2 additions & 0 deletions .ci/.jenkins_framework_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ FRAMEWORK:
- httpx-0.14
- httpx-newest
- httplib2-newest
- sanic-20.12
- sanic-newest
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"django": "tests.contrib.django.fixtures",
"flask": "tests.contrib.flask.fixtures",
"aiohttp": "aiohttp.pytest_plugin",
"sanic": "tests.contrib.sanic.fixtures",
}.items():
try:
importlib.import_module(module)
Expand Down
179 changes: 179 additions & 0 deletions docs/sanic.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
[[sanic-support]]
=== Sanic Support

Incorporating Elastic APM into your Sanic project only requires a few easy
steps.

[float]
[[sanic-installation]]
==== Installation

Install the Elastic APM agent using pip:

[source,bash]
----
$ pip install elastic-apm
----

or add `elastic-apm` to your project's `requirements.txt` file.


[float]
[[sanic-setup]]
==== Setup

To set up the agent, you need to initialize it with appropriate settings.

The settings are configured either via environment variables, or as
initialization arguments.

You can find a list of all available settings in the
<<configuration, Configuration>> page.

To initialize the agent for your application using environment variables:

[source,python]
----
from sanic import Sanic
from elasticapm.contrib.sanic import ElasticAPM

app = Sanic(name="elastic-apm-sample")
apm = ElasticAPM(app=app)
----

To configure the agent using initialization arguments and Sanic's Configuration infrastructure:

[source,python]
----
# Create a file named external_config.py in your application
# If you want this module based configuration to be used for APM, prefix them with ELASTIC_APM_
ELASTIC_APM_SERVER_URL = "https://serverurl.apm.com:443"
ELASTIC_APM_SECRET_TOKEN = "sometoken"
----

[source,python]
----
from sanic import Sanic
from elasticapm.contrib.sanic import ElasticAPM

app = Sanic(name="elastic-apm-sample")
app.config.update_config("path/to/external_config.py")
apm = ElasticAPM(app=app)
----

[float]
[[sanic-usage]]
==== Usage

Once you have configured the agent, it will automatically track transactions
and capture uncaught exceptions within sanic.

Capture an arbitrary exception by calling
<<client-api-capture-exception,`capture_exception`>>:

[source,python]
----
from sanic import Sanic
from elasticapm.contrib.sanic import ElasticAPM

app = Sanic(name="elastic-apm-sample")
apm = ElasticAPM(app=app)

try:
1 / 0
except ZeroDivisionError:
apm.capture_exception()
----

Log a generic message with <<client-api-capture-message,`capture_message`>>:

[source,python]
----
from sanic import Sanic
from elasticapm.contrib.sanic import ElasticAPM

app = Sanic(name="elastic-apm-sample")
apm = ElasticAPM(app=app)

apm.capture_message('hello, world!')
----

[float]
[[sanic-performance-metrics]]
==== Performance metrics

If you've followed the instructions above, the agent has installed our
instrumentation middleware which will process all requests through your app.
This will measure response times, as well as detailed performance data for
all supported technologies.

NOTE: Due to the fact that `asyncio` drivers are usually separate from their
synchronous counterparts, specific instrumentation is needed for all drivers.
The support for asynchronous drivers is currently quite limited.

[float]
[[sanic-ignoring-specific-views]]
===== Ignoring specific routes

You can use the
<<config-transactions-ignore-patterns,`TRANSACTIONS_IGNORE_PATTERNS`>>
configuration option to ignore specific routes. The list given should be a
list of regular expressions which are matched against the transaction name:

[source,python]
----
from sanic import Sanic
from elasticapm.contrib.sanic import ElasticAPM

app = Sanic(name="elastic-apm-sample")
apm = ElasticAPM(app=app, config={
'TRANSACTIONS_IGNORE_PATTERNS': ['^GET /secret', '/extra_secret'],
})
----

This would ignore any requests using the `GET /secret` route
and any requests containing `/extra_secret`.

[float]
[[extended-sanic-usage]]
==== Extended Sanic APM Client Usage

Sanic's contributed APM client also provides a few extendable way to configure selective behaviors to enhance the
information collected as part of the transactions being tracked by the APM.

In order to enable this behavior, the APM Client middleware provides a few callback functions that you can leverage
in order to simplify the process of generating additional contexts into the traces being collected.
[cols="1,1,1,1"]
|===
| Callback Name | Callback Invocation Format | Expected Return Format | Is Async

| transaction_name_callback
| transaction_name_callback(request)
| string
| false

| user_context_callback
| user_context_callback(request)
| (username_string, user_email_string, userid_string)
| true

| custom_context_callback
| custom_context_callback(request) or custom_context_callback(response)
| dict(str=str)
| true

| label_info_callback
| label_info_callback()
| dict(str=str)
| true
|===

[float]
[[supported-stanic-and-python-versions]]
==== Supported Sanic and Python versions

A list of supported <<supported-sanic,Sanic>> and
<<supported-python,Python>> versions can be found on our
<<supported-technologies,Supported Technologies>> page.

NOTE: Elastic APM only supports `asyncio` when using Python 3.7+
3 changes: 2 additions & 1 deletion docs/set-up.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ To get you off the ground, we’ve prepared guides for setting up the Agent with
* <<aiohttp-server-support,aiohttp>>
* <<tornado-support,Tornado>>
* <<starlette-support,Starlette/FastAPI>>
* <<sanic-support,Sanic>>

For custom instrumentation, see <<instrumenting-custom-code, Instrumenting Custom Code>>.

Expand All @@ -19,4 +20,4 @@ include::./aiohttp-server.asciidoc[]

include::./tornado.asciidoc[]

include::./starlette.asciidoc[]
include::./starlette.asciidoc[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
include::./starlette.asciidoc[]
include::./starlette.asciidoc[]
include::./sanic.asciidoc[]

10 changes: 10 additions & 0 deletions docs/supported-technologies.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The Elastic APM Python Agent comes with support for the following frameworks:
* <<supported-aiohttp,Aiohttp Server>>
* <<supported-tornado,Tornado>>
* <<supported-starlette,Starlette/FastAPI>>
* <<supported-sanic,Sanic>>

For other frameworks and custom Python code, the agent exposes a set of <<api,APIs>> for integration.

Expand Down Expand Up @@ -71,6 +72,15 @@ We support these tornado versions:
* 6.0+


[float]
[[supported-sanic]]
=== Sanic

We support these sanic versions:

* 20.12.2+


[float]
[[supported-starlette]]
=== Starlette/FastAPI
Expand Down
Loading