Пример #1
0
    def test_contextmanager_connection(self):
        conn, tracer = self._get_conn_tracer()

        with conn as c:
            if MySQLdb.version_info < (2, 0):
                cursor = c
            else:
                cursor = c.cursor()
            rowcount = cursor.execute("SELECT 1")
            assert rowcount == 1
            rows = cursor.fetchall()
            assert len(rows) == 1
            spans = tracer.pop()
            assert len(spans) == 1

            span = spans[0]
            assert_is_measured(span)
            assert span.service == "mysql"
            assert span.name == "mysql.query"
            assert span.span_type == "sql"
            assert span.error == 0
            assert span.get_metric("out.port") == 3306
            assert_dict_issuperset(
                span.meta,
                {
                    "out.host": u"127.0.0.1",
                    "db.name": u"test",
                    "db.user": u"test",
                },
            )
Пример #2
0
    def test_simple_query_fetchll(self):
        with self.override_config("mysql", dict(trace_fetch_methods=True)):
            conn, tracer = self._get_conn_tracer()
            cursor = conn.cursor()
            cursor.execute("SELECT 1")
            rows = cursor.fetchall()
            assert len(rows) == 1
            spans = tracer.pop()
            assert len(spans) == 2

            span = spans[0]
            assert_is_measured(span)
            assert span.service == "mysql"
            assert span.name == "mysql.query"
            assert span.span_type == "sql"
            assert span.error == 0
            assert span.get_metric("out.port") == 3306
            assert_dict_issuperset(
                span.get_tags(),
                {
                    "out.host": u"127.0.0.1",
                    "db.name": u"test",
                    "db.user": u"test",
                },
            )

            assert spans[1].name == "mysql.query.fetchall"
Пример #3
0
    def test_simple_query_ot_fetchall(self):
        """OpenTracing version of test_simple_query."""
        with self.override_config("dbapi2", dict(trace_fetch_methods=True)):
            conn, tracer = self._get_conn_tracer()

            ot_tracer = init_tracer("mysql_svc", tracer)
            with ot_tracer.start_active_span("mysql_op"):
                cursor = conn.cursor()
                cursor.execute("SELECT 1")
                rows = cursor.fetchall()
                assert len(rows) == 1

            spans = tracer.pop()
            assert len(spans) == 3
            ot_span, dd_span, fetch_span = spans

            # confirm parenting
            assert ot_span.parent_id is None
            assert dd_span.parent_id == ot_span.span_id

            assert ot_span.service == "mysql_svc"
            assert ot_span.name == "mysql_op"

            assert_is_measured(dd_span)
            assert dd_span.service == "pymysql"
            assert dd_span.name == "pymysql.query"
            assert dd_span.span_type == "sql"
            assert dd_span.error == 0
            assert dd_span.get_metric("out.port") == MYSQL_CONFIG.get("port")
            meta = {}
            meta.update(self.DB_INFO)
            assert_dict_issuperset(dd_span.meta, meta)

            assert fetch_span.name == "pymysql.query.fetchall"
Пример #4
0
    def test_simple_query_with_positional_args(self):
        conn, tracer = self._get_conn_tracer_with_positional_args()

        cursor = conn.cursor()
        cursor.execute("SELECT 1")
        rows = cursor.fetchall()
        assert len(rows) == 1
        spans = tracer.pop()
        assert len(spans) == 1

        span = spans[0]
        assert_is_measured(span)
        assert span.service == "mysql"
        assert span.name == "mysql.query"
        assert span.span_type == "sql"
        assert span.error == 0
        assert span.get_metric("out.port") == 3306
        assert_dict_issuperset(
            span.meta,
            {
                "out.host": u"127.0.0.1",
                "db.name": u"test",
                "db.user": u"test",
            },
        )
Пример #5
0
    def test_patch_unpatch(self):
        unpatch()
        # assert we start unpatched
        conn = mysql.connector.connect(**MYSQL_CONFIG)
        assert not Pin.get_from(conn)
        conn.close()

        patch()
        try:
            conn = mysql.connector.connect(**MYSQL_CONFIG)
            pin = Pin.get_from(conn)
            assert pin
            pin.clone(service="pin-svc", tracer=self.tracer).onto(conn)
            assert conn.is_connected()

            cursor = conn.cursor()
            cursor.execute("SELECT 1")
            rows = cursor.fetchall()
            assert len(rows) == 1
            spans = self.pop_spans()
            assert len(spans) == 1

            span = spans[0]
            assert span.service == "pin-svc"
            assert span.name == "mysql.query"
            assert span.span_type == "sql"
            assert span.error == 0
            assert span.get_metric("out.port") == 3306
            assert_dict_issuperset(
                span.get_tags(),
                {
                    "out.host": u"127.0.0.1",
                    "db.name": u"test",
                    "db.user": u"test",
                },
            )
            assert span.get_tag("sql.query") is None

        finally:
            unpatch()

            # assert we finish unpatched
            conn = mysql.connector.connect(**MYSQL_CONFIG)
            assert not Pin.get_from(conn)
            conn.close()

        patch()
Пример #6
0
    def test_simple_cache_clear(self):
        self.cache.clear()
        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        span = spans[0]
        assert_is_measured(span)
        self.assertEqual(span.service, self.SERVICE)
        self.assertEqual(span.resource, "clear")
        self.assertEqual(span.name, "flask_cache.cmd")
        self.assertEqual(span.span_type, "cache")
        self.assertEqual(span.error, 0)

        expected_meta = {
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Пример #7
0
    def test_query_proc(self):
        conn, tracer = self._get_conn_tracer()

        # create a procedure
        tracer.enabled = False
        cursor = conn.cursor()
        cursor.execute("DROP PROCEDURE IF EXISTS sp_sum")
        cursor.execute("""
            CREATE PROCEDURE sp_sum (IN p1 INTEGER, IN p2 INTEGER, OUT p3 INTEGER)
            BEGIN
                SET p3 := p1 + p2;
            END;""")

        tracer.enabled = True
        proc = "sp_sum"
        data = (40, 2, None)
        output = cursor.callproc(proc, data)
        assert len(output) == 3
        # resulted p3 isn't stored on output[2], we need to fetch it with select
        # http://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
        cursor.execute("SELECT @_sp_sum_2;")
        assert cursor.fetchone()[0] == 42

        spans = tracer.pop()
        assert spans, spans

        # number of spans depends on MySQL implementation details,
        # typically, internal calls to execute, but at least we
        # can expect the next to the last closed span to be our proc.
        span = spans[-2]
        assert_is_measured(span)
        assert span.service == "mysql"
        assert span.name == "mysql.query"
        assert span.span_type == "sql"
        assert span.error == 0
        assert span.get_metric("out.port") == 3306
        assert_dict_issuperset(
            span.meta,
            {
                "out.host": u"127.0.0.1",
                "db.name": u"test",
                "db.user": u"test",
            },
        )
        assert span.get_tag("sql.query") is None
Пример #8
0
    def test_simple_cache_delete_many(self):
        self.cache.delete_many("complex_operation", "another_complex_op")
        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        span = spans[0]
        assert_is_measured(span)
        self.assertEqual(span.service, self.SERVICE)
        self.assertEqual(span.resource, "delete_many")
        self.assertEqual(span.name, "flask_cache.cmd")
        self.assertEqual(span.span_type, "cache")
        self.assertEqual(span.error, 0)

        expected_meta = {
            "flask_cache.key": "['complex_operation', 'another_complex_op']",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Пример #9
0
    def test_simple_cache_add(self):
        self.cache.add(u"á_complex_number", 50)
        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        span = spans[0]
        assert_is_measured(span)
        self.assertEqual(span.service, self.SERVICE)
        self.assertEqual(span.resource, "add")
        self.assertEqual(span.name, "flask_cache.cmd")
        self.assertEqual(span.span_type, "cache")
        self.assertEqual(span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_number",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.get_tags(), expected_meta)
Пример #10
0
    def test_simple_cache_get_many(self):
        self.cache.get_many("first_complex_op", "second_complex_op")
        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        span = spans[0]
        assert_is_measured(span)
        self.assertEqual(span.service, self.SERVICE)
        self.assertEqual(span.resource, "get_many")
        self.assertEqual(span.name, "flask_cache.cmd")
        self.assertEqual(span.span_type, "cache")
        self.assertEqual(span.error, 0)

        expected_meta = {
            "flask_cache.key": "['first_complex_op', 'second_complex_op']",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Пример #11
0
    def test_simple_cache_set(self):
        self.cache.set(u"á_complex_operation", u"with_á_value\nin two lines")
        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        span = spans[0]
        assert_is_measured(span)
        self.assertEqual(span.service, self.SERVICE)
        self.assertEqual(span.resource, "set")
        self.assertEqual(span.name, "flask_cache.cmd")
        self.assertEqual(span.span_type, "cache")
        self.assertEqual(span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_operation",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(span.meta, expected_meta)
Пример #12
0
    def test_query_proc(self):
        conn, tracer = self._get_conn_tracer()

        # create a procedure
        tracer.enabled = False
        cursor = conn.cursor()
        cursor.execute("DROP PROCEDURE IF EXISTS sp_sum")
        cursor.execute("""
            CREATE PROCEDURE sp_sum (IN p1 INTEGER, IN p2 INTEGER, OUT p3 INTEGER)
            BEGIN
                SET p3 := p1 + p2;
            END;""")

        tracer.enabled = True
        proc = "sp_sum"
        data = (40, 2, None)

        # spans[len(spans) - 2]
        cursor.callproc(proc, data)

        # spans[len(spans) - 1]
        cursor.execute("""
                       SELECT @_sp_sum_0, @_sp_sum_1, @_sp_sum_2
                       """)
        output = cursor.fetchone()
        assert len(output) == 3
        assert output[2] == 42

        spans = tracer.pop()
        assert spans, spans

        # number of spans depends on PyMySQL implementation details,
        # typically, internal calls to execute, but at least we
        # can expect the last closed span to be our proc.
        span = spans[len(spans) - 2]
        assert_is_measured(span)
        assert span.service == "pymysql"
        assert span.name == "pymysql.query"
        assert span.span_type == "sql"
        assert span.error == 0
        assert span.get_metric("out.port") == MYSQL_CONFIG.get("port")
        meta = {}
        meta.update(self.DB_INFO)
        assert_dict_issuperset(span.meta, meta)
Пример #13
0
    def test_query_proc(self):
        conn, tracer = self._get_conn_tracer()

        # create a procedure
        tracer.enabled = False
        cursor = conn.cursor()
        cursor.execute("DROP PROCEDURE IF EXISTS sp_sum")
        cursor.execute(
            """
            CREATE PROCEDURE sp_sum (IN p1 INTEGER, IN p2 INTEGER, OUT p3 INTEGER)
            BEGIN
                SET p3 := p1 + p2;
            END;"""
        )

        tracer.enabled = True
        proc = "sp_sum"
        data = (40, 2, None)
        output = cursor.callproc(proc, data)
        assert len(output) == 3
        assert output[2] == 42

        spans = tracer.pop()
        assert spans, spans

        # number of spans depends on MySQL implementation details,
        # typically, internal calls to execute, but at least we
        # can expect the last closed span to be our proc.
        span = spans[len(spans) - 1]
        assert_is_measured(span)
        assert span.service == "mysql"
        assert span.name == "mysql.query"
        assert span.span_type == "sql"
        assert span.error == 0
        assert span.get_metric("out.port") == 3306
        assert_dict_issuperset(
            span.meta,
            {
                "out.host": u"127.0.0.1",
                "db.name": u"test",
                "db.user": u"test",
            },
        )
        assert span.get_tag("sql.query") is None
Пример #14
0
    def test_patch_unpatch(self):
        unpatch()
        # assert we start unpatched
        conn = pymysql.connect(**MYSQL_CONFIG)
        assert not Pin.get_from(conn)
        conn.close()

        patch()
        try:
            conn = pymysql.connect(**MYSQL_CONFIG)
            pin = Pin.get_from(conn)
            assert pin
            pin.clone(tracer=self.tracer).onto(conn)
            assert not conn._closed

            cursor = conn.cursor()
            cursor.execute("SELECT 1")
            rows = cursor.fetchall()
            assert len(rows) == 1
            spans = self.pop_spans()
            assert len(spans) == 1

            span = spans[0]
            assert span.service == "pymysql"
            assert span.name == "pymysql.query"
            assert span.span_type == "sql"
            assert span.error == 0
            assert span.get_metric("out.port") == MYSQL_CONFIG.get("port")

            meta = {}
            meta.update(self.DB_INFO)
            assert_dict_issuperset(span.meta, meta)
        finally:
            unpatch()

            # assert we finish unpatched
            conn = pymysql.connect(**MYSQL_CONFIG)
            assert not Pin.get_from(conn)
            conn.close()

        patch()
Пример #15
0
    def test_simple_query_ot_fetchall(self):
        """OpenTracing version of test_simple_query."""
        with self.override_config("mysql", dict(trace_fetch_methods=True)):
            conn, tracer = self._get_conn_tracer()

            ot_tracer = init_tracer("mysql_svc", tracer)

            with ot_tracer.start_active_span("mysql_op"):
                cursor = conn.cursor()
                cursor.execute("SELECT 1")
                rows = cursor.fetchall()
                assert len(rows) == 1

            spans = tracer.pop()
            assert len(spans) == 3

            ot_span, dd_span, fetch_span = spans

            # confirm parenting
            assert ot_span.parent_id is None
            assert dd_span.parent_id == ot_span.span_id

            assert ot_span.service == "mysql_svc"
            assert ot_span.name == "mysql_op"

            assert_is_measured(dd_span)
            assert dd_span.service == "mysql"
            assert dd_span.name == "mysql.query"
            assert dd_span.span_type == "sql"
            assert dd_span.error == 0
            assert dd_span.get_metric("out.port") == 3306
            assert_dict_issuperset(
                dd_span.get_tags(),
                {
                    "out.host": u"127.0.0.1",
                    "db.name": u"test",
                    "db.user": u"test",
                },
            )

            assert fetch_span.name == "mysql.query.fetchall"
Пример #16
0
def test_simple_query(connection, tracer):
    cursor = connection.cursor()
    cursor.execute("SELECT 1")
    rows = cursor.fetchall()
    assert len(rows) == 1
    spans = tracer.pop()
    assert len(spans) == 1
    span = spans[0]
    assert_is_measured(span)
    assert span.service == "mariadb"
    assert span.name == "mariadb.query"
    assert span.span_type == "sql"
    assert span.error == 0
    assert span.get_metric("out.port") == 3306

    assert_dict_issuperset(
        span.meta,
        {
            "out.host": u"127.0.0.1",
            "db.name": u"test",
            "db.user": u"test",
        },
    )
Пример #17
0
    def test_simple_query(self):
        conn, tracer = self._get_conn_tracer()

        cursor = conn.cursor()

        # PyMySQL returns back the rowcount instead of a cursor
        rowcount = cursor.execute("SELECT 1")
        assert rowcount == 1

        rows = cursor.fetchall()
        assert len(rows) == 1
        spans = tracer.pop()
        assert len(spans) == 1

        span = spans[0]
        assert_is_measured(span)
        assert span.service == "pymysql"
        assert span.name == "pymysql.query"
        assert span.span_type == "sql"
        assert span.error == 0
        assert span.get_metric("out.port") == MYSQL_CONFIG.get("port")
        meta = {}
        meta.update(self.DB_INFO)
        assert_dict_issuperset(span.meta, meta)
Пример #18
0
    def test_simple_cache_get_ot(self):
        """OpenTracing version of test_simple_cache_get."""
        ot_tracer = init_tracer("my_svc", self.tracer)

        # create the TracedCache instance for a Flask app
        Cache = get_traced_cache(self.tracer, service=self.SERVICE)
        app = Flask(__name__)
        cache = Cache(app, config={"CACHE_TYPE": "simple"})

        with ot_tracer.start_active_span("ot_span"):
            cache.get(u"á_complex_operation")

        spans = self.get_spans()
        self.assertEqual(len(spans), 2)
        ot_span, dd_span = spans

        # confirm the parenting
        self.assertIsNone(ot_span.parent_id)
        self.assertEqual(dd_span.parent_id, ot_span.span_id)

        self.assertEqual(ot_span.resource, "ot_span")
        self.assertEqual(ot_span.service, "my_svc")

        assert_is_measured(dd_span)
        self.assertEqual(dd_span.service, self.SERVICE)
        self.assertEqual(dd_span.resource, "get")
        self.assertEqual(dd_span.name, "flask_cache.cmd")
        self.assertEqual(dd_span.span_type, "cache")
        self.assertEqual(dd_span.error, 0)

        expected_meta = {
            "flask_cache.key": u"á_complex_operation",
            "flask_cache.backend": "simple",
        }

        assert_dict_issuperset(dd_span.meta, expected_meta)
Пример #19
0
    def test_simple_query_fetchall(self):
        with self.override_config("dbapi2", dict(trace_fetch_methods=True)):
            conn, tracer = self._get_conn_tracer()

            cursor = conn.cursor()
            cursor.execute("SELECT 1")
            rows = cursor.fetchall()
            assert len(rows) == 1
            spans = tracer.pop()
            assert len(spans) == 2

            span = spans[0]
            assert_is_measured(span)
            assert span.service == "pymysql"
            assert span.name == "pymysql.query"
            assert span.span_type == "sql"
            assert span.error == 0
            assert span.get_metric("out.port") == MYSQL_CONFIG.get("port")
            meta = {}
            meta.update(self.DB_INFO)
            assert_dict_issuperset(span.meta, meta)

            fetch_span = spans[1]
            assert fetch_span.name == "pymysql.query.fetchall"