Skip to content

Instantly share code, notes, and snippets.

@wmak
Last active April 22, 2021 00:13
Show Gist options
  • Save wmak/fffab40bdbe5329f2ef8bd0a4ff44659 to your computer and use it in GitHub Desktop.
Save wmak/fffab40bdbe5329f2ef8bd0a4ff44659 to your computer and use it in GitHub Desktop.
Incorrect Parent Span Repro
{
"event_id": "58cf30f61e064c59b3f6cb0e158db393",
"project": 2301244,
"release": null,
"dist": null,
"platform": "python",
"message": "",
"datetime": "2021-04-22T00:02:39.743010+00:00",
"tags": [
[
"celery_task_id",
"4b8ea86e-5d27-498f-a570-64d88c6bffea"
],
[
"environment",
"production"
],
[
"level",
"info"
],
[
"runtime",
"CPython 3.8.2"
],
[
"runtime.name",
"CPython"
],
[
"server_name",
"Williams-MacBook-Pro.local"
],
[
"status",
"ok"
],
[
"transaction",
"main.add_together"
]
],
"_metrics": {
"bytes.ingested.event": 1079,
"bytes.stored.event": 1632
},
"contexts": {
"runtime": {
"name": "CPython",
"version": "3.8.2",
"build": "3.8.2 (default, Oct 2 2020, 10:45:42) \n[Clang 12.0.0 (clang-1200.0.32.27)]",
"type": "runtime"
},
"trace": {
"trace_id": "81b0b4ababc34509beafa5d802f2d94f",
"span_id": "bf51c619c015dd84",
"parent_span_id": "88731de923d086b8",
"op": "celery.task",
"status": "ok",
"type": "trace"
}
},
"culprit": "main.add_together",
"environment": "production",
"extra": {
"celery-job": {
"args": [],
"kwargs": {},
"task_name": "main.add_together"
},
"sys.argv": [
"/Users/wmak/.virtualenvs/celery-no-parent/bin/celery",
"-A",
"main.celery",
"worker"
]
},
"grouping_config": {
"enhancements": "eJybzDRxY3J-bm5-npWRgaGlroGxrpHxBABcYgcZ",
"id": "newstyle:2019-10-29"
},
"key_id": "1059069",
"level": "info",
"location": "main.add_together",
"logger": "",
"metadata": {
"location": "main.add_together",
"title": "main.add_together"
},
"received": 1619049759.768175,
"sdk": {
"name": "sentry.python",
"version": "1.0.0",
"integrations": [
"argv",
"atexit",
"celery",
"dedupe",
"excepthook",
"flask",
"logging",
"modules",
"stdlib",
"threading"
],
"packages": [
{
"name": "pypi:sentry-sdk",
"version": "1.0.0"
}
]
},
"spans": [],
"start_timestamp": 1619049759.238811,
"timestamp": 1619049759.74301,
"title": "main.add_together",
"transaction": "main.add_together",
"type": "transaction",
"version": "7"
}
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
from flask import Flask
from celery import Celery
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['result_backend'],
broker=app.config['broker_url']
)
celery.conf.update(
beat_schedule={
"time_scheduler": {
"task": "main.add_together",
"schedule": 5.0, # every 5 seconds
}
}
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
app = Flask(__name__)
app.config.update(
broker_url='redis://localhost:6379',
result_backend='redis://localhost:6379'
)
celery = make_celery(app)
sentry_sdk.init(
dsn=YOUR_DSN_HERE,
integrations=[
CeleryIntegration(),
FlaskIntegration(),
],
# To set a uniform sample rate
traces_sample_rate=1.0,
)
@app.route('/')
def hello_world():
add_together.delay()
return 'Hello, World!'
@celery.task()
def add_together():
import time
time.sleep(0.5)
celery==4.4.7
flask==1.1.2
sentry-sdk==1.0.0
redis==3.5.3
blinker==1.4
# Run redis on 6379
celery -A main.celery worker # start the celery worker
celery -A main.celery beat --loglevel=INFO --pidfile='' # start the celerybeat
# After about 5s should see the task scheduled:
# __ - ... __ - _
# LocalTime -> 2021-04-21 20:02:29
# Configuration ->
# . broker -> redis://localhost:6379//
# . loader -> celery.loaders.app.AppLoader
# . scheduler -> celery.beat.PersistentScheduler
# . db -> celerybeat-schedule
# . logfile -> [stderr]@%INFO
# . maxinterval -> 5.00 minutes (300s)
# [2021-04-21 20:02:29,218: INFO/MainProcess] beat: Starting...
# [2021-04-21 20:02:34,258: INFO/MainProcess] Scheduler: Sending due task time_scheduler (main.add_together)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment