Skip to content

Further improvements to pass traceparent XenAPI.py #5672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions scripts/examples/python/XenAPI/XenAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import os
import socket
import sys
import types

if sys.version_info[0] == 2:
import httplib as httplib
Expand All @@ -70,12 +71,42 @@
try:
if os.environ["OTEL_SDK_DISABLED"] == "false":
from opentelemetry import propagate
from opentelemetry.trace.propagation import set_span_in_context, get_current_span
from opentelemetry.trace.propagation import (
set_span_in_context,
get_current_span
)
otel = True

except Exception:
pass

def patch_transport(transport):
if otel and transport:
try:
original_make_connection = transport.make_connection

def with_tracecontext(self):
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._extra_headers += [(k, v)]

def make_connection_with_tracecontext(self, host):
with_tracecontext(self)
return original_make_connection(host)

transport.make_connection = types.MethodType(
make_connection_with_tracecontext, transport
)

except Exception:
pass

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

API_VERSION_1_1 = '1.1'
Expand All @@ -97,7 +128,6 @@ def _details_map(self):
return dict([(str(i), self.details[i])
for i in range(len(self.details))])


# Just a "constant" that we use to decide whether to retry the RPC
_RECONNECT_AND_RETRY = object()

Expand All @@ -110,25 +140,13 @@ 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)
self._extra_headers += [(key, value)]
def make_connection(self, host):
# compatibility with parent xmlrpclib.Transport HTTP/1.1 support
if self._connection and host == self._connection[0]:
return self._connection[1]

self._connection = host, UDSHTTPConnection(host)
self.with_tracecontext()
return self._connection[1]

def notimplemented(name, *args, **kwargs):
Expand Down Expand Up @@ -165,6 +183,8 @@ def __init__(self, uri, transport=None, encoding=None, verbose=0,
else:
xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding,
verbose, allow_none)
#patch the transport created by xmlrpclib.ServerProxy
patch_transport(self._ServerProxy__transport)
self.transport = transport
self._session = None
self.last_login_method = None
Expand Down
Loading