Skip to content

Lock cache #472

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions diracx-routers/src/diracx/routers/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

from diracx.core.settings import ServiceSettingsBase

logger = logging.getLogger(__name__)


class OTELSettings(ServiceSettingsBase):
"""Settings for the Open Telemetry Configuration."""
Expand All @@ -46,6 +48,29 @@
headers: Optional[dict[str, str]] = None


def randomly_enable_otel() -> bool:
"""Enable opentelemetry if the first integer in reverse order in the hostname
is odd.
"""
enable_otel = False
try:
import socket

name = socket.gethostname()

for c in name[::-1]:
try:
enable_otel = bool(int(c) % 2)
break
except ValueError:
pass
except Exception as exe:
logger.exception(exe)

Check warning on line 68 in diracx-routers/src/diracx/routers/otel.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/otel.py#L67-L68

Added lines #L67 - L68 were not covered by tests

logger.info(f"Randomly enabling OTEL {enable_otel=}")
return enable_otel
Comment on lines +55 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enable_otel = False
try:
import socket
name = socket.gethostname()
for c in name[::-1]:
try:
enable_otel = bool(int(c) % 2)
break
except ValueError:
pass
except Exception as exe:
logger.exception(exe)
logger.info(f"Randomly enabling OTEL {enable_otel=}")
return enable_otel
enable_otel = bool(int(next(filter(str.isdigit, socket.gethostname() + "0"))) % 2)
logger.info(f"Randomly enabling OTEL {enable_otel=}")
return enable_otel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a hell lot less readable 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it doesn't go backward



def instrument_otel(app: FastAPI) -> None:
"""Instrument the application to send OpenTelemetryData.
Metrics, Traces and Logs are sent to an OTEL collector.
Expand All @@ -59,6 +84,9 @@
if not otel_settings.enabled:
return

if not randomly_enable_otel():
return

Check warning on line 88 in diracx-routers/src/diracx/routers/otel.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/otel.py#L88

Added line #L88 was not covered by tests

# set the service name to show in traces
resource = Resource.create(
attributes={
Expand Down
Loading