Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions lite_bootstrap/bootstraps/fastapi_bootstrap.py

This file was deleted.

25 changes: 25 additions & 0 deletions lite_bootstrap/bootstraps/fastapi_bootstrap/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dataclasses
import typing

import fastapi

from lite_bootstrap.bootstraps.base import BaseBootstrap
from lite_bootstrap.bootstraps.fastapi_bootstrap.opentelemetry_instrument import FastAPIOpenTelemetryInstrument
from lite_bootstrap.bootstraps.fastapi_bootstrap.sentry_instrument import FastAPISentryInstrument


__all__ = [
"FastAPIBootstrap",
"FastAPIOpenTelemetryInstrument",
"FastAPISentryInstrument",
]


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class FastAPIBootstrap(BaseBootstrap):
app: fastapi.FastAPI
instruments: typing.Sequence[FastAPIOpenTelemetryInstrument | FastAPISentryInstrument]

def __post_init__(self) -> None:
for one_instrument in self.instruments:
one_instrument.app = self.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import contextlib
import dataclasses

import fastapi

from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument


with contextlib.suppress(ImportError):
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor


@dataclasses.dataclass(kw_only=True)
class FastAPIOpenTelemetryInstrument(OpenTelemetryInstrument):
excluded_urls: list[str] = dataclasses.field(default_factory=list)
app: fastapi.FastAPI = dataclasses.field(init=False)

def bootstrap(self) -> None:
super().bootstrap()
FastAPIInstrumentor.instrument_app(
app=self.app,
tracer_provider=self.tracer_provider,
excluded_urls=",".join(self.excluded_urls),
)

def teardown(self) -> None:
FastAPIInstrumentor.uninstrument_app(self.app)
super().teardown()
19 changes: 19 additions & 0 deletions lite_bootstrap/bootstraps/fastapi_bootstrap/sentry_instrument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import contextlib
import dataclasses

import fastapi

from lite_bootstrap.instruments.sentry_instrument import SentryInstrument


with contextlib.suppress(ImportError):
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware


@dataclasses.dataclass(kw_only=True)
class FastAPISentryInstrument(SentryInstrument):
app: fastapi.FastAPI = dataclasses.field(init=False)

def bootstrap(self) -> None:
super().bootstrap()
self.app.add_middleware(SentryAsgiMiddleware) # type: ignore[arg-type]
18 changes: 10 additions & 8 deletions lite_bootstrap/instruments/opentelemetry_instrument.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import contextlib
import dataclasses
import typing

import pydantic
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined]
from opentelemetry.sdk import resources
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from lite_bootstrap.instruments.base import BaseInstrument


with contextlib.suppress(ImportError):
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined]
from opentelemetry.sdk import resources
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class InstrumentorWithParams:
instrumentor: BaseInstrumentor
Expand All @@ -24,7 +26,7 @@ class OpenTelemetryInstrument(BaseInstrument):
container_name: str | None = None
endpoint: str | None = None
namespace: str | None = None
insecure: bool = pydantic.Field(default=True)
insecure: bool = True
instrumentors: list[InstrumentorWithParams | BaseInstrumentor] = dataclasses.field(default_factory=list)

tracer_provider: TracerProvider = dataclasses.field(init=False)
Expand Down
12 changes: 7 additions & 5 deletions lite_bootstrap/instruments/sentry_instrument.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import contextlib
import dataclasses
import typing

import pydantic
import sentry_sdk
from sentry_sdk.integrations import Integration

from lite_bootstrap.instruments.base import BaseInstrument


with contextlib.suppress(ImportError):
import sentry_sdk
from sentry_sdk.integrations import Integration


@dataclasses.dataclass(kw_only=True, slots=True)
class SentryInstrument(BaseInstrument):
dsn: str | None = None
sample_rate: float = pydantic.Field(default=1.0, le=1.0, ge=0.0)
sample_rate: float = dataclasses.field(default=1.0)
traces_sample_rate: float | None = None
environment: str | None = None
max_breadcrumbs: int = 15
Expand Down