Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiating AsyncPGInstrumentor after .instrument has been called causes tracing to fail #1790

Closed
marcuslimdw opened this issue May 9, 2023 · 0 comments · Fixed by #1791
Labels
bug Something isn't working

Comments

@marcuslimdw
Copy link
Contributor

Describe your environment

Python 3.11 on a M2 Mac.

Steps to reproduce

Run the following code (with python -m asyncio to allow top-level async/await):

import asyncpg

from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.trace import set_tracer_provider

provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
set_tracer_provider(provider)

dsn = "postgres://postgres:password@localhost:54320/postgres"

AsyncPGInstrumentor().instrument()
AsyncPGInstrumentor()

connection = await asyncpg.connect(dsn)
await connection.execute("SELECT 1")

What is the expected behavior?

The SQL query runs successfully and a span is exported to the console.

What is the actual behavior?
What did you see instead?

AttributeError: 'NoneType' object has no attribute 'start_as_current_span'

Additional context

Each instantiation of AsyncPGInstrumentor runs __init__, which sets self._tracer to None. However, BaseInstrumentor overrides __new__ to implement the singleton pattern, so only one instance of AsyncPGInstrumentor is ever created. Instantiating AsyncPGInstrumentor after instrument has been called (which sets self._tracer) therefore sets self._tracer back to None, which is a state inconsistent with _is_instrumented_by_opentelemetry (which will still be True).

A simple solution is to remove the line self._tracer = None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant