-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.py
41 lines (32 loc) · 1.23 KB
/
task.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from celery import Celery
from celery.signals import worker_process_init
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.celery import CeleryInstrumentor
app = Celery(
broker=os.environ['BROKER_URL'],
backend=os.environ['RESULT_BACKEND'],
)
@worker_process_init.connect
def init_worker(**kwargs):
# Configure the tracer to export traces to Jaeger
resource = Resource(attributes={
"service.name": os.environ.get("OTEL_SERVICE_NAME", "worker"),
})
trace.set_tracer_provider(TracerProvider(resource=resource))
otlp_exporter = OTLPSpanExporter(
endpoint=os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "otel-collector:4317"),
insecure=os.environ.get("OTEL_EXPORTER_OTLP_INSECURE", True),
)
span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
CeleryInstrumentor().instrument()
@app.task
def add(x, y):
result = x + y
print(result)
return result