Interceptors¶
The helpers/interceptors subpackage provides request/response interceptors for FastAPI and gRPC, covering rate
limiting, metrics collection, exception handling, and distributed tracing.
FastAPI¶
metric¶
FastAPI middleware interceptor for collecting Prometheus metrics on HTTP requests.
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor ¶
Bases: BaseHTTPMiddleware
A FastAPI interceptor for collecting and reporting metrics using Prometheus.
This interceptor measures the response time of HTTP requests and records it in a Prometheus histogram. It also tracks the number of active requests using a Prometheus gauge. The interceptor captures errors and logs them for monitoring purposes.
Source code in archipy/helpers/interceptors/fastapi/metric/interceptor.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.ZERO_TO_ONE_SECONDS_BUCKETS
class-attribute
¶
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.ONE_TO_FIVE_SECONDS_BUCKETS
class-attribute
¶
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.FIVE_TO_THIRTY_SECONDS_BUCKETS
class-attribute
¶
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.TOTAL_BUCKETS
class-attribute
¶
TOTAL_BUCKETS: list[float] = (
ZERO_TO_ONE_SECONDS_BUCKETS
+ ONE_TO_FIVE_SECONDS_BUCKETS
+ FIVE_TO_THIRTY_SECONDS_BUCKETS
+ [float("inf")]
)
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.RESPONSE_TIME_SECONDS
class-attribute
¶
RESPONSE_TIME_SECONDS: Histogram = Histogram(
"fastapi_response_time_seconds",
"Time spent processing HTTP request",
labelnames=("method", "status_code", "path_template"),
buckets=TOTAL_BUCKETS,
)
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.ACTIVE_REQUESTS
class-attribute
¶
ACTIVE_REQUESTS: Gauge = Gauge(
"fastapi_active_requests",
"Number of active HTTP requests",
labelnames=("method", "path_template"),
)
archipy.helpers.interceptors.fastapi.metric.interceptor.FastAPIMetricInterceptor.dispatch
async
¶
Intercept HTTP requests to measure response time and track active requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming HTTP request. |
required |
call_next
|
Callable[[Request], Awaitable[Response]]
|
The next interceptor or endpoint to call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Response
|
The HTTP response from the endpoint. |
Raises:
| Type | Description |
|---|---|
Exception
|
If an exception occurs during request processing, it is captured and re-raised. |
Source code in archipy/helpers/interceptors/fastapi/metric/interceptor.py
options: show_root_toc_entry: false heading_level: 3
rate_limit¶
FastAPI interceptor that enforces configurable rate limits on HTTP endpoints using Redis as a backend.
archipy.helpers.interceptors.fastapi.rate_limit.fastapi_rest_rate_limit_handler.FastAPIRestRateLimitHandler ¶
A rate-limiting handler for FastAPI REST endpoints using Redis for tracking.
This class provides rate-limiting functionality by tracking the number of requests made to a specific endpoint within a defined time window. If the request limit is exceeded, it raises an HTTP 429 Too Many Requests error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calls_count
|
StrictInt
|
The maximum number of allowed requests within the time window. |
1
|
milliseconds
|
StrictInt
|
The time window in milliseconds. |
0
|
seconds
|
StrictInt
|
The time window in seconds. |
0
|
minutes
|
StrictInt
|
The time window in minutes. |
0
|
hours
|
StrictInt
|
The time window in hours. |
0
|
days
|
StrictInt
|
The time window in days. |
0
|
query_params
|
set(StrictStr)
|
request query parameters for rate-limiting based on query params. |
None
|
Source code in archipy/helpers/interceptors/fastapi/rate_limit/fastapi_rest_rate_limit_handler.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
archipy.helpers.interceptors.fastapi.rate_limit.fastapi_rest_rate_limit_handler.FastAPIRestRateLimitHandler.query_params
instance-attribute
¶
archipy.helpers.interceptors.fastapi.rate_limit.fastapi_rest_rate_limit_handler.FastAPIRestRateLimitHandler.calls_count
instance-attribute
¶
archipy.helpers.interceptors.fastapi.rate_limit.fastapi_rest_rate_limit_handler.FastAPIRestRateLimitHandler.milliseconds
instance-attribute
¶
milliseconds = (
milliseconds
+ 1000 * seconds
+ 60 * 1000 * minutes
+ 60 * 60 * 1000 * hours
+ 24 * 60 * 60 * 1000 * days
)
archipy.helpers.interceptors.fastapi.rate_limit.fastapi_rest_rate_limit_handler.FastAPIRestRateLimitHandler.redis_client
instance-attribute
¶
options: show_root_toc_entry: false heading_level: 3
gRPC¶
base¶
Abstract base classes for gRPC client and server interceptors.
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails ¶
Bases: _ClientCallDetailsFields, ClientCallDetails
Describes an RPC to be invoked.
This class extends grpc.ClientCallDetails and provides additional fields for RPC details.
See https://grpc.github.io/grpc/python/grpc.html#grpc.ClientCallDetails
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.timeout
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.metadata
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.credentials
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.wait_for_ready
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.compression
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor ¶
Bases: UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, StreamUnaryClientInterceptor, StreamStreamClientInterceptor
Base class for gRPC client interceptors.
This class provides a base implementation for intercepting gRPC client calls. It supports unary-unary, unary-stream, stream-unary, and stream-stream RPCs.
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept
abstractmethod
¶
Intercepts a gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_unary_unary ¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_unary_stream ¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_stream_unary ¶
intercept_stream_unary(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_stream_stream ¶
intercept_stream_stream(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails ¶
Bases: _AsyncClientCallDetailsFields, ClientCallDetails
Describes an RPC to be invoked in an asynchronous context.
This class extends grpc.aio.ClientCallDetails and provides additional fields for RPC details.
See https://grpc.github.io/grpc/python/grpc.html#grpc.ClientCallDetails
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.timeout
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.metadata
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.credentials
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.wait_for_ready
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor ¶
Bases: UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, StreamUnaryClientInterceptor, StreamStreamClientInterceptor
Base class for asynchronous gRPC client interceptors.
This class provides a base implementation for intercepting asynchronous gRPC client calls. It supports unary-unary, unary-stream, stream-unary, and stream-stream RPCs.
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept
abstractmethod
async
¶
Intercepts an asynchronous gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_unary_unary
async
¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_unary_stream
async
¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_stream_unary
async
¶
intercept_stream_unary(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_stream_stream
async
¶
intercept_stream_stream(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
options: show_root_toc_entry: false heading_level: 3
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName ¶
Bases: BaseDTO
A data transfer object (DTO) representing the parsed method name of a gRPC call.
Attributes:
| Name | Type | Description |
|---|---|---|
full_name |
str
|
The full name of the method, including package, service, and method. |
package |
str
|
The package name. |
service |
str
|
The service name. |
method |
str
|
The method name. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.full_name
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.package
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.service
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor ¶
Bases: ServerInterceptor
Base class for gRPC server interceptors.
This class provides a base implementation for intercepting gRPC server calls. It allows custom logic to be injected into the request/response flow.
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor.intercept
abstractmethod
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a gRPC server call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The method to be intercepted. |
required |
request
|
object
|
The request object. |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
str
|
The full method name (e.g., "/package.Service/Method"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted method. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor ¶
Bases: ServerInterceptor
Base class for asynchronous gRPC server interceptors.
This class provides a simplified base implementation for intercepting async gRPC server calls. Unlike the synchronous version, async interceptors work differently and don't need the complex handler wrapping logic.
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor.intercept
abstractmethod
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The method to be intercepted. |
required |
request
|
object
|
The request object. |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted method. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], Awaitable[RpcMethodHandler]
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.parse_method_name ¶
Parses a gRPC method name into its components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method_name
|
str
|
The full method name (e.g., "/package.service/method"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MethodName |
MethodName
|
A |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
exception¶
gRPC server interceptor that catches exceptions and converts them to gRPC status codes.
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor ¶
Bases: BaseGrpcServerInterceptor
A sync gRPC server interceptor for centralized exception handling.
This interceptor catches all exceptions thrown by gRPC service methods and converts them to appropriate gRPC errors, eliminating the need for repetitive try-catch blocks in each service method.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor.intercept ¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a sync gRPC server call and handles exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The sync gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the sync gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Note
This method will not return anything if an exception is handled, as the exception handling will abort the gRPC context.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor ¶
Bases: BaseAsyncGrpcServerInterceptor
An async gRPC server interceptor for centralized exception handling.
This interceptor catches all exceptions thrown by gRPC service methods and converts them to appropriate gRPC errors, eliminating the need for repetitive try-catch blocks in each service method.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor.intercept
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call and handles exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The async gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the async gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Note
This method will not return anything if an exception is handled, as the exception handling will abort the gRPC context.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], Awaitable[RpcMethodHandler]
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
metric¶
gRPC server interceptor for collecting Prometheus metrics on RPC calls.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor ¶
Bases: BaseGrpcServerInterceptor
A gRPC server interceptor for collecting and reporting metrics using Prometheus.
This interceptor measures the response time of gRPC methods and records it in a Prometheus histogram. It also tracks the number of active requests using a Prometheus gauge. It also captures errors and logs them for monitoring purposes.
Source code in archipy/helpers/interceptors/grpc/metric/server_interceptor.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.ZERO_TO_ONE_SECONDS_BUCKETS
class-attribute
¶
Buckets for measuring response times between 1 and 5 seconds.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.ONE_TO_FIVE_SECONDS_BUCKETS
class-attribute
¶
Buckets for measuring response times between 5 and 30 seconds.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.FIVE_TO_THIRTY_SECONDS_BUCKETS
class-attribute
¶
Combined buckets for measuring response times from 0 to 30 seconds and beyond.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.TOTAL_BUCKETS
class-attribute
instance-attribute
¶
TOTAL_BUCKETS = (
ZERO_TO_ONE_SECONDS_BUCKETS
+ ONE_TO_FIVE_SECONDS_BUCKETS
+ FIVE_TO_THIRTY_SECONDS_BUCKETS
+ [float("inf")]
)
Prometheus histogram for tracking response times of gRPC methods.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.RESPONSE_TIME_SECONDS
class-attribute
instance-attribute
¶
RESPONSE_TIME_SECONDS = Histogram(
"grpc_response_time_seconds",
"Time spent processing gRPC request",
labelnames=(
"package",
"service",
"method",
"status_code",
),
buckets=TOTAL_BUCKETS,
)
Prometheus gauge for tracking active gRPC requests.
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.ACTIVE_REQUESTS
class-attribute
instance-attribute
¶
ACTIVE_REQUESTS = Gauge(
"grpc_active_requests",
"Number of active gRPC requests",
labelnames=("package", "service", "method"),
)
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.intercept ¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a gRPC server call to measure response time and track active requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Raises:
| Type | Description |
|---|---|
Exception
|
If an exception occurs during the method execution, it is captured and logged. |
Source code in archipy/helpers/interceptors/grpc/metric/server_interceptor.py
archipy.helpers.interceptors.grpc.metric.server_interceptor.GrpcServerMetricInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor ¶
Bases: BaseAsyncGrpcServerInterceptor
An async gRPC server interceptor for collecting and reporting metrics using Prometheus.
This interceptor measures the response time of async gRPC methods and records it in a Prometheus histogram. It also tracks the number of active requests using a Prometheus gauge. It also captures errors and logs them for monitoring purposes.
Source code in archipy/helpers/interceptors/grpc/metric/server_interceptor.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.ZERO_TO_ONE_SECONDS_BUCKETS
class-attribute
¶
Buckets for measuring response times between 1 and 5 seconds.
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.ONE_TO_FIVE_SECONDS_BUCKETS
class-attribute
¶
Buckets for measuring response times between 5 and 30 seconds.
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.FIVE_TO_THIRTY_SECONDS_BUCKETS
class-attribute
¶
Combined buckets for measuring response times from 0 to 30 seconds and beyond.
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.TOTAL_BUCKETS
class-attribute
instance-attribute
¶
TOTAL_BUCKETS = (
ZERO_TO_ONE_SECONDS_BUCKETS
+ ONE_TO_FIVE_SECONDS_BUCKETS
+ FIVE_TO_THIRTY_SECONDS_BUCKETS
+ [float("inf")]
)
Prometheus histogram for tracking response times of async gRPC methods.
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.RESPONSE_TIME_SECONDS
class-attribute
instance-attribute
¶
RESPONSE_TIME_SECONDS = Histogram(
"grpc_async_response_time_seconds",
"Time spent processing async gRPC request",
labelnames=(
"package",
"service",
"method",
"status_code",
),
buckets=TOTAL_BUCKETS,
)
Prometheus gauge for tracking active async gRPC requests.
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.ACTIVE_REQUESTS
class-attribute
instance-attribute
¶
ACTIVE_REQUESTS = Gauge(
"grpc_async_active_requests",
"Number of active async gRPC requests",
labelnames=("package", "service", "method"),
)
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.intercept
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call to measure response time and track active requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The async gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the async gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Raises:
| Type | Description |
|---|---|
Exception
|
If an exception occurs during the method execution, it is captured and logged. |
Source code in archipy/helpers/interceptors/grpc/metric/server_interceptor.py
archipy.helpers.interceptors.grpc.metric.server_interceptor.AsyncGrpcServerMetricInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], Awaitable[RpcMethodHandler]
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
trace¶
gRPC interceptors for propagating distributed tracing context across client and server.
archipy.helpers.interceptors.grpc.trace.client_interceptor.logger
module-attribute
¶
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor ¶
Bases: BaseGrpcClientInterceptor
A gRPC client interceptor for tracing requests using Elastic APM and Sentry APM.
This interceptor injects the Elastic APM trace parent header into gRPC client requests to enable distributed tracing across services. It also creates Sentry transactions to monitor the performance of gRPC calls.
Source code in archipy/helpers/interceptors/grpc/trace/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor.intercept ¶
Intercepts a gRPC client call to inject the Elastic APM trace parent header and monitor performance with Sentry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The gRPC method being intercepted. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the gRPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted gRPC method. |
Notes
- If both Elastic APM and Sentry are disabled, the interceptor passes the call through.
- Creates Sentry spans for tracing gRPC client calls.
- Injects Elastic APM trace parent header when available.
Source code in archipy/helpers/interceptors/grpc/trace/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor.intercept_unary_unary ¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor.intercept_unary_stream ¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor.intercept_stream_unary ¶
intercept_stream_unary(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.GrpcClientTraceInterceptor.intercept_stream_stream ¶
intercept_stream_stream(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor ¶
Bases: BaseAsyncGrpcClientInterceptor
An asynchronous gRPC client interceptor for tracing requests using Elastic APM and Sentry APM.
This interceptor injects the Elastic APM trace parent header into asynchronous gRPC client requests to enable distributed tracing across services. It also creates Sentry spans for monitoring performance.
Source code in archipy/helpers/interceptors/grpc/trace/client_interceptor.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor.intercept
async
¶
Intercepts an asynchronous gRPC client call to inject the Elastic APM trace parent header and monitor with Sentry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The asynchronous gRPC method being intercepted. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the gRPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted gRPC method. |
Notes
- If both Elastic APM and Sentry are disabled, the interceptor passes the call through.
- Creates Sentry spans for tracing async gRPC client calls.
- Injects Elastic APM trace parent header when available.
Source code in archipy/helpers/interceptors/grpc/trace/client_interceptor.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor.intercept_unary_unary
async
¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor.intercept_unary_stream
async
¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor.intercept_stream_unary
async
¶
intercept_stream_unary(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.trace.client_interceptor.AsyncGrpcClientTraceInterceptor.intercept_stream_stream
async
¶
intercept_stream_stream(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
options: show_root_toc_entry: false heading_level: 3
archipy.helpers.interceptors.grpc.trace.server_interceptor.logger
module-attribute
¶
archipy.helpers.interceptors.grpc.trace.server_interceptor.GrpcServerTraceInterceptor ¶
Bases: BaseGrpcServerInterceptor
A gRPC server interceptor for tracing requests using Elastic APM and Sentry APM.
This interceptor captures and traces gRPC server requests, enabling distributed tracing across services. It integrates with both Elastic APM and Sentry to monitor and log transactions.
Source code in archipy/helpers/interceptors/grpc/trace/server_interceptor.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
archipy.helpers.interceptors.grpc.trace.server_interceptor.GrpcServerTraceInterceptor.intercept ¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a gRPC server call to trace the request using Elastic APM and Sentry APM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Raises:
| Type | Description |
|---|---|
Exception
|
If an exception occurs during the method execution, it is captured and logged. |
Notes
- If both Elastic APM and Sentry are disabled, the interceptor passes the call through.
- Creates Sentry transactions for tracing gRPC server calls.
- Handles Elastic APM distributed tracing with trace parent headers.
Source code in archipy/helpers/interceptors/grpc/trace/server_interceptor.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
archipy.helpers.interceptors.grpc.trace.server_interceptor.GrpcServerTraceInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.trace.server_interceptor.AsyncGrpcServerTraceInterceptor ¶
Bases: BaseAsyncGrpcServerInterceptor
An async gRPC server interceptor for tracing requests using Elastic APM and Sentry APM.
This interceptor captures and traces async gRPC server requests, enabling distributed tracing across services. It integrates with both Elastic APM and Sentry to monitor and log transactions.
Source code in archipy/helpers/interceptors/grpc/trace/server_interceptor.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
archipy.helpers.interceptors.grpc.trace.server_interceptor.AsyncGrpcServerTraceInterceptor.intercept
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call to trace the request using Elastic APM and Sentry APM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The async gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the async gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Raises:
| Type | Description |
|---|---|
Exception
|
If an exception occurs during the method execution, it is captured and logged. |
Notes
- If both Elastic APM and Sentry are disabled, the interceptor passes the call through.
- Creates Sentry transactions for tracing async gRPC server calls.
- Handles Elastic APM distributed tracing with trace parent headers.
Source code in archipy/helpers/interceptors/grpc/trace/server_interceptor.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
archipy.helpers.interceptors.grpc.trace.server_interceptor.AsyncGrpcServerTraceInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], Awaitable[RpcMethodHandler]
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3