File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
diracx-routers/src/diracx/routers Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 31
31
32
32
from diracx .core .settings import ServiceSettingsBase
33
33
34
+ logger = logging .getLogger (__name__ )
35
+
34
36
35
37
class OTELSettings (ServiceSettingsBase ):
36
38
"""Settings for the Open Telemetry Configuration."""
@@ -46,6 +48,29 @@ class OTELSettings(ServiceSettingsBase):
46
48
headers : Optional [dict [str , str ]] = None
47
49
48
50
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
+
49
74
def instrument_otel (app : FastAPI ) -> None :
50
75
"""Instrument the application to send OpenTelemetryData.
51
76
Metrics, Traces and Logs are sent to an OTEL collector.
@@ -59,6 +84,9 @@ def instrument_otel(app: FastAPI) -> None:
59
84
if not otel_settings .enabled :
60
85
return
61
86
87
+ if not randomly_enable_otel ():
88
+ return
89
+
62
90
# set the service name to show in traces
63
91
resource = Resource .create (
64
92
attributes = {
You can’t perform that action at this time.
0 commit comments