Skip to content

Commit

Permalink
Introduce Context.suppress_instrumentation (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Sep 27, 2019
1 parent 2ab7b9d commit 74f2cec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from requests.sessions import Session

from opentelemetry import propagators
from opentelemetry.context import Context
from opentelemetry.trace import SpanKind


Expand Down Expand Up @@ -50,8 +51,8 @@ def enable(tracer):

@functools.wraps(wrapped)
def instrumented_request(self, method, url, *args, **kwargs):
# TODO: Check if we are in an exporter, cf. OpenCensus
# execution_context.is_exporter()
if Context.suppress_instrumentation:
return wrapped(self, method, url, *args, **kwargs)

# See
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md#http-client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import typing
from enum import Enum

from opentelemetry.context import Context
from opentelemetry.sdk import util

from .. import Span, SpanProcessor
Expand Down Expand Up @@ -72,11 +73,15 @@ def on_start(self, span: Span) -> None:
pass

def on_end(self, span: Span) -> None:
suppress_instrumentation = Context.suppress_instrumentation
try:
Context.suppress_instrumentation = True
self.span_exporter.export((span,))
# pylint: disable=broad-except
except Exception as exc:
logger.warning("Exception while exporting data: %s", exc)
finally:
Context.suppress_instrumentation = suppress_instrumentation

def shutdown(self) -> None:
self.span_exporter.shutdown()
Expand Down Expand Up @@ -176,11 +181,15 @@ def export(self) -> bool:
while idx < self.max_export_batch_size and self.queue:
self.spans_list[idx] = self.queue.pop()
idx += 1
suppress_instrumentation = Context.suppress_instrumentation
try:
Context.suppress_instrumentation = True
self.span_exporter.export(self.spans_list[:idx])
# pylint: disable=broad-except
except Exception:
logger.exception("Exception while exporting data.")
finally:
Context.suppress_instrumentation = suppress_instrumentation

# clean up list
for index in range(idx):
Expand Down

0 comments on commit 74f2cec

Please sign in to comment.