def ready(self): self.client = get_client() register_handlers(self.client) if self.client.config.instrument: instrument(self.client) else: self.client.logger.debug("Skipping instrumentation. INSTRUMENT is set to False.")
def _init_app(self) -> None: """ Initialize all the required middleware and other application infrastructure that will perform the necessary capture of the APM instrumentation artifacts :return: None """ if not self._client: self._client = make_client(config=self._client_config, client_cls=self._client_cls, **self._client_config) if not self._skip_init_exception_handler: self._setup_exception_manager() if self._client.config.instrument and self._client.config.enabled: instrument() try: from elasticapm.contrib.celery import register_instrumentation register_instrumentation(client=self._client) except ImportError: self._logger.debug( "Failed to setup instrumentation. " "Please install requirements for elasticapm.contrib.celery if instrumentation is required" ) pass if not self._skip_init_middleware: self._setup_request_handler(entity=self._app)
def test_psycopg2_composable_query_works(postgres_connection, elasticapm_client): """ Check that we parse queries that are psycopg2.sql.Composable correctly """ control.instrument() cursor = postgres_connection.cursor() query = sql.SQL( "SELECT * FROM {table} WHERE {row} LIKE 't%' ORDER BY {row} DESC" ).format( table=sql.Identifier('test'), row=sql.Identifier('name'), ) baked_query = query.as_string(cursor.__wrapped__) result = None try: elasticapm_client.begin_transaction("web.django") cursor.execute(query) result = cursor.fetchall() elasticapm_client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the spans for the other tests. assert [(2, 'two'), (3, 'three')] == result transactions = elasticapm_client.instrumentation_store.get_all() spans = transactions[0]['spans'] span = spans[0] assert span['name'] == 'SELECT FROM test' assert 'db' in span['context'] assert span['context']['db']['type'] == 'sql' assert span['context']['db']['statement'] == baked_query
def ready(self): self.client = get_client() register_handlers(self.client) if not self.client.config.disable_instrumentation: instrument(self.client) else: self.client.logger.debug( "Skipping instrumentation. DISABLE_INSTRUMENTATION is set.")
def ready(self): client = get_client() register_handlers(client) if 'SKIP_INSTRUMENT' not in os.environ: instrument(client) else: client.logger.debug( "Skipping instrumentation. SKIP_INSTRUMENT is set.")
def ready(self): self.client = get_client() if self.client.config.autoinsert_django_middleware: self.insert_middleware(django_settings) register_handlers(self.client) if self.client.config.instrument: instrument(self.client) else: self.client.logger.debug("Skipping instrumentation. INSTRUMENT is set to False.")
def test_psycopg2_tracing_outside_of_elasticapm_transaction(postgres_connection, elasticapm_client): control.instrument() cursor = postgres_connection.cursor() # check that the cursor is a proxy, even though we're not in an elasticapm # transaction assert isinstance(cursor, PGCursorProxy) cursor.execute('SELECT 1') transactions = elasticapm_client.instrumentation_store.get_all() assert transactions == []
def instrument(client): """ Auto-instruments code to get nice spans """ from elasticapm.instrumentation.control import instrument instrument() try: import celery # noqa F401 from elasticapm.contrib.celery import register_instrumentation register_instrumentation(client) except ImportError: client.logger.debug("Not instrumenting Celery, couldn't import")
def test_psycopg2_register_type(postgres_connection, elasticapm_client): import psycopg2.extras control.instrument() try: elasticapm_client.begin_transaction("web.django") new_type = psycopg2.extras.register_uuid(None, postgres_connection) elasticapm_client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the spans for the other tests. elasticapm_client.instrumentation_store.get_all() assert new_type is not None
def test_psycopg2_select_LIKE(postgres_connection): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ client = get_client() control.instrument() cursor = postgres_connection.cursor() try: client.begin_transaction("web.django") cursor.execute("SELECT * FROM test WHERE name LIKE 't%'") cursor.fetchall() client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. transactions = client.instrumentation_store.get_all() traces = transactions[0]['traces'] assert traces[0]['name'] == 'SELECT FROM test'
def test_psycopg2_register_json(postgres_connection, elasticapm_client): # register_json bypasses register_type, so we have to test unwrapping # separately import psycopg2.extras control.instrument() try: elasticapm_client.begin_transaction("web.django") # as arg new_type = psycopg2.extras.register_json(postgres_connection, loads=lambda x: x) assert new_type is not None # as kwarg new_type = psycopg2.extras.register_json( conn_or_curs=postgres_connection, loads=lambda x: x) assert new_type is not None elasticapm_client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. elasticapm_client.instrumentation_store.get_all()
def test_psycopg2_select_LIKE(postgres_connection, elasticapm_client): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ control.instrument() cursor = postgres_connection.cursor() query = "SELECT * FROM test WHERE name LIKE 't%'" try: elasticapm_client.begin_transaction("web.django") cursor.execute(query) cursor.fetchall() elasticapm_client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the spans for the other tests. transactions = elasticapm_client.instrumentation_store.get_all() spans = transactions[0]['spans'] span = spans[0] assert span['name'] == 'SELECT FROM test' assert 'db' in span['context'] assert span['context']['db']['type'] == 'sql' assert span['context']['db']['statement'] == query
# flake8: noqa from gevent import monkey monkey.patch_all() from psycogreen.gevent import patch_psycopg patch_psycopg() from elasticapm.instrumentation.control import instrument instrument() # noqa: E402,E702 import os import signal import gevent from gevent.pywsgi import WSGIServer from django.core.wsgi import get_wsgi_application server = WSGIServer(('0.0.0.0', int(os.environ.get('PORT', '8000'))), get_wsgi_application()) gevent.signal_handler(signal.SIGTERM, server.stop) server.serve_forever() gevent.get_hub().join()