This gist depends on redis-server.
Start Celery:
celery -A test_celery worker --loglevel=info
Then run the script manually to creata a task:
python test_celery.py
from celery import Celery | |
import time, appoptics | |
from appoptics import loader | |
loader.load_inst_modules() | |
app = Celery( | |
'test_celery' | |
,broker= 'redis://localhost/0' | |
) | |
app.conf.update( | |
CELERY_RESULT_BACKEND = 'redis://localhost/1' | |
) | |
@appoptics.log_method('maths') | |
def maths(): | |
time.sleep(1) | |
@app.task | |
def add(x, y): | |
start_time = time.time() * 1e6 # start time for metric span | |
trace = appoptics.start_trace('Celery_task', keys=None, xtr=None) # start a trace | |
time.sleep(2) | |
maths() | |
result = x+y # the actual celery task | |
trace_context = appoptics.end_trace('Celery_task') #end the trace | |
status_code = 200 # set the status code of the current trasaction (maps to the UI) | |
stop_time = time.time() * 1e6 # stop the timing for the metric span | |
# Create metric span | |
appoptics.util.SwigSpan.createHttpSpan( | |
None, "analyze prices", int(stop_time - start_time), status_code, | |
"GET", 499 < status_code < 600, | |
) | |
return result | |
#for creating task data | |
if __name__ == '__main__': | |
result = add.delay(4, 4) | |
print( result.get() ) |