Skip to content

Commit

Permalink
fix: adjust styling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
moaddib666 committed Nov 30, 2020
1 parent a1cb634 commit ce69a70
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Changelog

## Version 0.16b1

Released 2020-11-27
## Unreleased

- Initial release
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@
_SUPPRESS_URLLIB_INSTRUMENTATION_KEY = "suppress_urllib_instrumentation"


class URLLibInstrumentor(BaseInstrumentor, MetricMixin):
"""An instrumentor for urllib
See `BaseInstrumentor`
"""

def _instrument(self, **kwargs):
"""Instruments urllib module
Args:
**kwargs: Optional arguments
``tracer_provider``: a TracerProvider, defaults to global
``span_callback``: An optional callback invoked before returning the http response.
Invoked with Span and http.client.HTTPResponse
``name_callback``: Callback which calculates a generic span name for an
outgoing HTTP request based on the method and url.
Optional: Defaults to get_default_span_name.
"""

_instrument(
tracer_provider=kwargs.get("tracer_provider"),
span_callback=kwargs.get("span_callback"),
name_callback=kwargs.get("name_callback"),
)

self.init_metrics(
__name__, __version__,
)

# pylint: disable=W0201
self.metric_recorder = HTTPMetricRecorder(
self.meter, HTTPMetricType.CLIENT
)

def _uninstrument(self, **kwargs):
_uninstrument()

def uninstrument_opener(
self, opener: OpenerDirector
): # pylint: disable=no-self-use
_uninstrument_from(opener, restore_as_bound_func=True)


# pylint: disable=unused-argument
# pylint: disable=R0915
def get_default_span_name(method):
Expand All @@ -72,7 +114,7 @@ def _instrument(tracer_provider=None, span_callback=None, name_callback=None):
opener_open = OpenerDirector.open

@functools.wraps(opener_open)
def instrumented_request(opener, fullurl, data=None, timeout=None):
def instrumented_open(opener, fullurl, data=None, timeout=None):

if isinstance(fullurl, str):
request_ = Request(fullurl, data)
Expand All @@ -85,11 +127,11 @@ def get_or_create_headers():
def call_wrapped():
return opener_open(opener, request_, data=data, timeout=timeout)

return _instrumented_requests_call(
return _instrumented_open_call(
opener, request_, call_wrapped, get_or_create_headers
)

def _instrumented_requests_call(
def _instrumented_open_call(
opener, request, call_wrapped, get_or_create_headers
): # pylint: disable=too-many-locals
if context.get_value("suppress_instrumentation") or context.get_value(
Expand Down Expand Up @@ -165,8 +207,8 @@ def _instrumented_requests_call(

return result

instrumented_request.opentelemetry_instrumentation_urllib_applied = True
OpenerDirector.open = instrumented_request
instrumented_open.opentelemetry_instrumentation_urllib_applied = True
OpenerDirector.open = instrumented_open


def _uninstrument():
Expand All @@ -189,45 +231,3 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False):
if restore_as_bound_func:
original = types.MethodType(original, instr_root)
setattr(instr_root, instr_func_name, original)


class URLLibInstrumentor(BaseInstrumentor, MetricMixin):
"""An instrumentor for urllib
See `BaseInstrumentor`
"""

def _instrument(self, **kwargs):
"""Instruments urllib module
Args:
**kwargs: Optional arguments
``tracer_provider``: a TracerProvider, defaults to global
``span_callback``: An optional callback invoked before returning the http response.
Invoked with Span and http.client.HTTPResponse
``name_callback``: Callback which calculates a generic span name for an
outgoing HTTP request based on the method and url.
Optional: Defaults to get_default_span_name.
"""

_instrument(
tracer_provider=kwargs.get("tracer_provider"),
span_callback=kwargs.get("span_callback"),
name_callback=kwargs.get("name_callback"),
)

self.init_metrics(
__name__, __version__,
)

# pylint: disable=W0201
self.metric_recorder = HTTPMetricRecorder(
self.meter, HTTPMetricType.CLIENT
)

def _uninstrument(self, **kwargs):
_uninstrument()

def uninstrument_opener(
self, opener: OpenerDirector
): # pylint: disable=no-self-use
_uninstrument_from(opener, restore_as_bound_func=True)

0 comments on commit ce69a70

Please sign in to comment.