Skip to content
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

handle destination_info for UnixDomainSocketConnection correctly #766

Merged
merged 3 commits into from
Mar 20, 2020
Merged
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
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"