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 target2(): ev.wait() enable_tracing() self.es.get(index='test-index', doc_type='tweet', id=2) ev.set() disable_tracing()
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_disable_tracing(self, mock_perform_req): init_tracing(self.tracer, trace_all_requests=False) enable_tracing() disable_tracing() self.assertEqual(0, len(self.tracer.spans)) self.es.get(index='test-index', doc_type='tweet', id=1) self.assertEqual(0, len(self.tracer.spans)) disable_tracing() # shouldn't cause a problem
def test_trace_result_tags(self, mock_perform_req): init_tracing(self.tracer, trace_all_requests=False) mock_perform_req.return_value = { 'found': False, 'timed_out': True, 'took': 7 } enable_tracing() self.es.get(index='test-index', doc_type='tweet', id=1) self.assertEqual(1, len(self.tracer.spans)) self.assertTrue(all(map(lambda x: x.is_finished, self.tracer.spans))) self.assertEqual('False', self.tracer.spans[0].tags['elasticsearch.found']) self.assertEqual('True', self.tracer.spans[0].tags['elasticsearch.timed_out']) self.assertEqual('7', self.tracer.spans[0].tags['elasticsearch.took'])
def test_trace_all_requests(self, mock_perform_req): init_tracing(self.tracer) 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))) enable_tracing() disable_tracing() # Shouldnt prevent further tracing self.es.get(index='test-index', doc_type='tweet', id=4) self.assertEqual(4, 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 is None, self.tracer.spans)))
def test_trace_after_error(self, mock_perform_req): init_tracing(self.tracer, trace_all_requests=False) enable_tracing() mock_perform_req.side_effect = RuntimeError() try: self.es.get(index='test-index', doc_type='tweet', id=1) except RuntimeError as exc: pass self.tracer.clear() # Should not cause any further tracing mock_perform_req.side_effect = None self.es.get(index='test-index', doc_type='tweet', id=1) self.assertEqual(0, len(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'])
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 print(res['_source']) elasticsearch_opentracing.clear_active_span()