pydantic / logfire

Uncomplicated Observability for Python and beyond! 🪵🔥
https://docs.pydantic.dev/logfire/
MIT License
1.62k stars 46 forks source link

Interference between Logfire and Sentry. Logfire 'swallowing' loguru error events #228

Closed bllchmbrs closed 3 weeks ago

bllchmbrs commented 3 weeks ago

Description

Reproduction:

from dotenv import load_dotenv
load_dotenv()

import sentry_sdk
import os
from loguru import logger
from sentry_sdk.integrations.loguru import LoguruIntegration
sentry_sdk.init(
    dsn="https://3ebebc9edb0d46634a41d36ac3f84745@o4506402211037184.ingest.sentry.io/4506402212413440",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    environment=os.environ.get("ENV", "development"),
    traces_sample_rate=0.2,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=0.2,
    integrations=[
        LoguruIntegration(),
    ],
)

logger.error("This is a test")
# SHOWS UP IN SENTRY

import os
print([k for k in os.environ.keys() if k.startswith("LOGFIRE")])
import logfire
logfire.configure()

logger.configure(handlers=[logfire.loguru_handler()])

logger.error("This is a test with Logfire and Sentry")
# DOES NOT SHOW UP IN SENTRY, ONLY LOGFIRE

See the comments, after I configure logfire + sentry, logfire swallows all events.

Python, Logfire & OS Versions, related packages (not required)

logfire version - '0.37.0'
python 3.11.5
alexmojaki commented 3 weeks ago

Sentry's LoguruIntegration contains code like logger.add(LoguruEventHandler(.... logger.configure overrides those handlers. Either call logger.configure first, or call logger.add(logfire.loguru_handler()).

bllchmbrs commented 3 weeks ago

thanks - I was actually just about to get here as I was reading the code in more detail - thanks!!!

bllchmbrs commented 3 weeks ago

For posterity, logger.add(logfire.loguru_handler()) doesn't work. will update with what does.

bllchmbrs commented 3 weeks ago

Best practice: load sentry after the case. That causes events to fire in both locations.

alexmojaki commented 3 weeks ago

I think it should actually be logger.add(**logfire.loguru_handler())