Skip to content

Commit

Permalink
handle destination_info for UnixDomainSocketConnection correctly (#766)
Browse files Browse the repository at this point in the history
* handle destination_info for UnixDomainSocketConnection correctly

closes #765
  • Loading branch information
beniwohli authored Mar 20, 2020
1 parent b47b53e commit affce46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ endif::[]
[[release-notes-5.x]]
=== Python Agent version 5.x
[[release-notes-5.5.2]]
==== v5.5.2
[float]
===== Bug fixes
* Fixed an issue with Redis using unix domain sockets and destination information {pull}766[#766]
[[release-notes-5.5.1]]
==== v5.5.1
Expand Down
19 changes: 12 additions & 7 deletions elasticapm/instrumentation/packages/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ class RedisConnectionInstrumentation(AbstractInstrumentedModule):
def call(self, module, method, wrapped, instance, args, kwargs):
span = execution_context.get_span()
if span and span.subtype == "redis":
port = int(instance.port) if instance.port else None
destination_info = {
"address": instance.host,
"port": port,
"service": {"name": "redis", "resource": "redis", "type": "db"},
}
span.context["destination"] = destination_info
span.context["destination"] = get_destination_info(instance)
return wrapped(*args, **kwargs)


def get_destination_info(connection):
destination_info = {"service": {"name": "redis", "resource": "redis", "type": "db"}}
if hasattr(connection, "port"):
destination_info["port"] = connection.port
destination_info["address"] = connection.host
elif hasattr(connection, "path"):
destination_info["port"] = None
destination_info["address"] = "unix://" + connection.path
return destination_info
12 changes: 10 additions & 2 deletions tests/instrumentation/redis_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@

import pytest # isort:skip

pytest.importorskip("redis") # isort:skip
redis = pytest.importorskip("redis") # isort:skip

import os
from functools import partial

import redis
from redis import UnixDomainSocketConnection
from redis.client import StrictRedis

from elasticapm.conf.constants import TRANSACTION
from elasticapm.instrumentation.packages.redis import get_destination_info
from elasticapm.traces import capture_span

pytestmark = [pytest.mark.redis]
Expand Down Expand Up @@ -155,3 +156,10 @@ def test_redis_client(instrument, elasticapm_client, redis_conn):
assert spans[2]["type"] == "test"

assert len(spans) == 3


def test_unix_domain_socket_connection_destination_info():
conn = UnixDomainSocketConnection("/some/path")
destination_info = get_destination_info(conn)
assert destination_info["port"] is None
assert destination_info["address"] == "unix:///some/path"

0 comments on commit affce46

Please sign in to comment.