Пример #1
0
def instrument_psycopg2(module):
    register_database_client(module, database_product='Postgres',
            quoting_style='single+dollar', explain_query='explain',
            explain_stmts=('select', 'insert', 'update', 'delete'),
            instance_info=instance_info)

    enable_datastore_instance_feature(module)

    wrap_object(module, 'connect', ConnectionFactory, (module,))
def instrument_psycopg2(module):
    register_database_client(module, database_product='Postgres',
            quoting_style='single', explain_query='explain',
            explain_stmts=('select', 'insert', 'update', 'delete'),
            instance_info=instance_info)

    enable_datastore_instance_feature(module)

    wrap_object(module, 'connect', ConnectionFactory, (module,))
def test_datastore_instance(name, system_hostname, db_hostname, product, port,
                            unix_socket, database_path,
                            expected_instance_metric, monkeypatch):

    monkeypatch.setattr('newrelic.common.system_info.gethostname',
                        lambda: system_hostname)

    class FakeModule():
        pass

    register_database_client(FakeModule, product)
    enable_datastore_instance_feature(FakeModule)

    port_path_or_id = port or database_path or unix_socket

    node = DatabaseNode(
        dbapi2_module=FakeModule,
        sql='',
        children=[],
        start_time=0,
        end_time=1,
        duration=1,
        exclusive=1,
        stack_trace=None,
        sql_format='obfuscated',
        connect_params=None,
        cursor_params=None,
        sql_parameters=None,
        execute_params=None,
        host=db_hostname,
        port_path_or_id=port_path_or_id,
        database_name=database_path,
        guid=None,
        agent_attributes={},
        user_attributes={},
    )

    empty_stats = StatsEngine()
    transaction = current_transaction()
    unscoped_scope = ''

    # Check 'Datastore/instance' metric to confirm that:
    #   1. metric name is reported correctly
    #   2. metric scope is always unscoped

    for metric in node.time_metrics(empty_stats, transaction, None):
        if metric.name.startswith("Datastore/instance/"):
            assert metric.name == expected_instance_metric, name
            assert metric.scope == unscoped_scope
def instrument_mysqldb(module):
    register_database_client(module, database_product='MySQL',
            quoting_style='single+double', explain_query='explain',
            explain_stmts=('select',), instance_info=instance_info)

    enable_datastore_instance_feature(module)

    wrap_object(module, 'connect', ConnectionFactory, (module,))

    # The connect() function is actually aliased with Connect() and
    # Connection, the later actually being the Connection type object.
    # Instrument Connect(), but don't instrument Connection in case that
    # interferes with direct type usage. If people are using the
    # Connection object directly, they should really be using connect().

    if hasattr(module, 'Connect'):
        wrap_object(module, 'Connect', ConnectionFactory, (module,))
Пример #5
0
def instrument_mysqldb(module):
    register_database_client(module,
                             database_product='MySQL',
                             quoting_style='single+double',
                             explain_query='explain',
                             explain_stmts=('select', ),
                             instance_info=instance_info)

    enable_datastore_instance_feature(module)

    wrap_object(module, 'connect', ConnectionFactory, (module, ))

    # The connect() function is actually aliased with Connect() and
    # Connection, the later actually being the Connection type object.
    # Instrument Connect(), but don't instrument Connection in case that
    # interferes with direct type usage. If people are using the
    # Connection object directly, they should really be using connect().

    if hasattr(module, 'Connect'):
        wrap_object(module, 'Connect', ConnectionFactory, (module, ))
Пример #6
0
            host, port = addr

        return (host, port, getattr(con_params, "database", None))

    @classmethod
    def instance_info(cls, args, kwargs):
        return cls._instance_info(*args, **kwargs)


register_database_client(
    PostgresApi,
    "Postgres",
    quoting_style="single+dollar",
    instance_info=PostgresApi.instance_info,
)
enable_datastore_instance_feature(PostgresApi)


class ProtocolProxy(ObjectProxy):
    async def bind_execute(self, state, *args, **kwargs):
        with DatabaseTrace(
                state.query,
                dbapi2_module=PostgresApi,
                connect_params=getattr(self, "_nr_connect_params", None),
        ):
            return await self.__wrapped__.bind_execute(state, *args, **kwargs)

    async def bind_execute_many(self, state, *args, **kwargs):
        with DatabaseTrace(
                state.query,
                dbapi2_module=PostgresApi,