Skip to content

Commit

Permalink
Add logfire entry
Browse files Browse the repository at this point in the history
  • Loading branch information
devdupont committed May 18, 2024
1 parent 7f379a5 commit 1870118
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Start from the official Python 3.11 container
FROM python:3.12.2
# Start from the official Python 3.12 container
FROM python:3.12.3

# Expose the default application port
EXPOSE 8080
Expand All @@ -12,6 +12,7 @@ RUN useradd -m -r user && chown user /home/api

# Install the required Python packages
COPY pyproject.toml .
COPY main.py .
COPY ./account ./account
RUN pip install -U pip
RUN pip install -U . --no-cache-dir --compile
Expand All @@ -20,4 +21,4 @@ RUN pip install -U . --no-cache-dir --compile
USER user

# Run the application
CMD ["uvicorn", "account.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "4", "--lifespan", "on"]
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "4", "--lifespan", "on"]
8 changes: 5 additions & 3 deletions account/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from contextlib import asynccontextmanager

import logfire
import rollbar
from rollbar.contrib.fastapi import ReporterMiddleware
from fastapi import FastAPI
Expand All @@ -28,13 +29,14 @@


@asynccontextmanager
@logfire.no_auto_trace
async def lifespan(app: FastAPI): # type: ignore
"""Initialize application services."""
# Init Database
client = AsyncIOMotorClient(CONFIG.mongo_uri.strip('"')) # type: ignore[var-annotated]
app.db = client[CONFIG.database] # type: ignore[attr-defined]
client = AsyncIOMotorClient(CONFIG.mongo_uri.strip('"'))
app.db = client[CONFIG.database]
documents = [Addon, Plan, TokenUsage, User]
await init_beanie(app.db, document_models=documents) # type: ignore[arg-type,attr-defined]
await init_beanie(app.db, document_models=documents)
print("Startup complete")
yield
print("Shutdown complete")
Expand Down
2 changes: 1 addition & 1 deletion account/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _gen(self) -> None:
@classmethod
async def new(cls, name: str = "Token", type: str = "app") -> "UserToken":
"""Generate a new unique token."""
token = cls(_id=ObjectId(), name=name, type=type, value="") # type: ignore[arg-type]
token = cls(_id=ObjectId(), name=name, type=type, value="")
await token.refresh()
return token

Expand Down
2 changes: 1 addition & 1 deletion account/routes/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter
from account.config import CONFIG
from . import stripe, token, user
from . import token, user # , stripe

router = APIRouter(prefix=f"/{CONFIG.admin_root}", include_in_schema=False)

Expand Down
4 changes: 2 additions & 2 deletions account/routes/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ async def stripe_fulfill(
) -> Response:
"""Stripe event handler."""
try:
event = get_event(await request.body(), stripe_signature) # type: ignore
event = get_event(await request.body(), stripe_signature)
except (ValueError, SignatureVerificationError) as exc:
raise HTTPException(400) from exc
if handler := _EVENTS.get(event.type):
if await handler(event.data.object): # type: ignore
if await handler(event.data.object):
return Response()
else:
print(f"Unhandled event type {event.type}")
Expand Down
14 changes: 14 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Test main with logfire tracing."""

import logfire
from opentelemetry.instrumentation.pymongo import PymongoInstrumentor


logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record="all"))
logfire.install_auto_tracing(modules=["account"])
PymongoInstrumentor().instrument(capture_statement=True)

from account.main import app # noqa: E402


logfire.instrument_fastapi(app)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"fastapi-mail==1.4.1",
"httpx==0.27.0",
"kewkew==0.1",
"logfire[fastapi,system-metrics,pymongo]==0.32.1",
"mailchimp3==3.0.21",
"python-decouple==3.8",
"rollbar==1.0.0",
Expand Down

0 comments on commit 1870118

Please sign in to comment.