-
Notifications
You must be signed in to change notification settings - Fork 711
Add metrics instrumentation celery #1679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ocelotl
merged 19 commits into
open-telemetry:main
from
Akochavi:metrics-instrumentation-celery
Jun 18, 2023
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
86a7f86
Add metrics instrumentation celery
Akochavi 6f6aae8
wip
Akochavi 1bf999e
lint fixes
Akochavi f8486ec
wip
Akochavi 8cd3daf
wip
Akochavi 4f1a7b5
Merge branch 'main' into metrics-instrumentation-celery
Akochavi da02e96
Merge branch 'main' into metrics-instrumentation-celery
Akochavi d491e94
wip
Akochavi fbc6200
wip
Akochavi 1678415
wip
Akochavi b0ce93c
Merge branch 'main' into metrics-instrumentation-celery
Akochavi 001cc06
wip
Akochavi b727d65
wip
Akochavi 4fabcbf
wip
Akochavi 3be197c
wip
Akochavi f8dd80b
wip
Akochavi 2966367
Merge branch 'main' into metrics-instrumentation-celery
shalevr 4d37fd4
Merge branch 'main' into metrics-instrumentation-celery
ocelotl 70f959c
Merge branch 'main' into metrics-instrumentation-celery
shalevr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
109 changes: 109 additions & 0 deletions
109
instrumentation/opentelemetry-instrumentation-celery/tests/test_metrics.py
This file contains hidden or 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,109 @@ | ||
import time | ||
import threading | ||
from timeit import default_timer | ||
from typing import Union, Optional | ||
|
||
from opentelemetry.sdk.metrics._internal.point import Metric | ||
from opentelemetry.sdk.metrics.export import ( | ||
HistogramDataPoint, | ||
NumberDataPoint, | ||
) | ||
from opentelemetry.test.test_base import TestBase | ||
from opentelemetry.instrumentation.celery import CeleryInstrumentor | ||
|
||
from .celery_test_tasks import task_add, app | ||
|
||
|
||
class TestMetrics(TestBase): | ||
def setUp(self): | ||
super().setUp() | ||
self._worker = app.Worker(app=app, pool="solo", concurrency=1, | ||
hostname='celery@akochavi') | ||
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 get_metrics(self): | ||
Akochavi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CeleryInstrumentor().instrument() | ||
result = task_add.delay(1, 2) | ||
|
||
timeout = time.time() + 60 * 1 # 1 minutes from now | ||
while not result.ready(): | ||
if time.time() > timeout: | ||
break | ||
time.sleep(0.05) | ||
CeleryInstrumentor().uninstrument() | ||
resource_metrics = ( | ||
self.memory_metrics_reader.get_metrics_data().resource_metrics | ||
) | ||
|
||
all_metrics = [] | ||
for metrics in resource_metrics: | ||
for scope_metrics in metrics.scope_metrics: | ||
all_metrics.extend(scope_metrics.metrics) | ||
|
||
return all_metrics | ||
Akochavi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def assert_metric_expected( | ||
Akochavi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
metric: Metric, | ||
expected_value: Union[int, float], | ||
expected_attributes: dict = None, | ||
est_delta: Optional[float] = None, | ||
): | ||
data_point = next(iter(metric.data.data_points)) | ||
|
||
if isinstance(data_point, HistogramDataPoint): | ||
self.assertEqual( | ||
data_point.count, | ||
1, | ||
) | ||
if est_delta is None: | ||
self.assertEqual( | ||
data_point.sum, | ||
expected_value, | ||
) | ||
else: | ||
self.assertAlmostEqual( | ||
data_point.sum, | ||
expected_value, | ||
delta=est_delta, | ||
) | ||
elif isinstance(data_point, NumberDataPoint): | ||
self.assertEqual( | ||
data_point.value, | ||
expected_value, | ||
) | ||
|
||
if expected_attributes: | ||
self.assertDictEqual( | ||
expected_attributes, | ||
dict(data_point.attributes), | ||
) | ||
|
||
def test_basic_metric(self): | ||
start_time = default_timer() | ||
task_runtime_estimated = (default_timer() - start_time) * 1000 | ||
|
||
metrics = self.get_metrics() | ||
self.assertEqual(len(metrics), 1) | ||
|
||
task_runtime = metrics[0] | ||
print(task_runtime) | ||
self.assertEqual( | ||
task_runtime.name, "flower.task.runtime.seconds" | ||
) | ||
self.assert_metric_expected( | ||
task_runtime, | ||
task_runtime_estimated, | ||
{ | ||
'task': 'tests.celery_test_tasks.task_add', | ||
'worker': 'celery@akochavi', | ||
}, | ||
est_delta=200, | ||
) | ||
Akochavi marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.