File tree Expand file tree Collapse file tree 6 files changed +89
-64
lines changed
Expand file tree Collapse file tree 6 files changed +89
-64
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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]
Original file line number Diff line number Diff line change 1+ import contextlib
12import dataclasses
23import 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-
115from 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 )
1517class 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 )
Original file line number Diff line number Diff line change 1+ import contextlib
12import dataclasses
23import typing
34
4- import pydantic
5- import sentry_sdk
6- from sentry_sdk .integrations import Integration
7-
85from 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 )
1214class 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
You can’t perform that action at this time.
0 commit comments