20
20
passed script without any instrumentation.
21
21
"""
22
22
23
+ import time
24
+
25
+ def to_otel_timestamp (ts ):
26
+ return int (ts * 1000000000 )
27
+
28
+ observer_ts_start = to_otel_timestamp (time .time ())
29
+ observer_mono_start = time .monotonic ()
30
+
31
+ def current_otel_time ():
32
+ return observer_ts_start + to_otel_timestamp (time .monotonic () - observer_mono_start )
33
+
23
34
import configparser
24
35
import functools
25
36
import inspect
@@ -117,6 +128,9 @@ def _init_tracing(configs: List[str], config_dir: str):
117
128
118
129
# On 3.10-3.12, the import of wrapt might trigger warnings, filter them:
119
130
simplefilter (action = "ignore" , category = DeprecationWarning )
131
+
132
+ import_ts_start = current_otel_time ()
133
+
120
134
import wrapt # type: ignore[import-untyped]
121
135
from opentelemetry import context , trace
122
136
from opentelemetry .baggage .propagation import W3CBaggagePropagator
@@ -127,6 +141,8 @@ def _init_tracing(configs: List[str], config_dir: str):
127
141
from opentelemetry .trace .propagation .tracecontext import (
128
142
TraceContextTextMapPropagator ,
129
143
)
144
+
145
+ import_ts_end = current_otel_time ()
130
146
except ImportError as err :
131
147
syslog .error ("missing opentelemetry dependencies: %s" , err )
132
148
return _span_noop , _patch_module_noop
@@ -359,6 +375,12 @@ def _patch_module(module_name):
359
375
for m in module_names :
360
376
_patch_module (m )
361
377
378
+ # Create spans to track observer.py's setup duration
379
+ t = tracers [0 ]
380
+ with t .start_as_current_span ("observer.py:init_tracing" , start_time = observer_ts_start ):
381
+ import_span = t .start_span ("observer.py:imports" , start_time = import_ts_start )
382
+ import_span .end (end_time = import_ts_end )
383
+
362
384
return span_of_tracers , _patch_module
363
385
364
386
@@ -373,7 +395,7 @@ def _patch_module(module_name):
373
395
# are not overridden and will be the defined no-op functions.
374
396
span , patch_module = _init_tracing (observer_configs , observer_config_dir )
375
397
376
- # If tracing is now operational, explicity set "OTEL_SDK_DISABLED" to "false".
398
+ # If tracing is now operational, explicitly set "OTEL_SDK_DISABLED" to "false".
377
399
# In our case, different from the standard, we want the tracing disabled by
378
400
# default, so if the env variable is not set the noop implementation is used.
379
401
os .environ ["OTEL_SDK_DISABLED" ] = "false"
@@ -420,6 +442,13 @@ def run(file):
420
442
print (e , file = sys .stderr ) # Print the exception message
421
443
print (traceback .format_exc (), file = sys .stderr ) # Print the traceback
422
444
return 139 # This is what the default SIGSEGV handler on Linux returns
445
+ except SystemExit as e : # catch SystemExit so we can close gracefully
446
+ _exit_code = e .code if e .code is not None else 0
447
+ debug ("Script exited with code %i" , _exit_code )
448
+ # Print the traceback if _exit_code is non-zero
449
+ if _exit_code :
450
+ print (traceback .format_exc (), file = sys .stderr )
451
+ return _exit_code
423
452
424
453
return run (argv0 )
425
454
0 commit comments