Skip to content

Commit 623c906

Browse files
committed
TMP: randomly enable OTEL
1 parent e50babe commit 623c906

File tree

1 file changed

+28
-0
lines changed
  • diracx-routers/src/diracx/routers

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
from diracx.core.settings import ServiceSettingsBase
3333

34+
logger = logging.getLogger(__name__)
35+
3436

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

4850

51+
def randomly_enable_otel() -> bool:
52+
"""Enable opentelemetry if the first integer in reverse order in the hostname
53+
is odd.
54+
"""
55+
enable_otel = False
56+
try:
57+
import socket
58+
59+
name = socket.gethostname()
60+
61+
for c in name[::-1]:
62+
try:
63+
enable_otel = bool(int(c) % 2)
64+
break
65+
except ValueError:
66+
pass
67+
except Exception as exe:
68+
logger.exception(exe)
69+
70+
logger.info(f"Randomly enabling OTEL {enable_otel=}")
71+
return enable_otel
72+
73+
4974
def instrument_otel(app: FastAPI) -> None:
5075
"""Instrument the application to send OpenTelemetryData.
5176
Metrics, Traces and Logs are sent to an OTEL collector.
@@ -59,6 +84,9 @@ def instrument_otel(app: FastAPI) -> None:
5984
if not otel_settings.enabled:
6085
return
6186

87+
if not randomly_enable_otel():
88+
return
89+
6290
# set the service name to show in traces
6391
resource = Resource.create(
6492
attributes={

0 commit comments

Comments
 (0)