Skip to content

Commit

Permalink
trace urllib3
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Hellegouarch <[email protected]>
  • Loading branch information
Lawouach committed Aug 2, 2023
1 parent 6bdb2f0 commit 27d93dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion chaostracing/oltp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
except ImportError:
logger.debug("Failed to import RequestsInstrumentor", exc_info=True)
HAS_REQUESTS = False
try:
from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor

HAS_URLLIB3 = True
except ImportError:
logger.debug("Failed to import URLLib3Instrumentor", exc_info=True)
HAS_URLLIB3 = False
from opentelemetry.propagate import set_global_textmap
from opentelemetry.sdk.resources import Resource, get_aggregated_resources
from opentelemetry.sdk.trace import Span, TracerProvider
Expand Down Expand Up @@ -152,6 +159,7 @@ def __init__(self) -> None:
self.current_span = None

def started(self, experiment: Experiment, journal: Journal) -> None:
logger.debug("Starting capturing OLTP traces")
stack = ExitStack()
span = stack.enter_context(new_span("experiment"))
span.set_attribute("chaostoolkit.experiment.title", experiment.get("title"))
Expand All @@ -162,6 +170,8 @@ def started(self, experiment: Experiment, journal: Journal) -> None:
self.current_span = span

def finish(self, journal: Journal) -> None:
logger.debug("Stopping capturing OLTP traces")

span = self.root_span
self.root_span = None

Expand All @@ -170,6 +180,8 @@ def finish(self, journal: Journal) -> None:

self.root_stack.close()

logger.debug("Finished capturing OLTP traces")

def interrupted(self, experiment: Experiment, journal: Journal) -> None:
self.root_span.set_attribute("chaostoolkit.experiment.interrupted", True)

Expand Down Expand Up @@ -439,7 +451,10 @@ def configure_traces(configuration: Configuration) -> None:


def configure_instrumentations(
trace_request: bool = False, trace_httpx: bool = False, trace_botocore: bool = False
trace_request: bool = False,
trace_httpx: bool = False,
trace_botocore: bool = False,
trace_urllib3: bool = False,
) -> None:
provider = trace.get_tracer_provider()

Expand All @@ -461,6 +476,12 @@ def configure_instrumentations(
else:
BotocoreInstrumentor().instrument(tracer_provider=provider)

if trace_urllib3:
if not HAS_URLLIB3:
logger.debug("Cannot trace urllib3 has its missing some dependency")
else:
URLLib3Instrumentor().instrument(tracer_provider=provider)


@contextmanager
def new_span(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ opentelemetry-exporter-prometheus>=1.12.0rc1
opentelemetry-propagator-b3>=1.15.0
opentelemetry-sdk-extension-aws>=2.0
opentelemetry-propagator-aws-xray>=1.0
opentelemetry-instrumentation-urllib3>=0.36b0

0 comments on commit 27d93dd

Please sign in to comment.