def test_should_skip_request(self): """ When calling should_skip_request with an enabled Pin and non-internal request returns False with a disabled Pin and non-internal request returns True with an enabled Pin and internal request returns True with a disabled Pin and internal request returns True """ # Enabled Pin and non-internal request self.tracer.enabled = True request = self.get_http_connection(SOCKET) pin = Pin.get_from(request) self.assertFalse(should_skip_request(pin, request)) # Disabled Pin and non-internal request self.tracer.enabled = False request = self.get_http_connection(SOCKET) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request)) # Enabled Pin and internal request self.tracer.enabled = True request = self.get_http_connection(self.tracer.writer.api.hostname, self.tracer.writer.api.port) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request)) # Disabled Pin and internal request self.tracer.enabled = False request = self.get_http_connection(self.tracer.writer.api.hostname, self.tracer.writer.api.port) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request))
def enable_tracing(self): # enabled tracing: # * middleware # * templates trace_app(self.app, self.tracer) patch() Pin.override(aiohttp_jinja2, tracer=self.tracer)
def enable_tracing(self): patch() Pin.override(aiohttp_jinja2, tracer=self.tracer)
def setUp(self): patch() self.tracer = get_dummy_tracer() Pin.override(httplib, tracer=self.tracer)
def get_https_connection(self, *args, **kwargs): conn = httplib.HTTPSConnection(*args, **kwargs) Pin.override(conn, tracer=self.tracer) return conn
async def patched_app_tracer(app_tracer): patch() app, tracer = app_tracer Pin.override(aiohttp_jinja2, tracer=tracer) return app, tracer
async def untraced_app_tracer(tracer, loop): patch() app = setup_app() Pin.override(aiohttp_jinja2, tracer=tracer) return app, tracer
def _wrap_urlopen(func, instance, args, kwargs): """ Wrapper function for the lower-level urlopen in urllib3 :param func: The original target function "urlopen" :param instance: The patched instance of ``HTTPConnectionPool`` :param args: Positional arguments from the target function :param kwargs: Keyword arguments from the target function :return: The ``HTTPResponse`` from the target function """ request_method = get_argument_value(args, kwargs, 0, "method") request_url = get_argument_value(args, kwargs, 1, "url") try: request_headers = get_argument_value(args, kwargs, 3, "headers") except ArgumentError: request_headers = None try: request_retries = get_argument_value(args, kwargs, 4, "retries") except ArgumentError: request_retries = None # HTTPConnectionPool allows relative path requests; convert the request_url to an absolute url if request_url.startswith("/"): request_url = parse.urlunparse(( instance.scheme, "{}:{}".format(instance.host, instance.port) if instance.port and instance.port not in DROP_PORTS else str(instance.host), request_url, None, None, None, )) parsed_uri = parse.urlparse(request_url) hostname = parsed_uri.netloc pin = Pin.get_from(instance) if not pin or not pin.enabled(): return func(*args, **kwargs) with pin.tracer.trace("urllib3.request", service=trace_utils.ext_service(pin, config.urllib3), span_type=SpanTypes.HTTP) as span: if config.urllib3.split_by_domain: span.service = hostname # If distributed tracing is enabled, propagate the tracing headers to downstream services if config.urllib3.distributed_tracing: if request_headers is None: request_headers = {} kwargs["headers"] = request_headers HTTPPropagator.inject(span.context, request_headers) if config.urllib3.analytics_enabled: span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config.urllib3.get_analytics_sample_rate()) retries = request_retries.total if isinstance( request_retries, urllib3.util.retry.Retry) else None # Call the target function response = None try: response = func(*args, **kwargs) finally: trace_utils.set_http_meta( span, integration_config=config.urllib3, method=request_method, url=request_url, status_code=None if response is None else response.status, query=parsed_uri.query, request_headers=request_headers, response_headers={} if response is None else dict(response.headers), retries_remain=retries, ) return response
def patch_algoliasearch(self): patch() Pin.override(self.index, tracer=self.tracer)
def setUp(self): super(HTTPLibBaseMixin, self).setUp() patch() Pin.override(httplib, tracer=self.tracer)
def _cursor(self, *args, **kwargs): cursor = yield from self.__wrapped__._cursor(*args, **kwargs) pin = Pin.get_from(self) if not pin: return cursor return self._self_cursor_cls(cursor, pin)
def _patched_search(func, instance, wrapt_args, wrapt_kwargs): """ wrapt_args is called the way it is to distinguish it from the 'args' argument to the algoliasearch.index.Index.search() method. """ if algoliasearch_version < (2, 0) and algoliasearch_version >= (1, 0): function_query_arg_name = 'args' elif algoliasearch_version >= (2, 0) and algoliasearch_version < (3, 0): function_query_arg_name = 'request_options' else: return func(*wrapt_args, **wrapt_kwargs) pin = Pin.get_from(instance) if not pin or not pin.enabled(): return func(*wrapt_args, **wrapt_kwargs) with pin.tracer.trace('algoliasearch.search', service=pin.service) as span: span.set_tag(SPAN_MEASURED_KEY) if not span.sampled: return func(*wrapt_args, **wrapt_kwargs) if config.algoliasearch.collect_query_text: span.set_tag('query.text', wrapt_kwargs.get('query', wrapt_args[0])) query_args = wrapt_kwargs.get( function_query_arg_name, wrapt_args[1] if len(wrapt_args) > 1 else None) if query_args and isinstance(query_args, dict): for query_arg, tag_name in QUERY_ARGS_DD_TAG_MAP.items(): value = query_args.get(query_arg) if value is not None: span.set_tag('query.args.{}'.format(tag_name), value) # Result would look like this # { # 'hits': [ # { # .... your search results ... # } # ], # 'processingTimeMS': 1, # 'nbHits': 1, # 'hitsPerPage': 20, # 'exhaustiveNbHits': true, # 'params': 'query=xxx', # 'nbPages': 1, # 'query': 'xxx', # 'page': 0 # } result = func(*wrapt_args, **wrapt_kwargs) if isinstance(result, dict): if result.get('processingTimeMS', None) is not None: span.set_metric('processing_time_ms', int(result['processingTimeMS'])) if result.get('nbHits', None) is not None: span.set_metric('number_of_hits', int(result['nbHits'])) return result
def traced_13_pipeline(func, instance, args, kwargs): pipeline = func(*args, **kwargs) pin = Pin.get_from(instance) if pin: pin.onto(pipeline) return pipeline
def enable_tracing(self): # aiohttp TestCase with the wrong context provider trace_app(self.app, self.tracer) patch() Pin.override(aiohttp_jinja2, tracer=self.tracer) self.tracer.configure(context_provider=DefaultContextProvider())