-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added context propagation support to celery instrumentation
- Loading branch information
Showing
11 changed files
with
225 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
instrumentation/opentelemetry-instrumentation-celery/tests/celery_test_tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from celery import Celery | ||
|
||
|
||
class Config: | ||
result_backend = "rpc" | ||
broker_backend = "memory" | ||
|
||
|
||
app = Celery(broker="memory:///") | ||
app.config_from_object(Config) | ||
|
||
|
||
@app.task | ||
def task_add(num_a, num_b): | ||
return num_a + num_b |
78 changes: 78 additions & 0 deletions
78
instrumentation/opentelemetry-instrumentation-celery/tests/test_tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import threading | ||
import time | ||
|
||
from opentelemetry.instrumentation.celery import CeleryInstrumentor | ||
from opentelemetry.test.test_base import TestBase | ||
from opentelemetry.trace import SpanKind | ||
|
||
from .celery_test_tasks import app, task_add | ||
|
||
|
||
class TestCeleryInstrumentation(TestBase): | ||
def setUp(self): | ||
super().setUp() | ||
self._worker = app.Worker(app=app, pool="solo", concurrency=1) | ||
self._thread = threading.Thread(target=self._worker.start) | ||
self._thread.daemon = True | ||
self._thread.start() | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
self._worker.stop() | ||
self._thread.join() | ||
|
||
def test_task(self): | ||
CeleryInstrumentor().instrument() | ||
|
||
result = task_add.delay(1, 2) | ||
while not result.ready(): | ||
time.sleep(0.05) | ||
|
||
spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) | ||
self.assertEqual(len(spans), 2) | ||
|
||
consumer, producer = spans | ||
|
||
self.assertEqual(consumer.name, "run/tests.celery_test_tasks.task_add") | ||
self.assertEqual(consumer.kind, SpanKind.CONSUMER) | ||
self.assert_span_has_attributes( | ||
consumer, | ||
{ | ||
"celery.action": "run", | ||
"celery.state": "SUCCESS", | ||
"messaging.destination": "celery", | ||
"celery.task_name": "tests.celery_test_tasks.task_add", | ||
}, | ||
) | ||
|
||
self.assertEqual( | ||
producer.name, "apply_async/tests.celery_test_tasks.task_add" | ||
) | ||
self.assertEqual(producer.kind, SpanKind.PRODUCER) | ||
self.assert_span_has_attributes( | ||
producer, | ||
{ | ||
"celery.action": "apply_async", | ||
"celery.task_name": "tests.celery_test_tasks.task_add", | ||
"messaging.destination_kind": "queue", | ||
"messaging.destination": "celery", | ||
}, | ||
) | ||
|
||
self.assertNotEqual(consumer.parent, producer.context) | ||
self.assertEqual(consumer.parent.span_id, producer.context.span_id) | ||
self.assertEqual(consumer.context.trace_id, producer.context.trace_id) |
Oops, something went wrong.