question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

elasticapm.label() "No transaction currently active." in celery integration. elasticapm.set_custom_context works.

See original GitHub issue

Describe the bug: Using Python Celery contrib implementation, i’m trying to use elasticapm.label() API instead of elasticapm.set_custom_context() in the same point in the code (in a transaction specifically) doesn’t seem to work and return an error.

This works as intended:

import elasticapm
from elasticapm.contrib.celery import register_exception_tracking, register_instrumentation

from .celery_app import app

elasticapm.instrument()
apm_client = elasticapm.Client()
register_instrumentation(apm_client)
register_exception_tracking(apm_client)

@app.task(bind=True, base=CustomBaseTaskClass)
def MyCoolFunction(self, instance_id, payload: str) -> int:
    elasticapm.set_custom_context({
        'instance_id': instance_id,
        'queue_name': self.request.delivery_info['routing_key'],
        'job_id': self.request.id
    })

    ...

This, isn’t working:

import elasticapm
from elasticapm.contrib.celery import register_exception_tracking, register_instrumentation

from .celery_app import app

elasticapm.instrument()
apm_client = elasticapm.Client()
register_instrumentation(apm_client)
register_exception_tracking(apm_client)

@app.task(bind=True, base=CustomBaseTaskClass)
def MyCoolFunction(self, instance_id, payload: str) -> int:
    elasticapm.label(
        instance_id = instance_id,
        queue_name = self.request.delivery_info['routing_key'],
        job_id = self.request.id
    )

    ...

returning the following error:

[2021-09-16 09:40:07,162: WARNING/ForkPoolWorker-1] Ignored labels instance_id, queue_name, job_id. No transaction currently active.

As reported in the documentation APM Overview [7.14] » Data Model » Metadata it should apply at the same entities as set_custom_context API, plus on Spans.

Environment

  • OS: Ubuntu 20.10
  • Python version: 3.8.10
  • Framework and version: Celery 5.1.2
  • APM Server version: 7.12.0
  • Agent version: 6.4.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
riccardomassullocommented, Sep 17, 2021

@zikphil thanks for the info, however this doesn’t explain the difference in behavior between set_custom_context and label API. However, the auto instrumentation for celery seems to work good, until i found this label API problem with Celery 5.1.0. Look at the following open issue, i commented how i achieved auto instrumentation for Celery Issue 685, official documentation about standalone celery auto instrumentation wasn’t really clear.

0reactions
basepicommented, Nov 4, 2021

Excellent! Thanks for the update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Agent API | APM Node.js Agent Reference [3.x] - Elastic
If no transaction is currently active, nothing happens. Note that the agent will do this for you automatically for all regular HTTP transactions....
Read more >
How to use the elastic-apm-node.setCustomContext function ...
To help you get started, we've selected a few elastic-apm-node. ... refDate, nbMinutes) fetchEventsTransaction.end() fetchEventsTransaction = null // set ...
Read more >
This.currentTransaction turns to null after a POST request #515
My application uses express to manage http requests. I used the function setCustomContext in order to add a message to the log, ...
Read more >
Elastic apm - Disable transaction/span programatically for ...
Looks like it can be done using drop_event processor in api-server.yml. processors: - drop_event: when: equals: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found