-
Notifications
You must be signed in to change notification settings - Fork 560
chore(http_server): emit pre-request web event #19817
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
litianningdatadog
wants to merge
2
commits into
tianning.li/2-flask-web-request-starting-event
from
tianning.li/2-1-http-server-request-starting-event
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,20 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.12 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/1362718.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.15.4 | ||
| hypothesis==6.45.0 | ||
| iniconfig==2.3.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==9.1.1 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.1.0 | ||
| sortedcontainers==2.4.0 |
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,20 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.11 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/13ed954.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.15.4 | ||
| hypothesis==6.45.0 | ||
| iniconfig==2.3.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==9.1.1 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.1.0 | ||
| sortedcontainers==2.4.0 |
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,20 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.14 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/1638d0f.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.15.4 | ||
| hypothesis==6.45.0 | ||
| iniconfig==2.3.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==9.1.1 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.1.0 | ||
| sortedcontainers==2.4.0 |
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,25 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.9 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/180047f.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.10.7 | ||
| exceptiongroup==1.3.1 | ||
| hypothesis==6.45.0 | ||
| importlib-metadata==8.7.1 | ||
| iniconfig==2.1.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==8.4.2 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.0.1 | ||
| sortedcontainers==2.4.0 | ||
| tomli==2.4.1 | ||
| typing-extensions==4.16.0 | ||
| zipp==3.23.1 |
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,23 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.10 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/3cb8c3c.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.15.4 | ||
| exceptiongroup==1.3.1 | ||
| hypothesis==6.45.0 | ||
| iniconfig==2.3.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==9.1.1 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.1.0 | ||
| sortedcontainers==2.4.0 | ||
| tomli==2.4.1 | ||
| typing-extensions==4.16.0 |
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,20 @@ | ||
| # | ||
| # This file is autogenerated by pip-compile with Python 3.13 | ||
| # by the following command: | ||
| # | ||
| # pip-compile --allow-unsafe --no-annotate .riot/requirements/43423a3.in | ||
| # | ||
| attrs==26.1.0 | ||
| coverage[toml]==7.15.4 | ||
| hypothesis==6.45.0 | ||
| iniconfig==2.3.0 | ||
| mock==5.2.0 | ||
| opentracing==2.4.0 | ||
| packaging==26.3 | ||
| pluggy==1.6.0 | ||
| pygments==2.21.0 | ||
| pytest==9.1.1 | ||
| pytest-cov==7.1.0 | ||
| pytest-mock==3.15.1 | ||
| pytest-randomly==4.1.0 | ||
| sortedcontainers==2.4.0 |
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,18 @@ | ||
| """ | ||
| Patch the standard library ``http.server`` module (``BaseHTTPRequestHandler``). | ||
|
|
||
| This integration does **not** create spans. Its only purpose is detecting the AWS Lambda | ||
| MicroVM ``/run`` lifecycle hook for applications that implement that hook with a raw | ||
| ``http.server`` handler instead of a supported web framework. | ||
|
|
||
|
|
||
| Enabling | ||
| ~~~~~~~~ | ||
|
|
||
| The http_server integration is enabled by default only in AWS Lambda MicroVM | ||
| environments. Use :ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>` | ||
| to enable it there, or set ``DD_TRACE_HTTP_SERVER_ENABLED=true`` to force-enable it | ||
| elsewhere. Disable it with ``DD_TRACE_HTTP_SERVER_ENABLED=false`` if needed:: | ||
|
|
||
| DD_TRACE_HTTP_SERVER_ENABLED=false ddtrace-run .... | ||
| """ |
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,48 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import http.server as http_server | ||
| from typing import Any | ||
| from typing import Callable | ||
|
|
||
| from wrapt import wrap_function_wrapper as _w | ||
|
|
||
| from ddtrace.contrib._events.web_framework import WebFrameworkEvents | ||
| from ddtrace.contrib.internal.trace_utils import unwrap as _u | ||
| from ddtrace.internal import core | ||
| from ddtrace.internal.serverless import in_aws_lambda_microvm | ||
|
|
||
|
|
||
| def get_version() -> str: | ||
| return "" | ||
|
|
||
|
|
||
| def _supported_versions() -> dict[str, str]: | ||
| return {"http.server": "*"} | ||
|
|
||
|
|
||
| def _wrap_parse_request( | ||
| wrapped: Callable[..., bool], | ||
| instance: http_server.BaseHTTPRequestHandler, | ||
| args: tuple[Any, ...], | ||
| kwargs: dict[str, Any], | ||
| ) -> bool: | ||
| parsed = wrapped(*args, **kwargs) | ||
| if parsed and in_aws_lambda_microvm(): | ||
| core.dispatch(WebFrameworkEvents.WEB_REQUEST_STARTING.value, (instance.command, instance.path)) | ||
| return parsed | ||
|
|
||
|
|
||
| def patch() -> None: | ||
| if getattr(http_server, "__datadog_patch", False): | ||
| return | ||
| http_server.__datadog_patch = True # type: ignore[attr-defined] # patch marker, not a real module attr | ||
|
|
||
| _w(http_server.BaseHTTPRequestHandler, "parse_request", _wrap_parse_request) | ||
|
|
||
|
|
||
| def unpatch() -> None: | ||
| if not getattr(http_server, "__datadog_patch", False): | ||
| return | ||
| http_server.__datadog_patch = False # type: ignore[attr-defined] # patch marker, not a real module attr | ||
|
|
||
| _u(http_server.BaseHTTPRequestHandler, "parse_request") |
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 |
|---|---|---|
|
|
@@ -167,6 +167,7 @@ | |
| "pylibmc", | ||
| "httpx", | ||
| "httplib", | ||
| "http_server", | ||
| "rq", | ||
| "jinja2", | ||
| "aredis", | ||
|
|
||
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
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
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
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
Empty file.
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,26 @@ | ||
| from ddtrace.contrib.internal.http_server.patch import get_version | ||
| from ddtrace.contrib.internal.http_server.patch import patch | ||
| from ddtrace.contrib.internal.http_server.patch import unpatch | ||
| from tests.contrib.patch import PatchTestCase | ||
|
|
||
|
|
||
| class TestHttpServerPatch(PatchTestCase.Base): | ||
| __integration_name__ = "http_server" | ||
| __module_name__ = "http.server" | ||
| __patch_func__ = patch | ||
| __unpatch_func__ = unpatch | ||
| __get_version__ = get_version | ||
|
|
||
| def assert_module_patched(self, http_server): | ||
| self.assert_wrapped(http_server.BaseHTTPRequestHandler.parse_request) | ||
|
|
||
| def assert_not_module_patched(self, http_server): | ||
| self.assert_not_wrapped(http_server.BaseHTTPRequestHandler.parse_request) | ||
|
|
||
| def assert_not_module_double_patched(self, http_server): | ||
| self.assert_not_double_wrapped(http_server.BaseHTTPRequestHandler.parse_request) | ||
|
|
||
| def test_and_emit_get_version(self): | ||
| version = get_version() | ||
| assert isinstance(version, str) | ||
| assert version == "" |
Oops, something went wrong.
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.