Skip to content
Merged
Show file tree
Hide file tree
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
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".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"now" or "not"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, double negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double negation is a bit annoying, but I wanted to follow the opentelemetry standard...

# 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