def trace_request(): if callable(skip_span) and skip_span(request): return op_name = request.endpoint if request.endpoint else request.path.strip( '/').replace('/', '_') if callable(operation_name): op_name = operation_name() or op_name span = None headers_carrier = dict(request.headers.items()) try: span_ctx = opentracing.tracer.extract( opentracing.Format.HTTP_HEADERS, headers_carrier) span = opentracing.tracer.start_span(operation_name=op_name, child_of=span_ctx) except (opentracing.InvalidCarrierException, opentracing.SpanContextCorruptedException): span = opentracing.tracer.start_span( operation_name=op_name, tags={'flask-no-propagation': True}) if span is None: span = opentracing.tracer.start_span(op_name) if request_attr: for attr in request_attr: if hasattr(request, attr): try: tag_value = str(getattr(request, attr)) tag_key = attr # Masking URL query and path params. if attr == 'url': tag_value = sanitize_url( tag_value, mask_url_query=mask_url_query, mask_url_path=mask_url_path) tag_key = ot_tags.HTTP_URL elif attr == 'method': tag_key = ot_tags.HTTP_METHOD if tag_value: span.set_tag(tag_key, tag_value) except Exception: pass if type(default_tags) is dict: for k, v in default_tags.items(): try: span.set_tag(k, v) except Exception: pass span.set_tag(ot_tags.COMPONENT, 'flask') span.set_tag(ot_tags.SPAN_KIND, ot_tags.SPAN_KIND_RPC_SERVER) # Use ``flask.request`` as in process context. request.current_span = span
def process_view(self, request, view_func, view_args, view_kwargs): if self._skip_span_callable and self._skip_span_callable(request, view_func, view_args, view_kwargs): return headers_carrier = self._get_headers(request) op_name = (self._op_name_callable(request, view_func, view_args, view_kwargs) if self._op_name_callable else view_func.__name__) span = None try: span_ctx = opentracing.tracer.extract(opentracing.Format.HTTP_HEADERS, headers_carrier) span = opentracing.tracer.start_span(operation_name=op_name, child_of=span_ctx) except (opentracing.InvalidCarrierException, opentracing.SpanContextCorruptedException): span = opentracing.tracer.start_span(operation_name=op_name, tags={'django-no-propagation': True}) span.set_tag(ot_tags.COMPONENT, 'django') span.set_tag(ot_tags.SPAN_KIND, ot_tags.SPAN_KIND_RPC_SERVER) span.set_tag(ot_tags.HTTP_METHOD, request.method) span.set_tag(ot_tags.HTTP_URL, sanitize_url(request.get_full_path())) # Default tags for k, v in self._default_tags.items(): try: span.set_tag(k, v) except Exception: # pragma: no cover pass request.current_span = span
def trace_request(): operation_name = request.endpoint span = None headers_carrier = dict(request.headers.items()) try: span_ctx = opentracing.tracer.extract( opentracing.Format.HTTP_HEADERS, headers_carrier) span = opentracing.tracer.start_span(operation_name=operation_name, child_of=span_ctx) except (opentracing.InvalidCarrierException, opentracing.SpanContextCorruptedException): span = opentracing.tracer.start_span( operation_name=operation_name, tags={'flask-no-propagation': True}) if span is None: span = opentracing.tracer.start_span(operation_name) if request_attr: for attr in request_attr: if hasattr(request, attr): try: tag_value = str(getattr(request, attr)) # Masking URL query and path params. if attr == 'url': tag_value = sanitize_url( tag_value, mask_url_query=mask_url_query, mask_url_path=mask_url_path) if tag_value: span.set_tag(attr, tag_value) except Exception: pass if type(default_tags) is dict: for k, v in default_tags.items(): try: span.set_tag(k, v) except Exception: pass span.set_tag(ot_tags.SPAN_KIND, ot_tags.SPAN_KIND_RPC_SERVER) # Use ``flask.request`` as in process context. request.current_span = span
def requests_send_wrapper(self, request, **kwargs): if ignore_url_patterns is not None: if any( re.match(pattern, request.url) for pattern in ignore_url_patterns): return __requests_http_send(self, request, **kwargs) op_name = '{}_{}'.format(OPERATION_NAME_PREFIX, request.method.lower()) k, request_span = get_span_from_kwargs(inspect_stack=False, **kwargs) kwargs.pop(k, None) components = parse.urlsplit(request.url) if request_span: (request_span.set_operation_name(op_name).set_tag( ot_tags.COMPONENT, 'requests').set_tag( ot_tags.PEER_HOSTNAME, components.hostname).set_tag( ot_tags.HTTP_URL, sanitize_url( request.url, mask_url_query=mask_url_query, mask_url_path=mask_url_path)).set_tag( ot_tags.HTTP_METHOD, request.method).set_tag( ot_tags.SPAN_KIND, ot_tags.SPAN_KIND_RPC_CLIENT).set_tag( 'timeout', kwargs.get('timeout'))) # Inject our current span context to outbound request try: carrier = {} opentracing.tracer.inject(request_span.context, Format.HTTP_HEADERS, carrier) request.headers.update(carrier) except opentracing.UnsupportedFormatException: logger.error('Failed to inject span context in request!') resp = __requests_http_send(self, request, **kwargs) request_span.set_tag(ot_tags.HTTP_STATUS_CODE, resp.status_code) if set_error_tag and not resp.ok: request_span.set_tag('error', True) return resp else: logger.warn('Failed to extract span during initiating request!') return __requests_http_send(self, request, **kwargs)
def test_sanitize_url(url, masked_q, masked_path, res): assert sanitize_url(url, mask_url_query=masked_q, mask_url_path=masked_path) == res