def test_disable_tracing_hostname_explicit(self): blacklist_paths = ['127.0.0.1', '192.168.0.1:80'] url = '127.0.0.1:8080' disable_tracing = utils.disable_tracing_hostname(url, blacklist_paths) self.assertFalse(disable_tracing) url = '127.0.0.1:80' disable_tracing = utils.disable_tracing_hostname(url, blacklist_paths) self.assertFalse(disable_tracing)
def call(url, *args, **kwargs): blacklist_hostnames = execution_context.get_opencensus_attr( 'blacklist_hostnames') parsed_url = urlparse(url) if parsed_url.port is None: dest_url = parsed_url.hostname else: dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port) if utils.disable_tracing_hostname(dest_url, blacklist_hostnames): return requests_func(url, *args, **kwargs) _tracer = execution_context.get_opencensus_tracer() _span = _tracer.start_span() _span.name = '[requests]{}'.format(requests_func.__name__) _span.span_kind = span_module.SpanKind.CLIENT # Add the requests url to attributes _tracer.add_attribute_to_current_span(HTTP_URL, url) result = requests_func(url, *args, **kwargs) # Add the status code to attributes _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, str(result.status_code)) _tracer.end_span() return result
def call(self, method, url, body, headers, *args, **kwargs): _tracer = execution_context.get_opencensus_tracer() blacklist_hostnames = execution_context.get_opencensus_attr( 'blacklist_hostnames') dest_url = '{}:{}'.format(self.host, self.port) if utils.disable_tracing_hostname(dest_url, blacklist_hostnames): return request_func(self, method, url, body, headers, *args, **kwargs) _span = _tracer.start_span() _span.span_kind = span_module.SpanKind.CLIENT _span.name = '[httplib]{}'.format(request_func.__name__) # Add the request url to attributes _tracer.add_attribute_to_current_span(HTTP_URL, url) # Add the request method to attributes _tracer.add_attribute_to_current_span(HTTP_METHOD, method) # Store the current span id to thread local. execution_context.set_opencensus_attr( 'httplib/current_span_id', _span.span_id) try: headers = headers.copy() headers.update(_tracer.propagator.to_headers( _span.context_tracer.span_context)) except Exception: # pragma: NO COVER pass return request_func(self, method, url, body, headers, *args, **kwargs)
def wrap_session_request(wrapped, instance, args, kwargs): """Wrap the session function to trace it.""" method = kwargs.get('method') or args[0] url = kwargs.get('url') or args[1] blacklist_hostnames = execution_context.get_opencensus_attr( 'blacklist_hostnames') parsed_url = urlparse(url) if parsed_url.port is None: dest_url = parsed_url.hostname else: dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port) if utils.disable_tracing_hostname(dest_url, blacklist_hostnames): return wrapped(*args, **kwargs) _tracer = execution_context.get_opencensus_tracer() _span = _tracer.start_span() _span.name = '[requests]{}'.format(method) _span.span_kind = span_module.SpanKind.CLIENT try: tracer_headers = _tracer.propagator.to_headers(_tracer.span_context) kwargs.setdefault('headers', {}).update(tracer_headers) except Exception: # pragma: NO COVER pass # Add the requests url to attributes _tracer.add_attribute_to_current_span(HTTP_URL, url) result = wrapped(*args, **kwargs) # Add the status code to attributes _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, str(result.status_code)) _tracer.end_span() return result
def test_disable_tracing_hostname_default(self): url = '127.0.0.1:8080' disable_tracing = utils.disable_tracing_hostname(url) self.assertFalse(disable_tracing)