Skip to content

Commit

Permalink
♻️ Add LogFire (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored Nov 15, 2024
1 parent a9ef20b commit fb4ec6c
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ jobs:
POSTGRES_PASSWORD: "${{ secrets.POSTGRES_PASSWORD }}"
EMAILABLE_KEY: "${{ secrets.EMAILABLE_KEY }}"
BUILDER_API_KEY: "${{ secrets.BUILDER_API_KEY }}"
LOGFIRE_BACKEND_TOKEN: "${{ secrets.LOGFIRE_BACKEND_TOKEN }}"
- name: Update Knative Service image
run: uv run typer --func deploy_cloud app.docker_builder run api $IMAGE_URL --min-scale 1
working-directory: backend
Expand Down Expand Up @@ -177,6 +178,7 @@ jobs:
CLOUDFLARE_ACCOUNT_ID: "${{ vars.CLOUDFLARE_ACCOUNT_ID }}"
CLOUDFLARE_ZONE_ID: "${{ vars.CLOUDFLARE_ZONE_ID }}"
CLOUDFLARE_API_TOKEN_SSL: "${{ secrets.CLOUDFLARE_API_TOKEN_SSL }}"
LOGFIRE_BACKEND_TOKEN: "${{ secrets.LOGFIRE_BACKEND_TOKEN }}"
# https://github.com/marketplace/actions/alls-green#why
alls-green-deploy-backend: # This job does nothing and is only used for the branch protection
if: always()
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
CLOUDFLARE_ACCOUNT_ID: "${{ vars.CLOUDFLARE_ACCOUNT_ID }}"
CLOUDFLARE_ZONE_ID: "${{ vars.CLOUDFLARE_ZONE_ID }}"
CLOUDFLARE_API_TOKEN_SSL: "${{ secrets.CLOUDFLARE_API_TOKEN_SSL }}"
LOGFIRE_BUILDER_TOKEN: "${{ secrets.LOGFIRE_BUILDER_TOKEN }}"

# Secrets for the builder
POSTGRES_PASSWORD: "${{ secrets.POSTGRES_PASSWORD }}"
Expand Down
4 changes: 4 additions & 0 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
HttpUrl,
PlainSerializer,
PostgresDsn,
SecretStr,
computed_field,
model_validator,
)
Expand Down Expand Up @@ -130,6 +131,8 @@ def all_cors_origins(self) -> list[str]:
RESERVED_APP_NAMES: Annotated[list[str] | str, BeforeValidator(parse_list_or_str)]
SENTRY_DSN: HttpUrl | None = None

LOGFIRE_BACKEND_TOKEN: SecretStr | None = None

REDIS_SERVER: str
REDIS_PORT: int = 6379
REDIS_PASSWORD: str | None = None
Expand Down Expand Up @@ -197,6 +200,7 @@ class BuilderSettings(SettingsEnv):
ECR_REGISTRY_URL: str
AWS_REGION: str
BUILDER_SENTRY_DSN: HttpUrl | None = None
LOGFIRE_BUILDER_TOKEN: SecretStr | None = None


class DepotSettings(SettingsEnv):
Expand Down
13 changes: 13 additions & 0 deletions backend/app/docker_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Any

import boto3
import logfire
import sentry_sdk
from asyncer import syncify
from botocore.exceptions import ClientError, NoCredentialsError
Expand All @@ -36,6 +37,7 @@
DepotSettings,
MainSettings,
)
from app.core.db import engine
from app.depot_py.depot.build import v1 as depot_build
from app.models import (
App,
Expand All @@ -47,6 +49,7 @@
)

builder_settings = BuilderSettings.get_settings()
common_settings = CommonSettings.get_settings()

# aws vars
aws_region = builder_settings.AWS_REGION
Expand All @@ -73,6 +76,16 @@
# FastAPI app
app = FastAPI()

if common_settings.ENVIRONMENT != "local" and builder_settings.LOGFIRE_BUILDER_TOKEN:
logfire.configure(
token=builder_settings.LOGFIRE_BUILDER_TOKEN.get_secret_value(),
environment=common_settings.ENVIRONMENT,
)
logfire.instrument_fastapi(app)
logfire.instrument_httpx()
logfire.instrument_sqlalchemy(engine=engine)
logfire.instrument_redis()


def create_namespace_by_team(namespace: str) -> None:
api_instance = get_kubernetes_client_core_v1()
Expand Down
18 changes: 16 additions & 2 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logfire
import sentry_sdk
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
Expand All @@ -6,6 +7,7 @@

from app.api.main import api_router
from app.core.config import CommonSettings, MainSettings
from app.core.db import engine
from app.core.exceptions import OAuth2Exception


Expand All @@ -14,20 +16,32 @@ def custom_generate_unique_id(route: APIRoute) -> str:


settings = MainSettings.get_settings()
common_settings = CommonSettings.get_settings()

if settings.SENTRY_DSN and CommonSettings.get_settings().ENVIRONMENT != "local":
if settings.SENTRY_DSN and common_settings.ENVIRONMENT != "local":
sentry_sdk.init(
dsn=str(settings.SENTRY_DSN),
enable_tracing=True,
environment=CommonSettings.get_settings().ENVIRONMENT,
environment=common_settings.ENVIRONMENT,
)


app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
generate_unique_id_function=custom_generate_unique_id,
)

if common_settings.ENVIRONMENT != "local" and settings.LOGFIRE_BACKEND_TOKEN:
logfire.configure(
token=settings.LOGFIRE_BACKEND_TOKEN.get_secret_value(),
environment=common_settings.ENVIRONMENT,
)
logfire.instrument_fastapi(app)
logfire.instrument_httpx()
logfire.instrument_sqlalchemy(engine=engine)
logfire.instrument_redis()


@app.exception_handler(OAuth2Exception)
async def oauth2_exception_handler(
Expand Down
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies = [
"betterproto>=2.0.0b7",
"asyncer>=0.0.8",
"cloudflare>=3.1.0",
"logfire[fastapi,httpx,redis,sqlalchemy]>=2.3.0",
]

[tool.uv]
Expand Down
Loading

0 comments on commit fb4ec6c

Please sign in to comment.