-
Notifications
You must be signed in to change notification settings - Fork 444
docs: add writeup of how autoinstrumentation works [LANGPLAT-352] #13640
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 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e0981be
init contributing-design document
emmettbutler 3298900
step one
emmettbutler 1d39868
other steps
emmettbutler 6e23554
link to new doc
emmettbutler 25444b9
Update docs/contributing-design.rst
emmettbutler e5b6a10
Update docs/contributing-design.rst
emmettbutler cc3dee7
new name
emmettbutler 8f01350
Update docs/contributing-design.rst
emmettbutler aa5af04
formatting
emmettbutler d45b893
spellcheck
emmettbutler b7df5a7
spellcheck
emmettbutler 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
The Design of dd-trace-py | ||
========================= | ||
|
||
Parts of the Library | ||
-------------------- | ||
|
||
When designing a change, one of the first decisions to make is where it should be made. This is an overview | ||
of the main functional areas of the library. | ||
|
||
A **product** is a unit of code within the library that implements functionality specific to a small set of | ||
customer-facing Datadog products. Examples include the `appsec module <https://github.com/DataDog/dd-trace-py/tree/1.x/ddtrace/appsec>`_ | ||
implementing functionality for `App and API Protection<https://www.datadoghq.com/product/application-security-management/>`_ | ||
and the `profiling <https://github.com/DataDog/dd-trace-py/tree/1.x/ddtrace/profiling>`_ module implementing | ||
functionality for `Continuous Profiling <https://docs.datadoghq.com/profiler/>`_. Ideally it only contains code | ||
that is specific to the Datadog product being supported, and no code related to Integrations. | ||
|
||
An **integration** is one of the modules in the `contrib <https://github.com/DataDog/dd-trace-py/tree/f26a526a6f79870e6e6a21d281f4796a434616bb/ddtrace/contrib>`_ | ||
directory, hooking our code into the internal logic of a given Python library. Ideally it only contains code | ||
that is specific to the library being integrated with, and no code related to Products. | ||
|
||
The **core** of the library is the abstraction layer that allows Products and Integrations to keep their concerns | ||
separate. It is implemented in the Python files in the `top level of ddtracepy <https://github.com/DataDog/dd-trace-py/tree/main/ddtrace>`_ | ||
and in the `internal` module. As an implementation detail, the core logic also happens to directly support | ||
`Application Performance Monitoring <https://docs.datadoghq.com/tracing/>`_. | ||
|
||
Be mindful and intentional about which of these categories your change fits into, and avoid mixing concerns between | ||
categories. If doing so requires more foundational refactoring or additional layers of abstraction, consider | ||
opening an issue describing the limitations of the current design. | ||
|
||
|
||
How Autoinstrumentation Works | ||
----------------------------- | ||
|
||
Autoinstrumentation is the feature of dd-trace-py that hooks into application code to facilitate application-level | ||
product functionality, for example the detailed traces visible in Datadog Application Monitoring. This is an | ||
overview of the technical implementation of this hooking functionality. | ||
|
||
This is the series of steps involved in autoinstrumentation: | ||
|
||
1. import bootstrap.sitecustomize, preload, clean up loaded modules, execute preexisting sitecustomizes | ||
2. start products | ||
3. set up an import hook for each integration | ||
4. when an integrated-with module is imported, the import hook calls the contrib's patch(). patch() replaces important functions in the module with transparent wrappers. | ||
5. These wrappers use the core API to create a tree of ExecutionContext objects. The context tree is traversed | ||
to generate the data to send to product intake. | ||
|
||
Step 1: Bootstrap | ||
----------------- | ||
|
||
The autoinstrumentation entrypoint is `import ddtrace.bootstrap.sitecustomize`. This can be done in user code, either directly or via | ||
`import ddtrace.auto`, or behind the scenes by `ddtrace-run`. | ||
|
||
ddtrace's sitecustomize script's basic goal is to start the Products that the library implements. | ||
These are subpackages like `_trace`, `profiling`, `debugging`, `appsec`, et cetera. Before starting these Products, | ||
some setup has to happen, especially the execution of preexisting PEP648 `sitecustomize` scripts to maintain | ||
user-facing guarantees about the runtime environment in which the application will execute. ddtrace's sitecustomize | ||
also attempts to "leave no trace" on the runtime environment, especially by unloading all of the modules it has | ||
used in the course of its setup phases. This helps reduce the possibility that ddtrace will use the same copy of | ||
an integrated-with module that the application uses, which can lead to undefined behavior. | ||
|
||
Step 2: Start Products | ||
---------------------- | ||
|
||
The Products implemented by the library conform to a Product Manager Protocol, which controls common functionality | ||
like setup, start, and stop. Each enabled Product module is loaded into the Manager instance on import, which happens in | ||
sitecustomize. In turn, sitecustomize runs the Protocol with `manager.run_protocol()` and later runs additional Product | ||
setup steps via `preload.post_preload`. Together, these calls comprise the setup of Products that will operate during the | ||
application's lifetime. | ||
|
||
Step 3: Set Up Import Hooks | ||
--------------------------- | ||
|
||
The core functionality set up during step 2 is autoinstrumentation, which is a prerequisite for many of the other Products. | ||
|
||
Autoinstrumentation setup involves registering hooks that will execute when a particular module is imported. The list of | ||
modules whose imports trigger this registration is defined in `_monkey.py`. During this step, each of these modules has an | ||
import hook registered for it. In the rest of this document, these modules are called "Instrumented Modules". | ||
|
||
Note that as of this writing, autoinstrumentation is implemented as a side effect of the `_trace` Product's setup phase. | ||
In a more abstract sense, autoinstrumentation can function as its own Product, and in the future may be refactored as such. | ||
|
||
Step 4: Import Occurs | ||
--------------------- | ||
|
||
The next step of autoinstrumentation happens when the application imports an Instrumented Module. The import triggers the | ||
import hook that was registered in step 3. The function that the hook executes has the primary goal of calling the `patch()` | ||
function of the integration module located at `ddtrace.contrib.<integration-name>.patch`. | ||
|
||
The goal of an integration's `patch()` function is to invisibly wrap the Instrumented Module with logic that generates the | ||
data about the module that's necessary for any relevant Products. The most commonly used wrapping approach is based on the | ||
`wrapt` library, which is included as a vendored dependency in `ddtrace`. Inside of the wrappers, it's common for | ||
integrations to build trees of `core.ExecutionContext` objects that store information about the running application's | ||
call stack. | ||
|
||
Whatever approach is taken, this step only sets up logic that will run later. | ||
|
||
Step 5: Application Logic Runs | ||
------------------------------ | ||
|
||
When the application uses the Instrumented Module after importing it, the wrappers created in step 4 are executed. This causes | ||
data about the running application to be collected in memory, often as a tree of `ExecutionContext` objects. Any Product | ||
that was started in step 2 may access these data and use them to build a payload to send to the relevant intake endpoints. | ||
The classic example is the `_trace` Product, which periodically traverses the context tree for the purpose of creating a `Trace` | ||
object that is subsequently serialized and sent to Datadog to power a flamegraph in the Application Observability product. |
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.
Uh oh!
There was an error while loading. Please reload this page.