Ejemplo n.º 1
0
 def test_prefix_env(self, request_mock):
     prefix = "prefix-from-args"
     env_var = "OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX"
     os.environ[env_var] = prefix
     ElasticsearchInstrumentor().uninstrument()
     ElasticsearchInstrumentor().instrument()
     request_mock.return_value = (1, {}, {})
     del os.environ[env_var]
     self._test_prefix(prefix)
    def test_response_hook(self, request_mock):
        response_attribute_name = "db.query_result"

        def response_hook(span, response):
            if span and span.is_recording():
                span.set_attribute(response_attribute_name,
                                   json.dumps(response))

        ElasticsearchInstrumentor().uninstrument()
        ElasticsearchInstrumentor().instrument(response_hook=response_hook)

        response_payload = {
            "took": 9,
            "timed_out": False,
            "_shards": {
                "total": 1,
                "successful": 1,
                "skipped": 0,
                "failed": 0,
            },
            "hits": {
                "total": {
                    "value": 1,
                    "relation": "eq"
                },
                "max_score":
                0.18232156,
                "hits": [{
                    "_index": "test-index",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 0.18232156,
                    "_source": {
                        "name": "tester"
                    },
                }],
            },
        }

        request_mock.return_value = (
            1,
            {},
            json.dumps(response_payload),
        )
        es = Elasticsearch()
        es.get(index="test-index", doc_type="_doc", id=1)

        spans = self.get_finished_spans()

        self.assertEqual(1, len(spans))
        self.assertEqual(
            json.dumps(response_payload),
            spans[0].attributes[response_attribute_name],
        )
Ejemplo n.º 3
0
    def test_request_hook(self, request_mock):
        request_hook_method_attribute = "request_hook.method"
        request_hook_url_attribute = "request_hook.url"
        request_hook_kwargs_attribute = "request_hook.kwargs"

        def request_hook(span, method, url, kwargs):

            attributes = {
                request_hook_method_attribute: method,
                request_hook_url_attribute: url,
                request_hook_kwargs_attribute: json.dumps(kwargs),
            }

            if span and span.is_recording():
                span.set_attributes(attributes)

        ElasticsearchInstrumentor().uninstrument()
        ElasticsearchInstrumentor().instrument(request_hook=request_hook)

        request_mock.return_value = (
            1,
            {},
            '{"found": false, "timed_out": true, "took": 7}',
        )
        es = Elasticsearch()
        index = "test-index"
        doc_id = 1
        kwargs = {"params": {"test": True}}
        es.get(index=index, doc_type="_doc", id=doc_id, **kwargs)

        spans = self.get_finished_spans()

        self.assertEqual(1, len(spans))
        self.assertEqual(
            "GET", spans[0].attributes[request_hook_method_attribute]
        )
        self.assertEqual(
            f"/{index}/_doc/{doc_id}",
            spans[0].attributes[request_hook_url_attribute],
        )
        self.assertEqual(
            json.dumps(kwargs),
            spans[0].attributes[request_hook_kwargs_attribute],
        )
Ejemplo n.º 4
0
    def test_span_not_recording(self, request_mock):
        request_mock.return_value = (1, {}, {})
        mock_tracer = mock.Mock()
        mock_span = mock.Mock()
        mock_span.is_recording.return_value = False
        mock_tracer.start_span.return_value = mock_span
        with mock.patch("opentelemetry.trace.get_tracer") as tracer:
            tracer.return_value = mock_tracer
            Elasticsearch()
            self.assertFalse(mock_span.is_recording())
            self.assertTrue(mock_span.is_recording.called)
            self.assertFalse(mock_span.set_attribute.called)
            self.assertFalse(mock_span.set_status.called)

        ElasticsearchInstrumentor().uninstrument()
Ejemplo n.º 5
0
    def test_instrumentor(self, request_mock):
        request_mock.return_value = (1, {}, {})

        es = Elasticsearch()
        es.index(index="sw", doc_type="people", id=1, body={"name": "adam"})

        spans_list = self.get_ordered_finished_spans()
        self.assertEqual(len(spans_list), 1)
        span = spans_list[0]

        # Check version and name in span's instrumentation info
        # self.check_span_instrumentation_info(span, opentelemetry.instrumentation.elasticsearch)
        self.check_span_instrumentation_info(
            span, opentelemetry.instrumentation.elasticsearch)

        # check that no spans are generated after uninstrument
        ElasticsearchInstrumentor().uninstrument()

        es.index(index="sw", doc_type="people", id=1, body={"name": "adam"})

        spans_list = self.get_ordered_finished_spans()
        self.assertEqual(len(spans_list), 1)
Ejemplo n.º 6
0
 def tearDown(self):
     super().tearDown()
     with self.disable_logging():
         ElasticsearchInstrumentor().uninstrument()
Ejemplo n.º 7
0
 def setUp(self):
     super().setUp()
     self.tracer = self.tracer_provider.get_tracer(__name__)
     ElasticsearchInstrumentor().instrument()
Ejemplo n.º 8
0
 def test_prefix_arg(self, request_mock):
     prefix = "prefix-from-env"
     ElasticsearchInstrumentor().uninstrument()
     ElasticsearchInstrumentor(span_name_prefix=prefix).instrument()
     request_mock.return_value = (1, {}, {})
     self._test_prefix(prefix)