Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 committed May 3, 2024
1 parent de8d075 commit 11b187c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion projects/etos_suite_runner/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
PyScaffold==3.2.3
packageurl-python~=0.11
cryptography>=42.0.4,<43.0.0
etos_lib==4.1.1
etos_lib==4.2.0
etos_environment_provider~=4.2
opentelemetry-api~=1.21
opentelemetry-exporter-otlp~=1.21
Expand Down
7 changes: 4 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 @@ -47,9 +47,10 @@
if os.getenv("OTEL_COLLECTOR_HOST"):
os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = 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 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" in os.environ:
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(
Expand Down
11 changes: 7 additions & 4 deletions projects/etos_suite_runner/src/etos_suite_runner/esr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from environment_provider.environment import release_full_environment
from etos_lib import ETOS
from etos_lib.logging.logger import FORMAT_CONFIG
from etos_lib.opentelemetry.semconv import Attributes as SemConvAttributes
from jsontas.jsontas import JsonTas
import opentelemetry

Expand Down Expand Up @@ -74,8 +73,10 @@ def _request_environment(self, ids: list[str]) -> None:
span_name = "request_environment"
suite_context = get_current_context()
with self.otel_tracer.start_as_current_span(
span_name, context=suite_context, kind=opentelemetry.trace.SpanKind.CLIENT,
) as span:
span_name,
context=suite_context,
kind=opentelemetry.trace.SpanKind.CLIENT,
):
try:
provider = EnvironmentProvider(self.params.tercc.meta.event_id, ids, copy=False)
result = provider.run()
Expand Down Expand Up @@ -111,7 +112,9 @@ def _release_environment(self) -> None:
span_name = "release_full_environment"
suite_context = get_current_context()
with self.otel_tracer.start_as_current_span(
span_name, context=suite_context, kind=opentelemetry.trace.SpanKind.CLIENT,
span_name,
context=suite_context,
kind=opentelemetry.trace.SpanKind.CLIENT,
):
status, message = release_full_environment(
etos=self.etos, jsontas=jsontas, suite_id=self.params.tercc.meta.event_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from .otel_tracing import OpenTelemetryBase


class TestStartException(Exception):
"""Exception when starting tests."""

Expand Down Expand Up @@ -94,11 +95,9 @@ def run_tests(self, test_suite: dict) -> None:
request["auth"] = self.__auth(**request["auth"])
method = getattr(self.etos.http, request.pop("method").lower())
span_name = "start_execution_space"
with self.tracer.start_as_current_span(
span_name, kind=trace.SpanKind.CLIENT
) as span:
with self.tracer.start_as_current_span(span_name, kind=trace.SpanKind.CLIENT) as span:
span.set_attribute(SemConvAttributes.EXECUTOR_ID, executor["id"])
span.set_attribute("http.request.body", dumps(request, indent=4))
span.set_attribute("http.request.body", dumps(request))
try:
response = method(**request)
response.raise_for_status()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
class OpenTelemetryBase:
"""Base functionality for OpenTelemetry data collection."""

# pylint: disable=too-few-public-methods
@staticmethod
def _record_exception(exc) -> None:
"""Record the given exception to the current OpenTelemetry span."""
Expand All @@ -46,9 +47,9 @@ 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(',')
pairs = value.split(",")
for pair in pairs:
k, v = pair.split('=', 1)
k, v = pair.split("=", 1)
if k == key:
return [v]
return []
Expand All @@ -57,9 +58,10 @@ 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 [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."""
propagator = TraceContextTextMapPropagator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import logging
from multiprocessing.pool import ThreadPool

import opentelemetry.trace

from environment_provider.environment import release_full_environment
from etos_lib.logging.logger import FORMAT_CONFIG
from jsontas.jsontas import JsonTas
Expand Down Expand Up @@ -59,7 +57,9 @@ def _release_environment(self):
jsontas = JsonTas()
span_name = "release_full_environment"
with self.otel_tracer.start_as_current_span(
span_name, context=self.otel_suite_context, kind=opentelemetry.trace.SpanKind.CLIENT,
span_name,
context=self.otel_suite_context,
kind=opentelemetry.trace.SpanKind.CLIENT,
):
status, message = release_full_environment(
etos=self.etos, jsontas=jsontas, suite_id=self.params.tercc.meta.event_id
Expand Down
10 changes: 6 additions & 4 deletions projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from typing import Iterator

from eiffellib.events import EiffelTestSuiteStartedEvent
import opentelemetry.trace
from environment_provider.lib.registry import ProviderRegistry
from environment_provider.environment import release_environment
from etos_lib import ETOS
Expand Down Expand Up @@ -108,7 +107,9 @@ def start(self, identifier: str, otel_context: opentelemetry.context.context.Con
# 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, kind=opentelemetry.trace.SpanKind.CLIENT,
span_name,
context=otel_context,
kind=opentelemetry.trace.SpanKind.CLIENT,
) as span:
span.set_attribute(SemConvAttributes.SUBSUITE_ID, identifier)
FORMAT_CONFIG.identifier = identifier
Expand Down Expand Up @@ -153,7 +154,8 @@ def release(self, testrun_id) -> None:

span_name = "release_environment"
with self.otel_tracer.start_as_current_span(
span_name, kind=opentelemetry.trace.SpanKind.CLIENT,
span_name,
kind=opentelemetry.trace.SpanKind.CLIENT,
) as span:
failure = release_environment(
etos=self.etos,
Expand All @@ -162,7 +164,7 @@ def release(self, testrun_id) -> None:
sub_suite=self.environment,
)
span.set_attribute(SemConvAttributes.TESTRUN_ID, testrun_id)
span.set_attribute(SemConvAttributes.ENVIRONMENT, json.dumps(self.environment, indent=4))
span.set_attribute(SemConvAttributes.ENVIRONMENT, json.dumps(self.environment))
if failure is not None:
self.logger.exception(
"Failed to check in %r", self.environment["id"], extra={"user_log": True}
Expand Down

0 comments on commit 11b187c

Please sign in to comment.