def test_trace_none(self, mock_perform_req):
        init_tracing(self.tracer, trace_all_requests=False)

        set_active_span(DummySpan())

        self.es.get(index='test-index', doc_type='tweet', id=3)
        self.assertEqual(0, len(self.tracer.spans))
    def test_tracing(self, mock_perform_req):
        init_tracing(self.tracer, trace_all_requests=False, prefix='Prod007')

        mock_perform_req.return_value = {'hits': []}

        main_span = DummySpan()
        set_active_span(main_span)
        enable_tracing()

        body = {"any": "data", "timestamp": datetime.datetime.now()}
        res = self.es.index(index='test-index', doc_type='tweet', id=1,
                            body=body, params={'refresh': True})
        self.assertEqual(mock_perform_req.return_value, res)
        self.assertEqual(1, len(self.tracer.spans))
        self.assertEqual(self.tracer.spans[0].operation_name, 'Prod007/test-index/tweet/1')
        self.assertEqual(self.tracer.spans[0].is_finished, True)
        self.assertEqual(self.tracer.spans[0].child_of, main_span)
        self.assertEqual(self.tracer.spans[0].tags, {
            'component': 'elasticsearch-py',
            'db.type': 'elasticsearch',
            'db.statement': body,
            'span.kind': 'client',
            'elasticsearch.url': '/test-index/tweet/1',
            'elasticsearch.method': 'PUT',
            'elasticsearch.params': {'refresh': True},
        })
        def target1():
            set_active_span(DummySpan())
            enable_tracing()
            self.es.get(index='test-index', doc_type='tweet', id=1)

            ev.set()
            ev.wait()

            disable_tracing()
    def test_clear_span(self, mock_perform_req):
        init_tracing(self.tracer, trace_all_requests=False)

        enable_tracing()

        set_active_span(DummySpan())
        clear_active_span()

        self.es.get(index='test-index', doc_type='tweet', id=1)
        self.assertEqual(1, len(self.tracer.spans))
        self.assertEqual(None, self.tracer.spans[0].child_of)
    def test_trace_all_requests_span(self, mock_perform_req):
        init_tracing(self.tracer)

        main_span = DummySpan()
        set_active_span(main_span)

        for i in range(3):
            self.es.get(index='test-index', doc_type='tweet', id=i)

        self.assertEqual(3, len(self.tracer.spans))
        self.assertTrue(all(map(lambda x: x.is_finished, self.tracer.spans)))
        self.assertTrue(all(map(lambda x: x.child_of == main_span, self.tracer.spans)))
    def test_disable_tracing_span(self, mock_perform_req):
        init_tracing(self.tracer, trace_all_requests=False)

        main_span = DummySpan()
        set_active_span(main_span)

        # Make sure the active span was preserved
        enable_tracing()
        disable_tracing()
        self.assertEqual(main_span, get_active_span())

        # Make sure it was preserved, by tracing.
        enable_tracing()

        self.es.get(index='test-index', doc_type='tweet', id=1)
        self.assertEqual(1, len(self.tracer.spans))
        self.assertEqual(main_span, self.tracer.spans[0].child_of)
    def test_trace_error(self, mock_perform_req):
        init_tracing(self.tracer, trace_all_requests=False)

        main_span = DummySpan()
        set_active_span(main_span)
        enable_tracing()
        mock_perform_req.side_effect = RuntimeError()

        try:
            self.es.get(index='test-index', doc_type='tweet', id=1)
        except RuntimeError as exc:
            catched_exc = exc

        self.assertEqual(1, len(self.tracer.spans))
        self.assertEqual(True, self.tracer.spans[0].is_finished)
        self.assertEqual(main_span, self.tracer.spans[0].child_of)
        self.assertEqual('true', self.tracer.spans[0].tags['error'])
        self.assertEqual(catched_exc, self.tracer.spans[0].tags['error.object'])
Esempio n. 8
0
tracer = lightstep.Tracer(component_name='elasticsearch-explicit',
                          access_token='{your_lightstep_token}')

if __name__ == '__main__':
    elasticsearch_opentracing.init_tracing(tracer, trace_all_requests=False)

    es = Elasticsearch(
        '127.0.0.1',
        transport_class=elasticsearch_opentracing.TracingTransport)

    # We want to trace only the creation of the document, not the
    # index creation not checking the document was actually created.

    with tracer.start_span('main span') as main_span:
        elasticsearch_opentracing.set_active_span(main_span)

        es.indices.create('test-index', ignore=400)

        doc = {
            'author': 'john',
            'text': 'Find me if you can',
            'timestamp': datetime.now(),
        }

        elasticsearch_opentracing.enable_tracing()
        es.index(index='test-index', doc_type='tweet', id=1,
                 body=doc)  # Traced
        elasticsearch_opentracing.disable_tracing()

        res = es.get(index='test-index', doc_type='tweet', id=1)  # Not traced
#!/usr/bin/env python3

# https://github.com/opentracing-contrib/python-elasticsearch

import elasticsearch_opentracing

elasticsearch_opentracing.init_tracing(
    tracer)  # An OpenTracing compatible tracer.
es = Elasticsearch(transport_class=elasticsearch_opentracing.TracingTransport)

elasticsearch_opentracing.set_active_span(main_span)  # Optional.

es.index(index='test-index',
         doc_type='tweet',
         id=99,
         body={
             'author': 'linus',
             'text': 'Hello there',
             'timestamp': datetime.now(),
         })
res = es.get(index='test-index', doc_type='tweet', id=99)

elasticsearch_opentracing.clear_active_span()