Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .ci/.matrix_exclude.yml
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,12 @@ exclude:
FRAMEWORK: httpx-0.14
- VERSION: python-3.14
FRAMEWORK: httpx-0.21
# httpx2 requires Python 3.10+
- VERSION: python-3.6
FRAMEWORK: httpx2-newest
- VERSION: python-3.7
FRAMEWORK: httpx2-newest
- VERSION: python-3.8
FRAMEWORK: httpx2-newest
- VERSION: python-3.9
FRAMEWORK: httpx2-newest
1 change: 1 addition & 0 deletions .ci/.matrix_framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ FRAMEWORK:
- pymemcache-newest
- graphene-2
- httpx-newest
- httpx2-newest
- httplib2-newest
- prometheus_client-newest
- sanic-newest
Expand Down
1 change: 1 addition & 0 deletions .ci/.matrix_framework_fips.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ FRAMEWORK:
- starlette-newest
- graphene-2
- httpx-newest
- httpx2-newest
- httplib2-newest
- prometheus_client-newest
- sanic-newest
1 change: 1 addition & 0 deletions .ci/.matrix_framework_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ FRAMEWORK:
- httpx-0.14
- httpx-0.21
- httpx-newest
- httpx2-newest
- httplib2-newest
- sanic-20.12
- sanic-newest
Expand Down
15 changes: 15 additions & 0 deletions docs/reference/supported-technologies.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,21 @@ Collected trace data:
* requested URL


#### httpx2 [automatic-instrumentation-httpx2]

Library: `httpx2` (`>=2`, requires Python 3.10+)

Instrumented methods:

* `httpx2.Client.send`
* `httpx2.AsyncClient.send`

Collected trace data:

* HTTP method
* requested URL


### Services [automatic-instrumentation-services]


Expand Down
29 changes: 29 additions & 0 deletions elasticapm/instrumentation/packages/httpx2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# BSD 3-Clause License
#
# Copyright (c) 2021, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 changes: 29 additions & 0 deletions elasticapm/instrumentation/packages/httpx2/async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# BSD 3-Clause License
#
# Copyright (c) 2021, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102 changes: 102 additions & 0 deletions elasticapm/instrumentation/packages/httpx2/async/httpcore2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# BSD 3-Clause License
#
# Copyright (c) 2021, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from elasticapm.contrib.asyncio.traces import async_capture_span
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
from elasticapm.instrumentation.packages.httpx import utils
from elasticapm.traces import DroppedSpan, execution_context
from elasticapm.utils import default_ports
from elasticapm.utils.disttracing import TracingOptions


class HTTPCore2AsyncInstrumentation(AsyncAbstractInstrumentedModule):
"""
This instrumentation only exists to make sure we add distributed tracing
headers on our requests from `httpx2`. `httpx2` is the only place this library
is used, so no spans will actually be created (due to already being in
a leaf span). However, the rest of the logic was left in (much of this
mirrors the urllib3 instrumentation) in case that situation ever changes.
"""

name = "httpcore2"

instrument_list = [
("httpcore2._async.connection", "AsyncHTTPConnection.handle_async_request"),
]

async def call(self, module, method, wrapped, instance, args, kwargs):
url, method, headers = utils.get_request_data(args, kwargs)
scheme, host, port, target = url

if port is not None and port != default_ports.get(scheme):
host += ":" + str(port)

signature = "%s %s" % (method.upper(), host)

url = "%s://%s%s" % (scheme, host, target)

transaction = execution_context.get_transaction()

async with async_capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}},
leaf=True,
) as span:
# if httpcore2 has been called in a leaf span, this span might be a DroppedSpan.
leaf_span = span
while isinstance(leaf_span, DroppedSpan):
leaf_span = leaf_span.parent

if headers is not None:
# It's possible that there are only dropped spans, e.g. if we started dropping spans due to the
# transaction_max_spans limit. In this case, the transaction.id is used
parent_id = leaf_span.id if leaf_span else transaction.id
trace_parent = transaction.trace_parent.copy_from(
span_id=parent_id, trace_options=TracingOptions(recorded=True)
)
utils.set_disttracing_headers(headers, trace_parent, transaction)
response = await wrapped(*args, **kwargs)
status_code = utils.get_status(response)
if status_code:
if span.context:
span.context["http"]["status_code"] = status_code
span.set_success() if status_code < 400 else span.set_failure()
return response

def mutate_unsampled_call_args(self, module, method, wrapped, instance, args, kwargs, transaction):
# since we don't have a span, we set the span id to the transaction id
trace_parent = transaction.trace_parent.copy_from(
span_id=transaction.id, trace_options=TracingOptions(recorded=False)
)
headers = utils.get_request_data(args, kwargs)[2]
utils.set_disttracing_headers(headers, trace_parent, transaction)
return args, kwargs
61 changes: 61 additions & 0 deletions elasticapm/instrumentation/packages/httpx2/async/httpx2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# BSD 3-Clause License
#
# Copyright (c) 2019, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from elasticapm.contrib.asyncio.traces import async_capture_span
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
from elasticapm.utils import get_host_from_url, sanitize_url


class Httpx2AsyncClientInstrumentation(AsyncAbstractInstrumentedModule):
name = "httpx2"

instrument_list = [("httpx2", "AsyncClient.send")]

async def call(self, module, method, wrapped, instance, args, kwargs):
request = kwargs.get("request") or args[0]

request_method = request.method.upper()
url = str(request.url)
name = "{request_method} {host}".format(request_method=request_method, host=get_host_from_url(url))
url = sanitize_url(url)

async with async_capture_span(
name,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}},
leaf=True,
) as span:
response = await wrapped(*args, **kwargs)
if response is not None:
if span.context:
span.context["http"]["status_code"] = response.status_code
span.set_success() if response.status_code < 400 else span.set_failure()
return response
29 changes: 29 additions & 0 deletions elasticapm/instrumentation/packages/httpx2/sync/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# BSD 3-Clause License
#
# Copyright (c) 2021, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading