def enable_tracing(self):
     # enabled tracing:
     #   * middleware
     #   * templates
     trace_app(self.app, self.tracer)
     patch()
     Pin.override(aiohttp_jinja2, tracer=self.tracer)
Ejemplo n.º 2
0
 def test_service_name_for_pin(self):
     # ensure for backward compatibility that changing the service
     # name via the Pin object also updates integration config
     Pin(service='intake').onto(self.Klass)
     instance = self.Klass()
     cfg = config.get_from(instance)
     assert cfg['service_name'] == 'intake'
    def test_patch_all_auto_enable(self):
        patch_all()
        Pin.override(self.index, tracer=self.tracer)
        self.perform_search('test search')

        spans = self.get_spans()
        self.reset()
        assert spans, spans
        assert len(spans) == 1

        unpatch()

        self.perform_search('test search')

        spans = self.get_spans()
        assert not spans, spans
Ejemplo n.º 4
0
    def setUp(self):
        class Klass(object):
            """Helper class where a Pin is always attached"""
            pass

        # define the Class and attach a Pin to it
        self.Klass = Klass
        Pin(service='metrics').onto(Klass)
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, span_type=SEARCH_SPAN_TYPE) as span:
        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_OTEL_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
Ejemplo n.º 6
0
 def test_configuration_copy(self):
     # ensure when a Pin is used, the given configuration is copied
     global_config = {
         'service_name': 'service',
     }
     Pin(service='service', _config=global_config).onto(self.Klass)
     instance = self.Klass()
     cfg = config.get_from(instance)
     cfg['service_name'] = 'metrics'
     assert global_config['service_name'] == 'service'
Ejemplo n.º 7
0
 def test_service_attribute_priority(self):
     # ensure the `service` arg has highest priority over configuration
     # for backward compatibility
     global_config = {
         'service_name': 'primary_service',
     }
     Pin(service='service', _config=global_config).onto(self.Klass)
     instance = self.Klass()
     cfg = config.get_from(instance)
     assert cfg['service_name'] == 'service'
def patch():
    if getattr(pymemcache.client, '_opentelemetry_patch', False):
        return

    setattr(pymemcache.client, '_opentelemetry_patch', True)
    setattr(pymemcache.client.base, 'Client', WrappedClient)

    # Create a global pin with default configuration for our pymemcache clients
    Pin(app=memcachedx.SERVICE,
        service=memcachedx.SERVICE,
        app_type=memcachedx.TYPE).onto(pymemcache)
Ejemplo n.º 9
0
 def test_configuration_copy_upside_down(self):
     # ensure when a Pin is created, it does not copy the given configuration
     # until it's used for at least once
     global_config = {
         'service_name': 'service',
     }
     Pin(service='service', _config=global_config).onto(self.Klass)
     # override the global config: users do that before using the integration
     global_config['service_name'] = 'metrics'
     # use the Pin via `get_from`
     instance = self.Klass()
     cfg = config.get_from(instance)
     # it should have users updated value
     assert cfg['service_name'] == 'metrics'
Ejemplo n.º 10
0
    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))
def patch():
    if algoliasearch_version == (0, 0):
        return

    if getattr(algoliasearch, OTEL_PATCH_ATTR, False):
        return

    setattr(algoliasearch, '_opentelemetry_patch', True)

    pin = Pin(
        service=config.algoliasearch.service_name, app=APP_NAME,
        app_type=AppTypes.db
    )

    if algoliasearch_version < (2, 0) and algoliasearch_version >= (1, 0):
        _w(algoliasearch.index, 'Index.search', _patched_search)
        pin.onto(algoliasearch.index.Index)
    elif algoliasearch_version >= (2, 0) and algoliasearch_version < (3, 0):
        from algoliasearch import search_index
        _w(algoliasearch, 'search_index.SearchIndex.search', _patched_search)
        pin.onto(search_index.SearchIndex)
    else:
        return
Ejemplo n.º 12
0
 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())
 def enable_tracing(self):
     patch()
     Pin.override(aiohttp_jinja2, tracer=self.tracer)
 def patch_algoliasearch(self):
     patch()
     Pin.override(self.index, tracer=self.tracer)
Ejemplo n.º 15
0
 def get_https_connection(self, *args, **kwargs):
     conn = httplib.HTTPSConnection(*args, **kwargs)
     Pin.override(conn, tracer=self.tracer)
     return conn
Ejemplo n.º 16
0
    def setUp(self):
        super(HTTPLibBaseMixin, self).setUp()

        patch()
        Pin.override(httplib, tracer=self.tracer)