diff --git a/Dockerfile b/Dockerfile index 8356e5a..ad30cd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,13 +8,6 @@ FROM python:3.9-slim-buster COPY --from=build /src/projects/etos_suite_runner/dist/*.whl /tmp # hadolint ignore=DL3013 -# hadolint ignore=DL3008 -#RUN apt-get update && \ -# apt-get install -y gcc libc-dev --no-install-recommends && \ -# pip install --no-cache-dir /tmp/*.whl && \ -# apt-get purge -y --auto-remove gcc libc-dev && \ -# rm -rf /var/lib/apt/lists/* && \ -# groupadd -r etos && useradd -r -m -s /bin/false -g etos etos RUN pip install --no-cache-dir /tmp/*.whl && groupadd -r etos && useradd -r -m -s /bin/false -g etos etos USER etos diff --git a/projects/etos_suite_runner/src/etos_suite_runner/__init__.py b/projects/etos_suite_runner/src/etos_suite_runner/__init__.py index 7355620..cea339f 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/__init__.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/__init__.py @@ -42,12 +42,19 @@ LOGGER = logging.getLogger(__name__) # Setting OTEL_COLLECTOR_HOST will override the default OTEL collector endpoint. -# This is needed when using the cluster-level OTEL collector instead of sidecar collector. +# This is needed because Suite Runner uses the cluster-level OpenTelemetry collector +# instead of a sidecar collector. if os.getenv("OTEL_COLLECTOR_HOST"): os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = os.getenv("OTEL_COLLECTOR_HOST") - LOGGER.info("Using OTEL collector: %s", os.getenv("OTEL_COLLECTOR_HOST")) +else: + LOGGER.debug("Environment variable OTEL_EXPORTER_OTLP_TRACES_ENDPOINT not used.") + LOGGER.debug("To specify an OpenTelemetry collector host use OTEL_COLLECTOR_HOST.") + del os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] if os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"): + LOGGER.info( + "Using OpenTelemetry collector: %s", os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT") + ) PROVIDER = TracerProvider( resource=Resource.create( { @@ -62,4 +69,4 @@ PROVIDER.add_span_processor(PROCESSOR) trace.set_tracer_provider(PROVIDER) else: - LOGGER.info("Suite runner OTEL_EXPORTER_OTLP_TRACES_ENDPOINT not set!") + LOGGER.info("OpenTelemetry not enabled. OTEL_COLLECTOR_HOST not set.") diff --git a/projects/etos_suite_runner/src/etos_suite_runner/esr.py b/projects/etos_suite_runner/src/etos_suite_runner/esr.py index 5621f8f..031fd37 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/esr.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/esr.py @@ -34,7 +34,7 @@ from .lib.esr_parameters import ESRParameters from .lib.exceptions import EnvironmentProviderException from .lib.runner import SuiteRunner -from .otel_tracing import get_current_context +from .lib.otel_tracing import get_current_context # Remove spam from pika. logging.getLogger("pika").setLevel(logging.WARNING) diff --git a/projects/etos_suite_runner/src/etos_suite_runner/otel_tracing.py b/projects/etos_suite_runner/src/etos_suite_runner/lib/otel_tracing.py similarity index 70% rename from projects/etos_suite_runner/src/etos_suite_runner/otel_tracing.py rename to projects/etos_suite_runner/src/etos_suite_runner/lib/otel_tracing.py index a80d94e..af8e74f 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/otel_tracing.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/lib/otel_tracing.py @@ -20,7 +20,7 @@ import os import opentelemetry -from opentelemetry.propagators.textmap import Getter, Setter +from opentelemetry.propagators.textmap import Getter from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator @@ -28,7 +28,10 @@ class EnvVarContextGetter(Getter): + """OpenTelemetry context getter class for environment variables.""" + def get(self, carrier, key): + """Get value using the given carrier variable and key.""" value = os.getenv(carrier) if value is not None: pairs = value.split(',') @@ -39,6 +42,7 @@ def get(self, carrier, key): return [] def keys(self, carrier): + """Get keys of the given carrier variable.""" value = os.getenv(carrier) if value is not None: return [pair.split('=')[0] for pair in value.split(',') if '=' in pair] @@ -46,21 +50,6 @@ def keys(self, carrier): def get_current_context() -> opentelemetry.context.context.Context: """Get current context propagated via environment variable OTEL_CONTEXT.""" - LOGGER.info("Current OpenTelemetry context env: %s", os.environ.get("OTEL_CONTEXT")) propagator = TraceContextTextMapPropagator() ctx = propagator.extract(carrier="OTEL_CONTEXT", getter=EnvVarContextGetter()) - LOGGER.info("Current OpenTelemetry context %s", ctx) - return ctx - - -def get_current_context_old() -> opentelemetry.context.context.Context: - """Get current context (propagated via environment variable OTEL_CONTEXT).""" - carrier = {} - LOGGER.info("Current OpenTelemetry context env: %s", os.environ.get("OTEL_CONTEXT")) - for kv in os.environ.get("OTEL_CONTEXT", "").split(","): - if kv: - k, v = kv.split("=", 1) - carrier[k] = v - ctx = opentelemetry.propagate.extract(carrier) - LOGGER.info("Current OpenTelemetry context %s", ctx) return ctx diff --git a/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py b/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py index ac4767f..0cfd2f3 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py @@ -23,7 +23,7 @@ import opentelemetry from .exceptions import EnvironmentProviderException -from ..otel_tracing import get_current_context +from .otel_tracing import get_current_context from .suite import TestSuite diff --git a/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py b/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py index 3a16b77..29d83dc 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py @@ -102,8 +102,8 @@ def start(self, identifier: str, otel_context: opentelemetry.context.context.Con :param identifier: An identifier for logs in this sub suite. """ - # OpenTelemetry context needs to be retrieved here: - # the subsuite is running in a separate process + # OpenTelemetry context needs to be explicitly given here when creating this new span. + # This is because the subsuite is running in a separate thread. span_name = "execute_testrunner" with self.otel_tracer.start_as_current_span(span_name, context=otel_context) as span: span.set_attribute(SemConvAttributes.SUBSUITE_ID, identifier)