Skip to content

Commit 468ef36

Browse files
authored
Merge pull request #6 from modern-python/refactor
change library structure
2 parents 3cebe41 + 5f579eb commit 468ef36

File tree

6 files changed

+89
-64
lines changed

6 files changed

+89
-64
lines changed

lite_bootstrap/bootstraps/fastapi_bootstrap.py

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import dataclasses
2+
import typing
3+
4+
import fastapi
5+
6+
from lite_bootstrap.bootstraps.base import BaseBootstrap
7+
from lite_bootstrap.bootstraps.fastapi_bootstrap.opentelemetry_instrument import FastAPIOpenTelemetryInstrument
8+
from lite_bootstrap.bootstraps.fastapi_bootstrap.sentry_instrument import FastAPISentryInstrument
9+
10+
11+
__all__ = [
12+
"FastAPIBootstrap",
13+
"FastAPIOpenTelemetryInstrument",
14+
"FastAPISentryInstrument",
15+
]
16+
17+
18+
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
19+
class FastAPIBootstrap(BaseBootstrap):
20+
app: fastapi.FastAPI
21+
instruments: typing.Sequence[FastAPIOpenTelemetryInstrument | FastAPISentryInstrument]
22+
23+
def __post_init__(self) -> None:
24+
for one_instrument in self.instruments:
25+
one_instrument.app = self.app
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import contextlib
2+
import dataclasses
3+
4+
import fastapi
5+
6+
from lite_bootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrument
7+
8+
9+
with contextlib.suppress(ImportError):
10+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
11+
12+
13+
@dataclasses.dataclass(kw_only=True)
14+
class FastAPIOpenTelemetryInstrument(OpenTelemetryInstrument):
15+
excluded_urls: list[str] = dataclasses.field(default_factory=list)
16+
app: fastapi.FastAPI = dataclasses.field(init=False)
17+
18+
def bootstrap(self) -> None:
19+
super().bootstrap()
20+
FastAPIInstrumentor.instrument_app(
21+
app=self.app,
22+
tracer_provider=self.tracer_provider,
23+
excluded_urls=",".join(self.excluded_urls),
24+
)
25+
26+
def teardown(self) -> None:
27+
FastAPIInstrumentor.uninstrument_app(self.app)
28+
super().teardown()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import contextlib
2+
import dataclasses
3+
4+
import fastapi
5+
6+
from lite_bootstrap.instruments.sentry_instrument import SentryInstrument
7+
8+
9+
with contextlib.suppress(ImportError):
10+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
11+
12+
13+
@dataclasses.dataclass(kw_only=True)
14+
class FastAPISentryInstrument(SentryInstrument):
15+
app: fastapi.FastAPI = dataclasses.field(init=False)
16+
17+
def bootstrap(self) -> None:
18+
super().bootstrap()
19+
self.app.add_middleware(SentryAsgiMiddleware) # type: ignore[arg-type]

lite_bootstrap/instruments/opentelemetry_instrument.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import contextlib
12
import dataclasses
23
import typing
34

4-
import pydantic
5-
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
6-
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined]
7-
from opentelemetry.sdk import resources
8-
from opentelemetry.sdk.trace import TracerProvider
9-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
10-
115
from lite_bootstrap.instruments.base import BaseInstrument
126

137

8+
with contextlib.suppress(ImportError):
9+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
10+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined]
11+
from opentelemetry.sdk import resources
12+
from opentelemetry.sdk.trace import TracerProvider
13+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
14+
15+
1416
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
1517
class InstrumentorWithParams:
1618
instrumentor: BaseInstrumentor
@@ -24,7 +26,7 @@ class OpenTelemetryInstrument(BaseInstrument):
2426
container_name: str | None = None
2527
endpoint: str | None = None
2628
namespace: str | None = None
27-
insecure: bool = pydantic.Field(default=True)
29+
insecure: bool = True
2830
instrumentors: list[InstrumentorWithParams | BaseInstrumentor] = dataclasses.field(default_factory=list)
2931

3032
tracer_provider: TracerProvider = dataclasses.field(init=False)

lite_bootstrap/instruments/sentry_instrument.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import contextlib
12
import dataclasses
23
import typing
34

4-
import pydantic
5-
import sentry_sdk
6-
from sentry_sdk.integrations import Integration
7-
85
from lite_bootstrap.instruments.base import BaseInstrument
96

107

8+
with contextlib.suppress(ImportError):
9+
import sentry_sdk
10+
from sentry_sdk.integrations import Integration
11+
12+
1113
@dataclasses.dataclass(kw_only=True, slots=True)
1214
class SentryInstrument(BaseInstrument):
1315
dsn: str | None = None
14-
sample_rate: float = pydantic.Field(default=1.0, le=1.0, ge=0.0)
16+
sample_rate: float = dataclasses.field(default=1.0)
1517
traces_sample_rate: float | None = None
1618
environment: str | None = None
1719
max_breadcrumbs: int = 15

0 commit comments

Comments
 (0)