Skip to content

Commit

Permalink
implement auto-inference of destination resource (#1359)
Browse files Browse the repository at this point in the history
* implement auto-inference of destination resource

closes #1159

* fix a bunch of tests
  • Loading branch information
beniwohli authored Oct 18, 2021
1 parent 9624775 commit 5ae5cf6
Show file tree
Hide file tree
Showing 47 changed files with 107 additions and 103 deletions.
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/asyncio/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from elasticapm.conf import constants
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, execution_context
from elasticapm.utils import get_host_from_url, sanitize_url, url_to_destination
from elasticapm.utils import get_host_from_url, sanitize_url
from elasticapm.utils.disttracing import TracingOptions


Expand All @@ -45,7 +45,6 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
method = kwargs["method"] if "method" in kwargs else args[0]
url = kwargs["url"] if "url" in kwargs else args[1]
url = str(url)
destination = url_to_destination(url)

signature = " ".join([method.upper(), get_host_from_url(url)])
url = sanitize_url(url)
Expand All @@ -55,7 +54,7 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
leaf_span = span
Expand Down
2 changes: 1 addition & 1 deletion elasticapm/instrumentation/packages/asyncio/aiomysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
"destination": {
"address": instance.connection.host,
"port": instance.connection.port,
"service": {"name": "mysql", "resource": "mysql", "type": "db"},
"service": {"name": "", "resource": "mysql", "type": ""},
},
}
action = "query"
Expand Down
2 changes: 1 addition & 1 deletion elasticapm/instrumentation/packages/asyncio/aioredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):


def _get_destination_info(connection):
destination_info = {"service": {"name": "aioredis", "resource": "redis", "type": "db"}}
destination_info = {"service": {"name": "", "resource": "redis", "type": ""}}

if hasattr(connection, "_pool_or_conn"):
destination_info["port"] = connection._pool_or_conn.address[1]
Expand Down
2 changes: 1 addition & 1 deletion elasticapm/instrumentation/packages/asyncio/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": kwargs.get("host", "localhost"),
"port": int(kwargs.get("port", default_ports.get("postgresql"))),
"service": {"name": "postgresql", "resource": "postgresql", "type": "db"},
"service": {"name": "", "resource": "postgresql", "type": ""},
}
context["destination"] = destination_info
async with async_capture_span(
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/asyncio/httpcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from elasticapm.contrib.asyncio.traces import async_capture_span
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, execution_context
from elasticapm.utils import default_ports, url_to_destination
from elasticapm.utils import default_ports
from elasticapm.utils.disttracing import TracingOptions


Expand Down Expand Up @@ -82,15 +82,14 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
signature = "%s %s" % (method.upper(), host)

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

transaction = execution_context.get_transaction()

async with async_capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
# if httpcore has been called in a leaf span, this span might be a DroppedSpan.
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/asyncio/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

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, url_to_destination
from elasticapm.utils import get_host_from_url, sanitize_url


class HttpxAsyncClientInstrumentation(AsyncAbstractInstrumentedModule):
Expand All @@ -45,13 +45,12 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
url = str(request.url)
name = "{request_method} {host}".format(request_method=request_method, host=get_host_from_url(url))
url = sanitize_url(url)
destination = url_to_destination(url)

async with async_capture_span(
name,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
response = await wrapped(*args, **kwargs)
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
context["destination"] = {
"address": host,
"port": port,
"service": {"name": "cassandra", "resource": "cassandra", "type": "db"},
}

with capture_span(name, span_type="db", span_subtype="cassandra", span_action=span_action, extra=context):
Expand Down
2 changes: 1 addition & 1 deletion elasticapm/instrumentation/packages/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class CursorProxy(wrapt.ObjectProxy):

def __init__(self, wrapped, destination_info=None):
super(CursorProxy, self).__init__(wrapped)
self._self_destination_info = destination_info
self._self_destination_info = destination_info or {}

def callproc(self, procname, params=None):
return self._trace_sql(self.__wrapped__.callproc, procname, params, action=EXEC_ACTION)
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def _update_context_by_request_data(self, context, instance, args, kwargs):

context["destination"] = {
"address": instance.host,
"service": {"name": "elasticsearch", "resource": "elasticsearch", "type": "db"},
}


Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/httpcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from elasticapm.conf import constants
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, capture_span, execution_context
from elasticapm.utils import default_ports, url_to_destination
from elasticapm.utils import default_ports
from elasticapm.utils.disttracing import TracingOptions


Expand Down Expand Up @@ -72,15 +72,14 @@ def call(self, module, method, wrapped, instance, args, kwargs):
signature = "%s %s" % (method.upper(), host)

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

transaction = execution_context.get_transaction()

with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
# if httpcore has been called in a leaf span, this span might be a DroppedSpan.
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from elasticapm.conf import constants
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, capture_span, execution_context
from elasticapm.utils import get_host_from_url, sanitize_url, url_to_destination
from elasticapm.utils import get_host_from_url, sanitize_url
from elasticapm.utils.disttracing import TracingOptions


Expand Down Expand Up @@ -72,13 +72,12 @@ def call(self, module, method, wrapped, instance, args, kwargs):
signature = params.get("method", "GET").upper()
signature += " " + get_host_from_url(params["url"])
url = sanitize_url(params["url"])
destination = url_to_destination(url)
transaction = execution_context.get_transaction()
with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
# if httplib2 has been called in a leaf span, this span might be a DroppedSpan.
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import capture_span
from elasticapm.utils import get_host_from_url, sanitize_url, url_to_destination
from elasticapm.utils import get_host_from_url, sanitize_url


class HttpxClientInstrumentation(AbstractInstrumentedModule):
Expand All @@ -45,13 +45,12 @@ def call(self, module, method, wrapped, instance, args, kwargs):
url = str(request.url)
name = "{request_method} {host}".format(request_method=request_method, host=get_host_from_url(url))
url = sanitize_url(url)
destination = url_to_destination(url)

with capture_span(
name,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
response = wrapped(*args, **kwargs)
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": args[0] if len(args) else kwargs.get("host", "localhost"),
"port": args[4] if len(args) > 4 else int(kwargs.get("port", default_ports.get("mysql"))),
"service": {"name": "mysql", "resource": "mysql", "type": "db"},
}
return MySQLConnectionProxy(wrapped(*args, **kwargs), destination_info=destination_info)
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/mysql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": kwargs.get("host", "localhost"),
"port": int(kwargs.get("port", default_ports.get("mysql"))),
"service": {"name": "mysql", "resource": "mysql", "type": "db"},
}
return MySQLConnectionProxy(wrapped(*args, **kwargs), destination_info=destination_info)
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": kwargs.get("host", "localhost"),
"port": int(kwargs.get("port", default_ports.get("postgresql"))),
"service": {"name": "postgresql", "resource": "postgresql", "type": "db"},
}
with capture_span(
signature,
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/pylibmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination = {
"address": address,
"port": port,
"service": {"name": "memcached", "resource": "memcached", "type": "cache"},
}
with capture_span(
wrapped_name,
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination = {
"address": address,
"port": port,
"service": {"name": "memcached", "resource": "memcached", "type": "cache"},
}

if "PooledClient" in name:
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": host,
"port": port,
"service": {"name": "mongodb", "resource": "mongodb", "type": "db"},
}
with capture_span(
signature,
Expand All @@ -106,7 +105,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):
span_type="db",
span_subtype="mongodb",
span_action="query",
extra={"destination": {"service": {"name": "mongodb", "resource": "mongodb", "type": "db"}}},
extra={"destination": {}},
):
return wrapped(*args, **kwargs)

Expand All @@ -124,7 +123,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):
span_type="db",
span_subtype="mongodb",
span_action="query",
extra={"destination": {"service": {"name": "mongodb", "resource": "mongodb", "type": "db"}}},
extra={"destination": {}},
) as span:
response = wrapped(*args, **kwargs)
if span.context and instance.address:
Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/pymssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination_info = {
"address": host,
"port": port,
"service": {"name": "mssql", "resource": "mssql", "type": "db"},
}
return PyMSSQLConnectionProxy(wrapped(*args, **kwargs), destination_info=destination_info)

Expand Down
1 change: 0 additions & 1 deletion elasticapm/instrumentation/packages/python_memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
destination = {
"address": address,
"port": port,
"service": {"name": "memcached", "resource": "memcached", "type": "cache"},
}
with capture_span(
name, span_type="cache", span_subtype="memcached", span_action="query", extra={"destination": destination}
Expand Down
2 changes: 1 addition & 1 deletion elasticapm/instrumentation/packages/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):


def get_destination_info(connection):
destination_info = {"service": {"name": "redis", "resource": "redis", "type": "db"}}
destination_info = {}
if hasattr(connection, "port"):
destination_info["port"] = connection.port
destination_info["address"] = connection.host
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import capture_span
from elasticapm.utils import get_host_from_url, sanitize_url, url_to_destination
from elasticapm.utils import get_host_from_url, sanitize_url


class RequestsInstrumentation(AbstractInstrumentedModule):
Expand All @@ -47,13 +47,12 @@ def call(self, module, method, wrapped, instance, args, kwargs):
signature = request.method.upper()
signature += " " + get_host_from_url(request.url)
url = sanitize_url(request.url)
destination = url_to_destination(url)

with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
response = wrapped(*args, **kwargs)
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from elasticapm.conf import constants
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, capture_span, execution_context
from elasticapm.utils import compat, default_ports, sanitize_url, url_to_destination
from elasticapm.utils import compat, default_ports, sanitize_url
from elasticapm.utils.disttracing import TracingOptions


Expand Down Expand Up @@ -74,7 +74,6 @@ def call(self, module, method, wrapped, instance, args, kwargs):
host = request_host(request_object)

url = sanitize_url(request_object.get_full_url())
destination = url_to_destination(url)
signature = method.upper() + " " + host

transaction = execution_context.get_transaction()
Expand All @@ -83,7 +82,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
# if urllib has been called in a leaf span, this span might be a DroppedSpan.
Expand Down
5 changes: 2 additions & 3 deletions elasticapm/instrumentation/packages/urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from elasticapm.conf import constants
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import DroppedSpan, capture_span, execution_context
from elasticapm.utils import default_ports, url_to_destination
from elasticapm.utils import default_ports
from elasticapm.utils.disttracing import TracingOptions


Expand Down Expand Up @@ -106,15 +106,14 @@ def call(self, module, method, wrapped, instance, args, kwargs):
signature = method.upper() + " " + host

url = "%s://%s%s" % (instance.scheme, host, url)
destination = url_to_destination(url)

transaction = execution_context.get_transaction()

with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}, "destination": destination},
extra={"http": {"url": url}},
leaf=True,
) as span:
# if urllib3 has been called in a leaf span, this span might be a DroppedSpan.
Expand Down
Loading

0 comments on commit 5ae5cf6

Please sign in to comment.