|
| 1 | +# Copyright 2020, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import typing |
| 17 | +import unittest |
| 18 | + |
| 19 | +from pymongo import MongoClient |
| 20 | + |
| 21 | +from opentelemetry import trace as trace_api |
| 22 | +from opentelemetry.ext.pymongo import trace_integration |
| 23 | +from opentelemetry.sdk.trace import Span, Tracer, TracerSource |
| 24 | +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor |
| 25 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( |
| 26 | + InMemorySpanExporter, |
| 27 | +) |
| 28 | + |
| 29 | +MONGODB_HOST = os.getenv("MONGODB_HOST ", "localhost") |
| 30 | +MONGODB_PORT = int(os.getenv("MONGODB_PORT ", "27017")) |
| 31 | +MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME ", "opentelemetry-tests") |
| 32 | +MONGODB_COLLECTION_NAME = "test" |
| 33 | + |
| 34 | + |
| 35 | +class TestFunctionalPymongo(unittest.TestCase): |
| 36 | + @classmethod |
| 37 | + def setUpClass(cls): |
| 38 | + cls._tracer_source = TracerSource() |
| 39 | + cls._tracer = Tracer(cls._tracer_source, None) |
| 40 | + cls._span_exporter = InMemorySpanExporter() |
| 41 | + cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) |
| 42 | + cls._tracer_source.add_span_processor(cls._span_processor) |
| 43 | + trace_integration(cls._tracer) |
| 44 | + client = MongoClient( |
| 45 | + MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000 |
| 46 | + ) |
| 47 | + db = client[MONGODB_DB_NAME] |
| 48 | + cls._collection = db[MONGODB_COLLECTION_NAME] |
| 49 | + |
| 50 | + def setUp(self): |
| 51 | + self._span_exporter.clear() |
| 52 | + |
| 53 | + def validate_spans(self): |
| 54 | + spans = self._span_exporter.get_finished_spans() |
| 55 | + self.assertEqual(len(spans), 2) |
| 56 | + for span in spans: |
| 57 | + if span.name == "rootSpan": |
| 58 | + root_span = span |
| 59 | + else: |
| 60 | + pymongo_span = span |
| 61 | + self.assertIsInstance(span.start_time, int) |
| 62 | + self.assertIsInstance(span.end_time, int) |
| 63 | + self.assertIsNot(root_span, None) |
| 64 | + self.assertIsNot(pymongo_span, None) |
| 65 | + self.assertIsNotNone(pymongo_span.parent) |
| 66 | + self.assertEqual(pymongo_span.parent.name, root_span.name) |
| 67 | + self.assertIs(pymongo_span.kind, trace_api.SpanKind.CLIENT) |
| 68 | + self.assertEqual( |
| 69 | + pymongo_span.attributes["db.instance"], MONGODB_DB_NAME |
| 70 | + ) |
| 71 | + self.assertEqual( |
| 72 | + pymongo_span.attributes["net.peer.name"], MONGODB_HOST |
| 73 | + ) |
| 74 | + self.assertEqual( |
| 75 | + pymongo_span.attributes["net.peer.port"], MONGODB_PORT |
| 76 | + ) |
| 77 | + |
| 78 | + def test_insert(self): |
| 79 | + """Should create a child span for insert |
| 80 | + """ |
| 81 | + with self._tracer.start_as_current_span("rootSpan"): |
| 82 | + self._collection.insert_one( |
| 83 | + {"name": "testName", "value": "testValue"} |
| 84 | + ) |
| 85 | + self.validate_spans() |
| 86 | + |
| 87 | + def test_update(self): |
| 88 | + """Should create a child span for update |
| 89 | + """ |
| 90 | + with self._tracer.start_as_current_span("rootSpan"): |
| 91 | + self._collection.update_one( |
| 92 | + {"name": "testName"}, {"$set": {"value": "someOtherValue"}} |
| 93 | + ) |
| 94 | + self.validate_spans() |
| 95 | + |
| 96 | + def test_find(self): |
| 97 | + """Should create a child span for find |
| 98 | + """ |
| 99 | + with self._tracer.start_as_current_span("rootSpan"): |
| 100 | + self._collection.find_one() |
| 101 | + self.validate_spans() |
| 102 | + |
| 103 | + def test_delete(self): |
| 104 | + """Should create a child span for delete |
| 105 | + """ |
| 106 | + with self._tracer.start_as_current_span("rootSpan"): |
| 107 | + self._collection.delete_one({"name": "testName"}) |
| 108 | + self.validate_spans() |
0 commit comments