Skip to content

Commit bc76f7c

Browse files
authored
Merge pull request #5870 from snwoods/private/stevenwo/CP-49876
CP-49876: Create spans for observer.py itself
2 parents ecafbe5 + 29344a7 commit bc76f7c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

python3/packages/observer.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
passed script without any instrumentation.
2121
"""
2222

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+
2334
import configparser
2435
import functools
2536
import inspect
@@ -117,6 +128,9 @@ def _init_tracing(configs: List[str], config_dir: str):
117128

118129
# On 3.10-3.12, the import of wrapt might trigger warnings, filter them:
119130
simplefilter(action="ignore", category=DeprecationWarning)
131+
132+
import_ts_start = current_otel_time()
133+
120134
import wrapt # type: ignore[import-untyped]
121135
from opentelemetry import context, trace
122136
from opentelemetry.baggage.propagation import W3CBaggagePropagator
@@ -127,6 +141,8 @@ def _init_tracing(configs: List[str], config_dir: str):
127141
from opentelemetry.trace.propagation.tracecontext import (
128142
TraceContextTextMapPropagator,
129143
)
144+
145+
import_ts_end = current_otel_time()
130146
except ImportError as err:
131147
syslog.error("missing opentelemetry dependencies: %s", err)
132148
return _span_noop, _patch_module_noop
@@ -359,6 +375,12 @@ def _patch_module(module_name):
359375
for m in module_names:
360376
_patch_module(m)
361377

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+
362384
return span_of_tracers, _patch_module
363385

364386

@@ -373,7 +395,7 @@ def _patch_module(module_name):
373395
# are not overridden and will be the defined no-op functions.
374396
span, patch_module = _init_tracing(observer_configs, observer_config_dir)
375397

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".
377399
# In our case, different from the standard, we want the tracing disabled by
378400
# default, so if the env variable is not set the noop implementation is used.
379401
os.environ["OTEL_SDK_DISABLED"] = "false"
@@ -420,6 +442,13 @@ def run(file):
420442
print(e, file=sys.stderr) # Print the exception message
421443
print(traceback.format_exc(), file=sys.stderr) # Print the traceback
422444
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
423452

424453
return run(argv0)
425454

0 commit comments

Comments
 (0)