Skip to content

Redis instrumentation breaks when host value is not present in the connection kwargs #389

@eexwhyzee

Description

@eexwhyzee

Recently upgraded from 0.11b0 to 0.19b0 and having issues getting the redis instrumentation to work with the fakeredis package that is being currently used to mock redis for testing.

It appears that the host value in the connection kwargs is not preserved in the fakeredis implementation (they explicitly remove it in their code) and is causing the tests to break.

Previously the tests were passing fine with 0.11b0 since it looks like the connection info is just omitted if they were not found.

Was wondering if _extract_conn_attributes can be a little bit more forgiving if the values aren't found? For instance, i was able to get the tests passing after i made slight changes to the _extract_conn_attributes function to have a default value if the host key was not found.

Thanks for any help in advance, and also willing to submit a PR if needed!

Describe your environment

  • python 3.6.9
  • opentelemetry-instrumentation-redis==0.19b0
  • fakeredis==1.5.0

Steps to reproduce
failing test:

conn_kwargs = {'db': 0, 'decode_responses': False, 'encoding': 'utf-8', 'encoding_errors': 'strict', ...}

    def _extract_conn_attributes(conn_kwargs):
        """ Transform redis conn info into dict """
        attributes = {
            "db.system": "redis",
        }
        db = conn_kwargs.get("db", 0)
        attributes["db.name"] = db
        attributes["db.redis.database_index"] = db
        try:
>           attributes["net.peer.name"] = conn_kwargs["host"]
E           KeyError: 'host'

conn_kwargs = {'db': 0, 'decode_responses': False, 'encoding': 'utf-8', 'encoding_errors': 'strict', ...}

    def _extract_conn_attributes(conn_kwargs):
        """ Transform redis conn info into dict """
        attributes = {
            "db.system": "redis",
        }
        db = conn_kwargs.get("db", 0)
        attributes["db.name"] = db
        attributes["db.redis.database_index"] = db
        try:
            attributes["net.peer.name"] = conn_kwargs["host"]
            attributes["net.peer.port"] = conn_kwargs.get("port", 6379)
            attributes["net.transport"] = "IP.TCP"
        except KeyError:
>           attributes["net.peer.name"] = conn_kwargs["path"]
E           KeyError: 'path'

redis connection kwargs vs fakeredis connection kwargs:

In [1]: import redis                                                                                     

In [2]: import fakeredis                                                                                 

In [3]: r_conn = redis.Redis(host='localhost', port=6379, db=0)                                          

In [4]: r_conn.connection_pool.connection_kwargs["host"]                                                 
Out[4]: 'localhost'

In [5]: faker_conn = fakeredis.FakeStrictRedis(host='localhost', port=6379, db=0)                        

In [6]: faker_conn.connection_pool.connection_kwargs["host"]                                             
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-6-48d7e1f1cda0> in <module>
----> 1 faker_conn.connection_pool.connection_kwargs["host"]

KeyError: 'host'

What is the expected behavior?
What did you expect to see?

What is the actual behavior?
What did you see instead?

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions