Skip to content

Commit

Permalink
Merge pull request #5639 from GabrielBuica/private/dbuica/CP-48995
Browse files Browse the repository at this point in the history
CP-48995: Instrument `XenAPI.py` to submit a `traceparent`
  • Loading branch information
robhoes authored Jun 5, 2024
2 parents 4877b1b + e5bb639 commit 99e05fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python3/packages/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ def _patch_module(module_name):
# If there are no configs, or an exception is raised, span and patch_module
# are not overridden and will be the defined no-op functions.
span, patch_module = _init_tracing(observer_configs, observer_config_dir)

# If tracing is now operational, explicity set "OTEL_SDK_DISABLED" to "false".
# In our case, different from the standard, we want the tracing disabled by
# default, so if the env variable is not set the noop implementation is used.
os.environ["OTEL_SDK_DISABLED"] = "false"
except Exception as exc:
syslog.error("Exception while setting up tracing, running script untraced: %s", exc)
span, patch_module = _span_noop, _patch_module_noop
Expand Down
22 changes: 22 additions & 0 deletions scripts/examples/python/XenAPI/XenAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
# --------------------------------------------------------------------

import gettext
import os
import socket
import sys

Expand All @@ -65,6 +66,15 @@
import http.client as httplib
import xmlrpc.client as xmlrpclib

otel = False
try:
if os.environ["OTEL_SDK_DISABLED"] == "false":
from opentelemetry import propagate
from opentelemetry.trace.propagation import set_span_in_context, get_current_span
otel = True

except Exception:
pass

translation = gettext.translation('xen-xm', fallback = True)

Expand Down Expand Up @@ -101,7 +111,19 @@ def connect(self):
class UDSTransport(xmlrpclib.Transport):
def add_extra_header(self, key, value):
self._extra_headers += [ (key,value) ]
def with_tracecontext(self):
if otel:
headers = {}
# pylint: disable=possibly-used-before-assignment
ctx = set_span_in_context(get_current_span())
# pylint: disable=possibly-used-before-assignment
propagators = propagate.get_global_textmap()
propagators.inject(headers, ctx)
self._extra_headers = []
for k, v in headers.items():
self.add_extra_header(k, v)
def make_connection(self, host):
self.with_tracecontext()
return UDSHTTPConnection(host)

def notimplemented(name, *args, **kwargs):
Expand Down

0 comments on commit 99e05fb

Please sign in to comment.