def dependency_patch(*args, **kwargs): result = ORIGINAL_REQUEST(*args, **kwargs) # Only collect request metric if sent from non-exporter thread if not execution_context.is_exporter(): count = dependency_map.get('count', 0) dependency_map['count'] = count + 1 return result
def call(self, method, url, body, headers, *args, **kwargs): # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return request_func(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 call(url, *args, **kwargs): # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return requests_func(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 dependency_patch(*args, **kwargs): result = ORIGINAL_REQUEST(*args, **kwargs) # Only collect request metric if sent from non-exporter thread if not execution_context.is_exporter(): # We don't want multiple threads updating this at once with _dependency_lock: count = dependency_map.get('count', 0) dependency_map['count'] = count + 1 return result
def call(url, *args, **kwargs): # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return requests_func(url, *args, **kwargs) excludelist_hostnames = execution_context.get_opencensus_attr( 'excludelist_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, excludelist_hostnames): return requests_func(url, *args, **kwargs) path = parsed_url.path if parsed_url.path else '/' _tracer = execution_context.get_opencensus_tracer() _span = _tracer.start_span() _span.name = '{}'.format(path) _span.span_kind = span_module.SpanKind.CLIENT # Add the component type to attributes _tracer.add_attribute_to_current_span("component", "HTTP") # Add the requests host to attributes _tracer.add_attribute_to_current_span(HTTP_HOST, dest_url) # Add the requests method to attributes _tracer.add_attribute_to_current_span(HTTP_METHOD, requests_func.__name__.upper()) # Add the requests path to attributes _tracer.add_attribute_to_current_span(HTTP_PATH, path) # Add the requests url to attributes _tracer.add_attribute_to_current_span(HTTP_URL, url) try: result = requests_func(url, *args, **kwargs) except requests.Timeout: _span.set_status(exceptions_status.TIMEOUT) raise except requests.URLRequired: _span.set_status(exceptions_status.INVALID_URL) raise except Exception as e: _span.set_status(exceptions_status.unknown(e)) raise else: # Add the status code to attributes _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, result.status_code) _span.set_status(utils.status_from_http_code(result.status_code)) return result finally: _tracer.end_span()
def wrap_session_request(wrapped, instance, args, kwargs): """Wrap the session function to trace it.""" # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return wrapped(*args, **kwargs) 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 call(self, *args, **kwargs): # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return response_func(self, *args, **kwargs) _tracer = execution_context.get_opencensus_tracer() current_span_id = execution_context.get_opencensus_attr( 'httplib/current_span_id') span = _tracer.current_span() # No corresponding request span is found, request not traced. if not span or span.span_id != current_span_id: return response_func(self, *args, **kwargs) result = response_func(self, *args, **kwargs) # Add the status code to attributes _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, result.status) _tracer.end_span() return result
def wrap_session_request(wrapped, instance, args, kwargs): """Wrap the session function to trace it.""" # Check if request was sent from an exporter. If so, do not wrap. if execution_context.is_exporter(): return wrapped(*args, **kwargs) method = kwargs.get('method') or args[0] url = kwargs.get('url') or args[1] excludelist_hostnames = execution_context.get_opencensus_attr( 'excludelist_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, excludelist_hostnames): return wrapped(*args, **kwargs) path = parsed_url.path if parsed_url.path else '/' _tracer = execution_context.get_opencensus_tracer() _span = _tracer.start_span() _span.name = '{}'.format(path) _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 component type to attributes _tracer.add_attribute_to_current_span("component", "HTTP") # Add the requests host to attributes _tracer.add_attribute_to_current_span(HTTP_HOST, dest_url) # Add the requests method to attributes _tracer.add_attribute_to_current_span(HTTP_METHOD, method.upper()) # Add the requests path to attributes _tracer.add_attribute_to_current_span(HTTP_PATH, path) # Add the requests url to attributes _tracer.add_attribute_to_current_span(HTTP_URL, url) try: result = wrapped(*args, **kwargs) except requests.Timeout: _span.set_status(exceptions_status.TIMEOUT) raise except requests.URLRequired: _span.set_status(exceptions_status.INVALID_URL) raise except Exception as e: _span.set_status(exceptions_status.unknown(e)) raise else: # Add the status code to attributes _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, result.status_code) _span.set_status(utils.status_from_http_code(result.status_code)) return result finally: _tracer.end_span()