|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from functools import wraps |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from opentelemetry import trace as trace_api |
| 21 | +from opentelemetry.ext.celery import CeleryInstrumentor |
| 22 | +from opentelemetry.sdk.trace import TracerProvider, export |
| 23 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( |
| 24 | + InMemorySpanExporter, |
| 25 | +) |
| 26 | + |
| 27 | +REDIS_HOST = os.getenv("REDIS_HOST", "localhost") |
| 28 | +REDIS_PORT = int(os.getenv("REDIS_PORT ", "6379")) |
| 29 | +REDIS_URL = "redis://{host}:{port}".format(host=REDIS_HOST, port=REDIS_PORT) |
| 30 | +BROKER_URL = "{redis}/{db}".format(redis=REDIS_URL, db=0) |
| 31 | +BACKEND_URL = "{redis}/{db}".format(redis=REDIS_URL, db=1) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="session") |
| 35 | +def celery_config(): |
| 36 | + return {"broker_url": BROKER_URL, "result_backend": BACKEND_URL} |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def celery_worker_parameters(): |
| 41 | + return { |
| 42 | + # See https://github.com/celery/celery/issues/3642#issuecomment-457773294 |
| 43 | + "perform_ping_check": False, |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(autouse=True) |
| 48 | +def patch_celery_app(celery_app, celery_worker): |
| 49 | + """Patch task decorator on app fixture to reload worker""" |
| 50 | + # See https://github.com/celery/celery/issues/3642 |
| 51 | + def wrap_task(fn): |
| 52 | + @wraps(fn) |
| 53 | + def wrapper(*args, **kwargs): |
| 54 | + result = fn(*args, **kwargs) |
| 55 | + celery_worker.reload() |
| 56 | + return result |
| 57 | + |
| 58 | + return wrapper |
| 59 | + |
| 60 | + celery_app.task = wrap_task(celery_app.task) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture(autouse=True) |
| 64 | +def instrument(tracer_provider, memory_exporter): |
| 65 | + CeleryInstrumentor().instrument(tracer_provider=tracer_provider) |
| 66 | + memory_exporter.clear() |
| 67 | + |
| 68 | + yield |
| 69 | + |
| 70 | + CeleryInstrumentor().uninstrument() |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture(scope="session") |
| 74 | +def tracer_provider(memory_exporter): |
| 75 | + original_tracer_provider = trace_api.get_tracer_provider() |
| 76 | + |
| 77 | + tracer_provider = TracerProvider() |
| 78 | + |
| 79 | + span_processor = export.SimpleExportSpanProcessor(memory_exporter) |
| 80 | + tracer_provider.add_span_processor(span_processor) |
| 81 | + |
| 82 | + trace_api.set_tracer_provider(tracer_provider) |
| 83 | + |
| 84 | + yield tracer_provider |
| 85 | + |
| 86 | + trace_api.set_tracer_provider(original_tracer_provider) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture(scope="session") |
| 90 | +def memory_exporter(): |
| 91 | + memory_exporter = InMemorySpanExporter() |
| 92 | + return memory_exporter |
0 commit comments