-
Notifications
You must be signed in to change notification settings - Fork 614
docs: New integration guide #5476
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0dedf54
docs: New integration guide
alexander-alderman-webb 237d87d
.
alexander-alderman-webb 578371b
.
alexander-alderman-webb aa72f97
remove snarky comments
alexander-alderman-webb 55cf5ec
typo
alexander-alderman-webb bc41a1d
.
alexander-alderman-webb ccfeaad
reference breaking changes doc
alexander-alderman-webb 70baeb0
specify telemetry types for attributes
alexander-alderman-webb 31cc621
review feedback
alexander-alderman-webb 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,28 +108,108 @@ tox -p auto -o -e <tox_env> -- <pytest_args> | |
|
|
||
| ## Adding a New Integration | ||
|
|
||
| 1. Write the integration. | ||
| - Instrument all application instances by default. Prefer global signals/patches. | ||
| - Don't make the user pass anything to your integration for anything to work. Aim for zero configuration. | ||
| - Everybody monkeypatches. That means you don't need to feel bad about it. | ||
| - Make sure your changes don't break end user contracts. The SDK should never alter the expected behavior of the underlying library or framework from the user's perspective and it shouldn't have any side effects. | ||
| - Be defensive. Don't assume the code you're patching will stay the same forever, especially if it's an internal function. Allow for future variability whenever it makes sense. | ||
| - Avoid registering a new client or the like. The user drives the client, and the client owns integrations. | ||
| - Allow the user to turn off the integration by changing the client. Check `sentry_sdk.get_client().get_integration(MyIntegration)` from within your signal handlers to see if your integration is still active before you do anything impactful (such as sending an event). | ||
| ### SDK Contract | ||
|
|
||
| 2. Write tests. | ||
| - Consider the minimum versions supported, and document in `_MIN_VERSIONS` in `integrations/__init__.py`. | ||
| - Create a new folder in `tests/integrations/`, with an `__init__` file that skips the entire suite if the package is not installed. | ||
| - Add the test suite to the script generating our test matrix. See [`scripts/populate_tox/README.md`](https://github.com/getsentry/sentry-python/blob/master/scripts/populate_tox/README.md#add-a-new-test-suite). | ||
| The SDK runs as part of users' applications. Users do not expect: | ||
|
|
||
| 3. Update package metadata. | ||
| - We use `extras_require` in `setup.py` to communicate minimum version requirements for integrations. People can use this in combination with tools like Poetry or Pipenv to detect conflicts between our supported versions and their used versions programmatically. | ||
| - their application to crash ([#4690](https://github.com/getsentry/sentry-python/issues/4690), [#4718](https://github.com/getsentry/sentry-python/issues/4718), [#4776](https://github.com/getsentry/sentry-python/issues/4776), [#4925](https://github.com/getsentry/sentry-python/issues/4925), [#4951](https://github.com/getsentry/sentry-python/issues/4951), [#4975](https://github.com/getsentry/sentry-python/issues/4975), [#5067](https://github.com/getsentry/sentry-python/issues/5067), [#5071](https://github.com/getsentry/sentry-python/issues/5071), [#5129](https://github.com/getsentry/sentry-python/issues/5129), [#5134](https://github.com/getsentry/sentry-python/issues/5134), [#5277](https://github.com/getsentry/sentry-python/issues/5277), [#5298](https://github.com/getsentry/sentry-python/issues/5298), [#5350](https://github.com/getsentry/sentry-python/issues/5350)). | ||
| - the SDK to mutate session cookies ([#4882](https://github.com/getsentry/sentry-python/issues/4882)). | ||
| - the SDK to swallow their exceptions ([#4853](https://github.com/getsentry/sentry-python/issues/4853)). | ||
| - their HTTP response streams to be consumed ([#4764](https://github.com/getsentry/sentry-python/issues/4764), [#4827](https://github.com/getsentry/sentry-python/issues/4827)). | ||
| - leaked file descriptors to eat their memory ([#5422](https://github.com/getsentry/sentry-python/issues/5422)). | ||
| - SDK-initiated database requests ([#5274](https://github.com/getsentry/sentry-python/issues/5274), [#5414](https://github.com/getsentry/sentry-python/issues/5414)). | ||
| - uWSGI performance degradation due to SDK patches ([#5107](https://github.com/getsentry/sentry-python/issues/5107)). | ||
| - change the signature of functions or coroutines ([#5072](https://github.com/getsentry/sentry-python/issues/5072)). | ||
|
|
||
| Do not set upper bounds on version requirements as people are often faster in adopting new versions of a web framework than we are in adding them to the test matrix or our package metadata. | ||
| So this means you should write the ugly code in the library to work around this? | ||
| Well, there's another consequence of running on thousands of applications. Maintenance burden is higher than for application code, because all code paths of the SDK are hit across the enormous variety of applications the SDK finds itself in. The diversity includes different CPython versions, permutations of package versions, and operating systems. | ||
| And once something you write is out there, you cannot remove it from the SDK without good reason. | ||
| This means that the SDK is not a playground for the inappropriate use of AI-assisted coding. | ||
|
|
||
| 4. Write the [docs](https://github.com/getsentry/sentry-docs). Follow the structure of [existing integration docs](https://docs.sentry.io/platforms/python/integrations/). And, please **make sure to add your integration to the table in `python/integrations/index.md`** (people often forget this step 🙂). | ||
| Oh, and did I mention that bugs in old SDK versions can still come to haunt you? Even if you have patched them in the meantime. | ||
|
|
||
| What's the concrete advice when writing a new integration? | ||
|
|
||
| ### Requirements | ||
|
|
||
| 1. Are you supporting a product feature? Then ensure that product expectations are clearly documented in https://develop.sentry.dev/sdk/telemetry/traces/modules/. | ||
|
|
||
| 2. Confirm that all attributes are defined in https://github.com/getsentry/sentry-conventions. | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
|
|
||
| 3. Ensure that the **semantics** of the attribute are clear. If the attribute is not uniquely defined, do not add it. | ||
| - For instance, do not attach a request model to an agent invocation span. On the other hand, a default request model can be well-defined. | ||
|
|
||
| ### Code | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section it very dense. It will improve scan-ability if you either mark the number list points bold or make it a heading (H4). |
||
|
|
||
| 0. Document why you're patching or hooking into something. | ||
| - Even if it's just to say that you're creating and managing a certain type of span in the patch, that's valuable. | ||
| - It should be clear what span is created and exited where and which patches apply which attributes on a given span. | ||
|
|
||
| 1. Do you even need to add this attribute on this span? | ||
| - Be intentional with supporting product features. Only adding what's necessary, or **be very sure that your addition provides value**. Decisions about what data lives on what types of spans are hard to undo, and limits future design space. | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
|
|
||
| 2. Avoid setting arbitrary objects. | ||
| - In line with the point above, prefer using an include-list of valuable entries when setting a dictionary attribute. Otherwise, tests will break again and again ([#5454](https://github.com/getsentry/sentry-python/pull/5454), [#5471](https://github.com/getsentry/sentry-python/pull/5471)). | ||
|
|
||
| 3. Instrument all application instances by default. Prefer global signals/patches. | ||
| - Patching instances is just harder. Your patches may unexpectedly not apply to some instances, or unexpectedly be applied multiple times ([!5195](https://github.com/getsentry/sentry-python/pull/5195)). | ||
|
|
||
| 4. Don't make the user pass anything to your integration for anything to work. Aim for zero configuration. | ||
| - Users tend to only consult the documentation when something goes wrong. So the default values for integration options must lead to the best out-of-the-box experience for the majority of users. | ||
|
|
||
| 5. Re-use code, but only when it makes sense. | ||
| - Think about future evolution of the library and your integration. | ||
| - If you're patching two internal methods that are similar but will diverge with time, don't force a common patch. | ||
| - If the shared SDK logic will diverge for two patches, just don't force them through a common path in the first place. | ||
| - If your shared code path has a bunch of conditionals today or will have a ton of conditionals in the future, that's the sign to not stick to DRY. | ||
|
|
||
| 6. Be explicit | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
| - You're developing against a library, and that library uses specific types. | ||
| - If you use `hasattr()` or `getattr()` to gate logic, you must verify the code path for all types that have this attribute. And Python has duck-typing, so good luck. | ||
| - If you use `type().__name__` to gate logic, you must verify the behavior for all types with a given name. And Python has duck-typing, so good luck. | ||
| - So just use `isinstance()` to save us a headache. | ||
|
|
||
| 7. Heuristics will bite you later. | ||
| - If something you write is best-effort, make sure there are no alternatives ([#4980](https://github.com/getsentry/sentry-python/issues/4980)). | ||
|
|
||
| 8. Obsess about the unhappy path. | ||
| - Users are interested in seeing what went wrong when something doesn't work. If the code in the `catch` block is garbage, that's a problem. | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
| - Let exceptions bubble-up as far as possible when reporting unhandled exceptions. | ||
| - Preserve the user's original exception. Python chains exceptions when code in a `catch` block throws, so if a `catch` block in the SDK throws, the SDK exception takes the foreground ([#5188](https://github.com/getsentry/sentry-python/issues/5188)). | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
| - Please don't report exceptions that are caught further up in the library's call chain as unhandled ([#5232](https://github.com/getsentry/sentry-python/issues/5232), [#5473](https://github.com/getsentry/sentry-python/issues/5473)). | ||
|
|
||
| 9. Make sure your changes don't break end user contracts. The SDK should never alter the expected behavior of the underlying library or framework from the user's perspective and it shouldn't have any side effects. | ||
|
alexander-alderman-webb marked this conversation as resolved.
alexander-alderman-webb marked this conversation as resolved.
|
||
|
|
||
| 10. Be defensive, but don't add dead code. | ||
| - Don't assume the code you're patching will stay the same forever, especially if it's an internal function. Allow for future variability whenever it makes sense. | ||
| - Dead code adds cognitive overhead when reasoning about code, so don't account for impossible scenarios. | ||
|
|
||
| 11. Write tests, but don't write mocks. | ||
| - You'd be surprised how many tests assert the wrong thing ([#5404](https://github.com/getsentry/sentry-python/pull/5404), [#5403](https://github.com/getsentry/sentry-python/pull/5403)). | ||
| - Others packaging the SDK seem to run our test suite, so don't write racy or other environment-dependent tests ([#4878](https://github.com/getsentry/sentry-python/issues/4878)). | ||
| - Actually look at the console output ([#4723](https://github.com/getsentry/sentry-python/issues/4723)). | ||
| - Don't test unreachable states, or your tests will be removed ([!5412](https://github.com/getsentry/sentry-python/pull/5412)). | ||
| - Don't call private SDK stuff directly, just use the patched library in a way that triggers the patch ([#5437](https://github.com/getsentry/sentry-python/pull/5437)). | ||
| - Don't write tests that are always skipped, that's just silly ([!5338](https://github.com/getsentry/sentry-python/pull/5338)). | ||
| - Mocks are _very expensive_ to maintain, particularly when testing patches for fast-moving libraries ([#5126](https://github.com/getsentry/sentry-python/pull/5126)). | ||
| - Consider the minimum versions supported, and document in `_MIN_VERSIONS` in `integrations/__init__.py`. | ||
| - Create a new folder in `tests/integrations/`, with an `__init__` file that skips the entire suite if the package is not installed. | ||
| - Add the test suite to the script generating our test matrix. See [`scripts/populate_tox/README.md`](https://github.com/getsentry/sentry-python/blob/master/scripts/populate_tox/README.md#add-a-new-test-suite). | ||
|
|
||
| 12. Be careful patching decorators | ||
| - Does the library's decorator apply to sync or async functions ([#5415](https://github.com/getsentry/sentry-python/pull/5415))? | ||
| - Some decorators can be applied to classes and functions, and both with and without arguments. Make sure you handle all applicable cases ([#5225](https://github.com/getsentry/sentry-python/issues/5225)). | ||
|
|
||
| 13. Avoid registering a new client or the like. The user drives the client, and the client owns integrations. | ||
|
|
||
| 14. Allow the user to turn off the integration by changing the client. Check `sentry_sdk.get_client().get_integration(MyIntegration)` from within your signal handlers to see if your integration is still active before you do anything impactful (such as sending an event). | ||
|
alexander-alderman-webb marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Document | ||
|
|
||
| 1. Write the [docs](https://github.com/getsentry/sentry-docs). Follow the structure of [existing integration docs](https://docs.sentry.io/platforms/python/integrations/). And, please **make sure to add your integration to the table in `python/integrations/index.md`** (people often forget this step 🙂). | ||
|
|
||
| 2. Merge docs after new version has been released. The docs are built and deployed after each merge, so your changes should go live in a few minutes. | ||
|
|
||
| 5. Merge docs after new version has been released. The docs are built and deployed after each merge, so your changes should go live in a few minutes. | ||
|
|
||
| ## Releasing a New Version | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.