Skip to content

Exception raised in set_context() #541

Closed
@zry656565

Description

@zry656565

Describe the bug: ...

[host: noj-59dfd94498-45jdw|pid: 24294|timestamp: 1564716068.956608] [ERROR|elasticapm.errors:188] Exception during timing of request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/elasticapm/contrib/django/middleware/__init__.py", line 185, in process_response
    elasticapm.set_context(lambda: self.client.get_user_info(request), "user")
  File "/usr/local/lib/python3.7/dist-packages/elasticapm/traces.py", line 410, in set_context
    transaction.context[key].update(data)
TypeError: 'function' object is not iterable

Related codes:

def set_context(data, key="custom"):
transaction = execution_context.get_transaction()
if not transaction:
return
if callable(data) and transaction.is_sampled:
data = data()
# remove invalid characters from key names
if not callable(data): # if transaction wasn't sampled, data is still a callable here and can be ignored
for k in list(data.keys()):
if TAG_RE.search(k):
data[TAG_RE.sub("_", k)] = data.pop(k)
if key in transaction.context:
transaction.context[key].update(data)
else:
transaction.context[key] = data

And when I print data when meeting exception, the output is:

<function TracingMiddleware.process_response.<locals>.<lambda> at 0x7f39e36a00e0>

I guess maybe we have to deal with the situation that data is callable and transaction.is_sampled == False.

Expected behavior:

set_context() should handle data in lambda type well.

Environment (please complete the following information)

  • OS: Linux
  • Python version: 3.7.4
  • Framework and version: Django 2.1.9
  • APM Server version: -
  • Agent version: 4.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions