Skip to content
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

Add support for OTEL logging #80

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions projects/etos_suite_runner/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ requires-python = ">=3.9"
dependencies = [
"packageurl-python~=0.11",
"cryptography>=42.0.4,<43.0.0",
"etos_lib==4.4.2",
"etos_environment_provider==5.2.1",
"etos_lib==4.5.0",
"etos_environment_provider==5.3.0",
"opentelemetry-api~=1.21",
"opentelemetry-exporter-otlp~=1.21",
"opentelemetry-sdk~=1.21",
Expand Down
57 changes: 33 additions & 24 deletions projects/etos_suite_runner/src/etos_suite_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,65 @@
import os
from importlib.metadata import PackageNotFoundError, version

from etos_lib.logging.logger import setup_logging
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_NAMESPACE, SERVICE_VERSION, Resource
from opentelemetry.sdk.resources import (
SERVICE_NAME,
SERVICE_NAMESPACE,
SERVICE_VERSION,
OTELResourceDetector,
ProcessResourceDetector,
Resource,
get_aggregated_resources,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from etos_lib.logging.logger import setup_logging

try:
VERSION = version("etos_suite_runner")
except PackageNotFoundError:
VERSION = "Unknown"


BASE_DIR = os.path.dirname(os.path.relpath(__file__))
DEV = os.getenv("DEV", "false").lower() == "true"
ENVIRONMENT = "development" if DEV else "production"
os.environ["ENVIRONMENT_PROVIDER_DISABLE_LOGGING"] = "true"
setup_logging("ETOS Suite Runner", VERSION, ENVIRONMENT)


LOGGER = logging.getLogger(__name__)

# Setting OTEL_COLLECTOR_HOST will override the default OTEL collector endpoint.
# 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")
else:
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"]
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.getenv("OTEL_COLLECTOR_HOST")
elif "OTEL_EXPORTER_OTLP_ENDPOINT" in os.environ:
LOGGER.debug("Environment variable OTEL_EXPORTER_OTLP_ENDPOINT not used.")
LOGGER.debug("To specify an OpenTelemetry collector host use OTEL_COLLECTOR_HOST.")
del os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"]

if os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"):
LOGGER.info(
"Using OpenTelemetry collector: %s", os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")

if os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"):
OTEL_RESOURCE = Resource.create(
{
SERVICE_NAME: "etos-suite-runner",
SERVICE_VERSION: VERSION,
SERVICE_NAMESPACE: ENVIRONMENT,
},
)
PROVIDER = TracerProvider(
resource=Resource.create(
{
SERVICE_NAME: "etos-suite-runner",
SERVICE_VERSION: VERSION,
SERVICE_NAMESPACE: ENVIRONMENT,
}
)

OTEL_RESOURCE = get_aggregated_resources(
[OTELResourceDetector(), ProcessResourceDetector()],
).merge(OTEL_RESOURCE)
LOGGER.info(
"Using OpenTelemetry collector: %s",
os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
)
PROVIDER = TracerProvider(resource=OTEL_RESOURCE)
EXPORTER = OTLPSpanExporter()
PROCESSOR = BatchSpanProcessor(EXPORTER)
PROVIDER.add_span_processor(PROCESSOR)
trace.set_tracer_provider(PROVIDER)
setup_logging("ETOS Suite Runner", VERSION, ENVIRONMENT, OTEL_RESOURCE)
else:
setup_logging("ETOS Suite Runner", VERSION, ENVIRONMENT)
LOGGER.info("OpenTelemetry not enabled. OTEL_COLLECTOR_HOST not set.")
Loading