-
Notifications
You must be signed in to change notification settings - Fork 230
GIT-521: add support for sanic framework #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
harshanarayana
wants to merge
21
commits into
elastic:master
from
harshanarayana:feature/GIT-521-sanic-apm-contrib
Closed
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 f8df92a
GIT-521: add base test infra configuration
harshanarayana 4a3a96e
GIT-521: refactor and address review comments from @ahopkins
harshanarayana 3122e03
GIT-521: fix transaction name generation workflow
harshanarayana 4bc656d
GIT-521: enable documentation for sanic contrib
harshanarayana 50374e0
GIT-521: add callback info to documentation
harshanarayana 5e8ed0e
GIT-521: cleanup invalid html file
harshanarayana 30c24c6
GIT-521: add monkey patch for exception handler
harshanarayana 159a483
GIT-521: mark exception as handled
harshanarayana 908b8e2
GIT-521: cleanup exception handler setup
harshanarayana c9d6f3b
GIT-521: add instance check before setting up apm exception handler
harshanarayana 9cb8645
GIT-521: move types into reusable file
harshanarayana 3350fb0
GIT-521: cleanup unsed types
harshanarayana b5c71f2
GIT-521: fix header sanitization and exception tracker
harshanarayana 819e9a3
GIT-521: add additional tests and transaction name generation
harshanarayana d23661a
GIT-521: fix tests client compatibility mode
harshanarayana beb9f09
GIT-521: fix tests for new sanic router
harshanarayana 3e79cf8
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
beniwohli 4f535b4
exclude sanic tests in Python 3.6
beniwohli bb58841
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
basepi 1ae5422
Merge branch 'master' into feature/GIT-521-sanic-apm-contrib
basepi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,5 @@ FRAMEWORK: | |
- httpx-newest | ||
- httplib2-newest | ||
- prometheus_client-newest | ||
- sanic-newest | ||
- aiomysql-newest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,5 @@ FRAMEWORK: | |
- httpx-0.14 | ||
- httpx-newest | ||
- httplib2-newest | ||
- sanic-20.12 | ||
- sanic-newest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.