Skip to content

Commit

Permalink
Review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 committed Apr 26, 2024
1 parent b4155d0 commit 84f150b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 30 deletions.
7 changes: 0 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions projects/etos_suite_runner/src/etos_suite_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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.")
2 changes: 1 addition & 1 deletion projects/etos_suite_runner/src/etos_suite_runner/esr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import os

import opentelemetry
from opentelemetry.propagators.textmap import Getter, Setter
from opentelemetry.propagators.textmap import Getter
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator


LOGGER = logging.getLogger(__name__)


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(',')
Expand All @@ -39,28 +42,14 @@ 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]
return []

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
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 84f150b

Please sign in to comment.