def setUp(self):
     super().setUp()
     self._tracer = self.tracer_provider.get_tracer(__name__)
     self.instrumentor = PymongoInstrumentor()
     self.instrumentor.instrument()
     self.instrumentor._commandtracer_instance._tracer = self._tracer
     client = MongoClient(MONGODB_HOST,
                          MONGODB_PORT,
                          serverSelectionTimeoutMS=2000)
     db = client[MONGODB_DB_NAME]
     self._collection = db[MONGODB_COLLECTION_NAME]
示例#2
0
def create_app(config):
    app = Flask(__name__)
    # http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/
    # mongodb config
    app.config['MONGODB_SETTINGS'] = {
        'db': 'recall',
        'host': '127.0.0.1',
        'port': 27017
    }
    # tracing config
    app.config[tracing.SETTINGS_NAME] = {
        'service_name': 'my-helloworld-service',
        'agent_host_name': '192.168.110.252',
        'agent_port': 6831
    }
    # metrics
    metrics = PrometheusMetrics(app)
    # Tracing
    tracing.init_app(app)
    # OpenTelemetry pymongo Instrumentation
    PymongoInstrumentor().instrument()
    # OpenTelemetry WSGI Instrumentation
    # https://opentelemetry-python.readthedocs.io/en/stable/instrumentation/wsgi/wsgi.html#usage-flask
    # app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
    # OpenTelemetry Flask Instrumentation
    FlaskInstrumentor().instrument_app(app)
    # 初始化数据库配置
    db.init_app(app)
    app.json_encoder = RecallJSONEncoder
    # app.response_class = RecallResponse
    app.register_blueprint(index.controller, url_prefix="/")
    app.register_blueprint(admin.controller, url_prefix="/admin")
    return app
    def test_pymongo_instrumentor(self):
        mock_register = mock.Mock()
        patch = mock.patch("pymongo.monitoring.register",
                           side_effect=mock_register)
        with patch:
            PymongoInstrumentor().instrument()

        self.assertTrue(mock_register.called)
示例#4
0
 def setUpClass(cls):
     super().setUpClass()
     cls._tracer = cls.tracer_provider.get_tracer(__name__)
     PymongoInstrumentor().instrument()
     client = MongoClient(
         MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000
     )
     db = client[MONGODB_DB_NAME]
     cls._collection = db[MONGODB_COLLECTION_NAME]
示例#5
0
    def test_uninstrument(self):
        # check that integration is working
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.memory_exporter.clear()
        self.assertEqual(len(spans), 1)

        # uninstrument and check not new spans are created
        PymongoInstrumentor().uninstrument()
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.memory_exporter.clear()
        self.assertEqual(len(spans), 0)

        # re-enable and check that it works again
        PymongoInstrumentor().instrument()
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans), 1)
class TestFunctionalPymongo(TestBase):
    def setUp(self):
        super().setUp()
        self._tracer = self.tracer_provider.get_tracer(__name__)
        self.instrumentor = PymongoInstrumentor()
        self.instrumentor.instrument()
        self.instrumentor._commandtracer_instance._tracer = self._tracer
        client = MongoClient(MONGODB_HOST,
                             MONGODB_PORT,
                             serverSelectionTimeoutMS=2000)
        db = client[MONGODB_DB_NAME]
        self._collection = db[MONGODB_COLLECTION_NAME]

    def tearDown(self):
        self.instrumentor.uninstrument()
        super().tearDown()

    def validate_spans(self):
        spans = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans), 2)
        for span in spans:
            if span.name == "rootSpan":
                root_span = span
            else:
                pymongo_span = span
            self.assertIsInstance(span.start_time, int)
            self.assertIsInstance(span.end_time, int)
        self.assertIsNot(root_span, None)
        self.assertIsNot(pymongo_span, None)
        self.assertIsNotNone(pymongo_span.parent)
        self.assertIs(pymongo_span.parent, root_span.get_span_context())
        self.assertIs(pymongo_span.kind, trace_api.SpanKind.CLIENT)
        self.assertEqual(pymongo_span.attributes[SpanAttributes.DB_NAME],
                         MONGODB_DB_NAME)
        self.assertEqual(pymongo_span.attributes[SpanAttributes.NET_PEER_NAME],
                         MONGODB_HOST)
        self.assertEqual(pymongo_span.attributes[SpanAttributes.NET_PEER_PORT],
                         MONGODB_PORT)

    def test_insert(self):
        """Should create a child span for insert"""
        with self._tracer.start_as_current_span("rootSpan"):
            self._collection.insert_one({
                "name": "testName",
                "value": "testValue"
            })
        self.validate_spans()

    def test_update(self):
        """Should create a child span for update"""
        with self._tracer.start_as_current_span("rootSpan"):
            self._collection.update_one({"name": "testName"},
                                        {"$set": {
                                            "value": "someOtherValue"
                                        }})
        self.validate_spans()

    def test_find(self):
        """Should create a child span for find"""
        with self._tracer.start_as_current_span("rootSpan"):
            self._collection.find_one()
        self.validate_spans()

    def test_delete(self):
        """Should create a child span for delete"""
        with self._tracer.start_as_current_span("rootSpan"):
            self._collection.delete_one({"name": "testName"})
        self.validate_spans()

    def test_uninstrument(self):
        # check that integration is working
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.memory_exporter.clear()
        self.assertEqual(len(spans), 1)

        # uninstrument and check not new spans are created
        PymongoInstrumentor().uninstrument()
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.memory_exporter.clear()
        self.assertEqual(len(spans), 0)

        # re-enable and check that it works again
        PymongoInstrumentor().instrument()
        self._collection.find_one()
        spans = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans), 1)