Skip to content

Commit 9bc948d

Browse files
committed
Add docstring for public api
1 parent 3d5d2b1 commit 9bc948d

File tree

9 files changed

+164
-0
lines changed

9 files changed

+164
-0
lines changed

src/asgi_monitor/integrations/fastapi.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,33 @@
2727

2828
@dataclass
2929
class TracingConfig(CommonTracingConfig):
30+
"""Configuration class for the OpenTelemetry middleware.
31+
Consult the OpenTelemetry ASGI documentation for more info about the configuration options.
32+
https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html
33+
"""
34+
3035
exclude_urls_env_key: str = "FASTAPI"
36+
"""Key to use when checking whether a list of excluded urls is passed via ENV.
37+
OpenTelemetry supports excluding urls by passing an env in the format '{exclude_urls_env_key}_EXCLUDED_URLS'.
38+
"""
39+
3140
scope_span_details_extractor: Callable[[Any], tuple[str, dict[str, Any]]] = _get_default_span_details
41+
"""
42+
Callback which should return a string and a tuple, representing the desired default span name and a dictionary
43+
with any additional span attributes to set.
44+
"""
3245

3346

3447
def setup_tracing(app: FastAPI, config: TracingConfig) -> None:
48+
"""
49+
Set up tracing for a FastAPI application.
50+
The function adds a TracingMiddleware to the FastAPI application based on TracingConfig.
51+
52+
:param FastAPI app: The FastAPI application instance.
53+
:param TracingConfig config: The Open Telemetry config.
54+
:returns: None
55+
"""
56+
3557
app.add_middleware(TracingMiddleware, config=config)
3658

3759

@@ -43,6 +65,19 @@ def setup_metrics(
4365
include_trace_exemplar: bool,
4466
include_metrics_endpoint: bool,
4567
) -> None:
68+
"""
69+
Set up metrics for a FastAPI application.
70+
This function adds a MetricsMiddleware to the FastAPI application with the specified parameters.
71+
If include_metrics_endpoint is True, it also adds a route for "/metrics" that returns Prometheus default metrics.
72+
73+
:param FastAPI app: The FastAPI application instance.
74+
:param str app_name: The name of the FastAPI application.
75+
:param str metrics_prefix: The prefix to use for the metrics (default is "fastapi").
76+
:param bool include_trace_exemplar: Whether to include trace exemplars in the metrics.
77+
:param bool include_metrics_endpoint: Whether to include a /metrics endpoint.
78+
:returns: None
79+
"""
80+
4681
app.add_middleware(
4782
MetricsMiddleware,
4883
app_name=app_name,

src/asgi_monitor/integrations/starlette.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,21 @@ def _get_path(request: Request) -> tuple[str, bool]:
6969

7070
@dataclass
7171
class TracingConfig(CommonTracingConfig):
72+
"""Configuration class for the OpenTelemetry middleware.
73+
Consult the OpenTelemetry ASGI documentation for more info about the configuration options.
74+
https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html
75+
"""
76+
7277
exclude_urls_env_key: str = "STARLETTE"
78+
"""Key to use when checking whether a list of excluded urls is passed via ENV.
79+
OpenTelemetry supports excluding urls by passing an env in the format '{exclude_urls_env_key}_EXCLUDED_URLS'.
80+
"""
81+
7382
scope_span_details_extractor: Callable[[Any], tuple[str, dict[str, Any]]] = _get_default_span_details
83+
"""
84+
Callback which should return a string and a tuple, representing the desired default span name and a dictionary
85+
with any additional span attributes to set.
86+
"""
7487

7588

7689
class TracingMiddleware:
@@ -162,6 +175,15 @@ async def get_metrics(request: Request) -> Response:
162175

163176

164177
def setup_tracing(app: Starlette, config: TracingConfig) -> None:
178+
"""
179+
Set up tracing for a Starlette application.
180+
The function adds a TracingMiddleware to the Starlette application based on TracingConfig.
181+
182+
:param Starlette app: The FastAPI application instance.
183+
:param TracingConfig config: The Open Telemetry config.
184+
:returns: None
185+
"""
186+
165187
app.add_middleware(TracingMiddleware, config=config)
166188

167189

@@ -173,6 +195,19 @@ def setup_metrics(
173195
include_trace_exemplar: bool,
174196
include_metrics_endpoint: bool,
175197
) -> None:
198+
"""
199+
Set up metrics for a Starlette application.
200+
This function adds a MetricsMiddleware to the Starlette application with the specified parameters.
201+
If include_metrics_endpoint is True, it also adds a route for "/metrics" that returns Prometheus default metrics.
202+
203+
:param Starlette app: The Starlette application instance.
204+
:param str app_name: The name of the Starlette application.
205+
:param str metrics_prefix: The prefix to use for the metrics (default is "starlette").
206+
:param bool include_trace_exemplar: Whether to include trace exemplars in the metrics.
207+
:param bool include_metrics_endpoint: Whether to include a /metrics endpoint.
208+
:returns: None
209+
"""
210+
176211
app.add_middleware(
177212
MetricsMiddleware,
178213
app_name=app_name,

src/asgi_monitor/logging/configure.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def configure_logging(
1515
json_format: bool,
1616
include_trace: bool,
1717
) -> None:
18+
"""
19+
Default logging setting for logging and structlog.
20+
21+
:param str | int level: Logging level.
22+
:param bool json_format: The format of the logs. If True, the log will be rendered as JSON.
23+
:param bool include_trace: Include tracing information ("trace_id", "span_id", "parent_span_id", "service.name").
24+
:returns: None
25+
"""
26+
1827
_configure_structlog(json_format=json_format, include_trace=include_trace)
1928
_configure_default_logging(level=level, json_format=json_format, include_trace=include_trace)
2029

src/asgi_monitor/logging/gunicorn/logger.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
class StubbedGunicornLogger(Logger):
10+
"""
11+
Custom Gunicorn logger that allows setting up log levels based on Gunicorn configuration.
12+
"""
13+
1014
def setup(self, cfg: Config) -> None:
1115
_name_to_level = {
1216
"CRITICAL": logging.CRITICAL,

src/asgi_monitor/logging/gunicorn/worker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77

88
class GunicornStandaloneApplication(BaseApplication):
9+
"""
10+
Custom standalone application class for running a Gunicorn server with a ASGI application using Uvicorn worker.
11+
"""
12+
913
def __init__(
1014
self,
1115
app: Any,

src/asgi_monitor/logging/uvicorn/log_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ def build_uvicorn_log_config(
119119
json_format: bool,
120120
include_trace: bool,
121121
) -> dict[str, Any]:
122+
"""
123+
Building a Uvicorn log config.
124+
125+
:param str | int level: Logging level.
126+
:param bool json_format: The format of the logs. If True, the log will be rendered as JSON.
127+
:param bool include_trace: Include tracing information ("trace_id", "span_id", "parent_span_id", "service.name").
128+
:returns: Logging configuration for Uvicorn
129+
"""
130+
122131
level_name = logging.getLevelName(level)
123132

124133
if json_format:

src/asgi_monitor/logging/uvicorn/worker.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,52 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
2828

2929

3030
class StructlogTextLogUvicornWorker(StructlogDefaultUvicornWorker):
31+
"""
32+
UvicornWorker with custom logging configuration:
33+
- level: DEBUG
34+
- json_format: False
35+
- include_trace: False
36+
"""
37+
3138
level: int = logging.DEBUG
3239
json_format: bool = False
3340
include_trace: bool = False
3441

3542

3643
class StructlogTraceTextLogUvicornWorker(StructlogDefaultUvicornWorker):
44+
"""
45+
UvicornWorker with custom logging configuration:
46+
- level: DEBUG
47+
- json_format: False
48+
- include_trace: False
49+
"""
50+
3751
level: int = logging.DEBUG
3852
json_format: bool = False
3953
include_trace: bool = True
4054

4155

4256
class StructlogJSONLogUvicornWorker(StructlogDefaultUvicornWorker):
57+
"""
58+
UvicornWorker with custom logging configuration:
59+
- level: DEBUG
60+
- json_format: True
61+
- include_trace: False
62+
"""
63+
4364
level: int = logging.DEBUG
4465
json_format: bool = True
4566
include_trace: bool = False
4667

4768

4869
class StructlogTraceJSONLogUvicornWorker(StructlogDefaultUvicornWorker):
70+
"""
71+
UvicornWorker with custom logging configuration:
72+
- level: DEBUG
73+
- json_format: True
74+
- include_trace: True
75+
"""
76+
4977
level: int = logging.DEBUG
5078
json_format: bool = True
5179
include_trace: bool = True

src/asgi_monitor/metrics/get_latest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@
2121

2222
@dataclass(frozen=True, slots=True)
2323
class MetricsResponse:
24+
"""
25+
Represents a response containing metrics data.
26+
"""
27+
2428
status_code: int
2529
headers: dict[str, str]
2630
payload: bytes
2731

2832

2933
def get_latest_metrics(*, openmetrics_format: bool) -> MetricsResponse:
34+
"""
35+
Generates the latest metrics data in either Prometheus or OpenMetrics format.
36+
37+
:param bool openmetrics_format: A flag indicating whether to generate metrics in OpenMetrics format.
38+
:returns: MetricsResponse
39+
"""
40+
3041
registry = REGISTRY
3142

3243
if "PROMETHEUS_MULTIPROC_DIR" in os.environ:

src/asgi_monitor/tracing/config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,40 @@
1414

1515
@dataclass
1616
class CommonTracingConfig:
17+
"""Configuration class for the OpenTelemetry middleware.
18+
Consult the OpenTelemetry ASGI documentation for more info about the configuration options.
19+
https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html
20+
"""
21+
1722
exclude_urls_env_key: str
23+
"""Key to use when checking whether a list of excluded urls is passed via ENV.
24+
OpenTelemetry supports excluding urls by passing an env in the format '{exclude_urls_env_key}_EXCLUDED_URLS'.
25+
"""
26+
1827
scope_span_details_extractor: Callable[[Any], tuple[str, dict[str, Any]]]
28+
"""
29+
Callback which should return a string and a tuple, representing the desired default span name and a dictionary
30+
with any additional span attributes to set.
31+
"""
32+
1933
server_request_hook_handler: OpenTelemetryHookHandler | None = field(default=None)
34+
"""Optional callback which is called with the server span and ASGI scope object for every incoming request."""
35+
2036
client_request_hook_handler: OpenTelemetryHookHandler | None = field(default=None)
37+
"""Optional callback which is called with the internal span and an ASGI scope which is sent as a dictionary for when
38+
the method receive is called.
39+
"""
40+
2141
client_response_hook_handler: OpenTelemetryHookHandler | None = field(default=None)
42+
"""Optional callback which is called with the internal span and an ASGI event which is sent as a dictionary for when
43+
the method send is called.
44+
"""
45+
2246
meter_provider: MeterProvider | None = field(default=None)
47+
"""Optional meter provider to use."""
48+
2349
tracer_provider: TracerProvider | None = field(default=None)
50+
"""Optional tracer provider to use."""
51+
2452
meter: Meter | None = field(default=None)
53+
"""Optional meter to use."""

0 commit comments

Comments
 (0)