Skip to content
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

Introduce Context.suppress_instrumentation #181

Merged
merged 3 commits into from
Sep 27, 2019
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
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:
Copy link
Member Author

Choose a reason for hiding this comment

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

Minor: I was a bit struggling whether to put this in finally statement or not.

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)
Context.suppress_instrumentation = suppress_instrumentation

This looks nicer (symmetric), however if there is uncaught exception, or later someone added a return in the try statement, the context could get wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

And introducing a context manager seems to be an overkill here.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with the finally here, but also think a context manager in context would make sense.

with Context.set('suppress_instrumentation', True):
    # do stuff that shouldn't be instrumented

This would also let you use the set/reset token, which you can't access here since Context doesn't expose the ContextVars it uses under the hood.

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