Exemplo n.º 1
0
    def test_instrumentor_create_pool(self):
        AiopgInstrumentor().instrument()

        pool = async_call(aiopg.create_pool(database="test"))
        cnx = async_call(pool.acquire())
        cursor = async_call(cnx.cursor())

        query = "SELECT * FROM test"
        async_call(cursor.execute(query))

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
        span = spans_list[0]

        # Check version and name in span's instrumentation info
        self.check_span_instrumentation_info(
            span, opentelemetry.instrumentation.aiopg)

        # check that no spans are generated after uninstrument
        AiopgInstrumentor().uninstrument()

        pool = async_call(aiopg.create_pool(database="test"))
        cnx = async_call(pool.acquire())
        cursor = async_call(cnx.cursor())
        query = "SELECT * FROM test"
        cursor.execute(query)

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
    def test_custom_tracer_provider_instrument_connection(self):
        resource = resources.Resource.create(
            {"service.name": "db-test-service"}
        )
        result = self.create_tracer_provider(resource=resource)
        tracer_provider, exporter = result

        cnx = async_call(aiopg.connect(database="test"))

        cnx = AiopgInstrumentor().instrument_connection(
            cnx, tracer_provider=tracer_provider
        )

        cursor = async_call(cnx.cursor())
        query = "SELECT * FROM test"
        async_call(cursor.execute(query))

        spans_list = exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
        span = spans_list[0]

        self.assertEqual(
            span.resource.attributes["service.name"], "db-test-service"
        )
        self.assertIs(span.resource, resource)
Exemplo n.º 3
0
    def test_instrument_connection(self):
        cnx = async_call(aiopg.connect(database="test"))
        query = "SELECT * FROM test"
        cursor = async_call(cnx.cursor())
        async_call(cursor.execute(query))

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 0)

        cnx = AiopgInstrumentor().instrument_connection(cnx)
        cursor = async_call(cnx.cursor())
        async_call(cursor.execute(query))

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
Exemplo n.º 4
0
 def tearDownClass(cls):
     if cls._cursor:
         cls._cursor.close()
     if cls._connection:
         cls._connection.close()
     if cls._pool:
         cls._pool.close()
     AiopgInstrumentor().uninstrument()
 def setUp(self):
     super().setUp()
     self._tracer = self.tracer_provider.get_tracer(__name__)
     AiopgInstrumentor().instrument(tracer_provider=self.tracer_provider)
     self._connection = async_call(
         aiopg.connect(
             dbname=POSTGRES_DB_NAME,
             user=POSTGRES_USER,
             password=POSTGRES_PASSWORD,
             host=POSTGRES_HOST,
             port=POSTGRES_PORT,
         ))
     self._cursor = async_call(self._connection.cursor())
        async def _ctx_manager_connect():
            AiopgInstrumentor().instrument()

            async with aiopg.connect(database="test") as cnx:
                async with cnx.cursor() as cursor:
                    query = "SELECT * FROM test"
                    await cursor.execute(query)

                    spans_list = self.memory_exporter.get_finished_spans()
                    self.assertEqual(len(spans_list), 1)
                    span = spans_list[0]

                    # Check version and name in span's instrumentation info
                    self.check_span_instrumentation_info(
                        span, opentelemetry.instrumentation.aiopg)
Exemplo n.º 7
0
 def setUpClass(cls):
     super().setUpClass()
     cls._connection = None
     cls._cursor = None
     cls._tracer = cls.tracer_provider.get_tracer(__name__)
     AiopgInstrumentor().instrument(tracer_provider=cls.tracer_provider)
     cls._pool = async_call(
         aiopg.create_pool(
             dbname=POSTGRES_DB_NAME,
             user=POSTGRES_USER,
             password=POSTGRES_PASSWORD,
             host=POSTGRES_HOST,
             port=POSTGRES_PORT,
         ))
     cls._connection = async_call(cls._pool.acquire())
     cls._cursor = async_call(cls._connection.cursor())
 def setUp(self):
     super().setUp()
     self._tracer = self.tracer_provider.get_tracer(__name__)
     AiopgInstrumentor().instrument(tracer_provider=self.tracer_provider)
     self._dsn = (
         f"dbname='{POSTGRES_DB_NAME}' user='******' password='******'"
         f" host='{POSTGRES_HOST}' port='{POSTGRES_PORT}'")
     self._pool = async_call(
         aiopg.create_pool(
             dbname=POSTGRES_DB_NAME,
             user=POSTGRES_USER,
             password=POSTGRES_PASSWORD,
             host=POSTGRES_HOST,
             port=POSTGRES_PORT,
         ))
     self._connection = async_call(self._pool.acquire())
     self._cursor = async_call(self._connection.cursor())
Exemplo n.º 9
0
    def test_custom_tracer_provider_create_pool(self):
        resource = resources.Resource.create({})
        result = self.create_tracer_provider(resource=resource)
        tracer_provider, exporter = result

        AiopgInstrumentor().instrument(tracer_provider=tracer_provider)

        pool = async_call(aiopg.create_pool(database="test"))
        cnx = async_call(pool.acquire())
        cursor = async_call(cnx.cursor())
        query = "SELECT * FROM test"
        async_call(cursor.execute(query))

        spans_list = exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
        span = spans_list[0]

        self.assertIs(span.resource, resource)
Exemplo n.º 10
0
 def tearDown(self):
     super().tearDown()
     aiopg.connect = self.origin_aiopg_connect
     aiopg.create_pool = self.origin_aiopg_create_pool
     with self.disable_logging():
         AiopgInstrumentor().uninstrument()
 def tearDown(self):
     self._cursor.close()
     self._connection.close()
     AiopgInstrumentor().uninstrument()
     super().tearDown()
Exemplo n.º 12
0
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

trace_provider = TracerProvider(
    resource=Resource.create({SERVICE_NAME: "my-service"}))
trace.set_tracer_provider(trace_provider)
tracer = trace.get_tracer(__name__)

jaeger_exporter = JaegerExporter(agent_host_name="127.0.0.1", agent_port=5775)
span_processor = BatchSpanProcessor(jaeger_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

AiopgInstrumentor().instrument()

dsn = "dbname=test user=postgres password=postgres host=127.0.0.1"


async def init_pg(app):
    pool = await aiopg.create_pool(dsn)
    app["pool"] = pool


async def handle(request):
    async with request.app["pool"].acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name