7
7
from huntflow_base_metrics .export import export_to_http_response
8
8
from huntflow_base_metrics .web_frameworks ._middleware import PathTemplate , PrometheusMiddleware
9
9
10
- __all__ = ["add_middleware" , "get_http_response_metrics" ]
10
+ __all__ = ["add_middleware" , "get_http_response_metrics" , "get_middleware" ]
11
11
12
12
13
13
class _PrometheusMiddleware (PrometheusMiddleware [Request ]):
@@ -42,9 +42,11 @@ def get_method(request: Request) -> str:
42
42
def get_path_template (request : Request ) -> PathTemplate :
43
43
match_info = request .match_info
44
44
value = request .rel_url .path
45
+ is_handled = False
45
46
if match_info and match_info .route .resource :
46
47
value = match_info .route .resource .canonical
47
- return PathTemplate (value = value , is_handled = match_info is not None )
48
+ is_handled = True
49
+ return PathTemplate (value = value , is_handled = is_handled )
48
50
49
51
50
52
def add_middleware (
@@ -67,6 +69,25 @@ def add_middleware(
67
69
app .middlewares .append (_PrometheusMiddleware .dispatch )
68
70
69
71
72
+ def get_middleware (
73
+ include_routes : Optional [Iterable [str ]] = None ,
74
+ exclude_routes : Optional [Iterable [str ]] = None ,
75
+ ) -> Callable :
76
+ """
77
+ Returns observing middleware to the given AioHTTP application.
78
+ Use if middleware order matters.
79
+
80
+ :param include_routes: optional set of path templates to observe.
81
+ If it's not empty, then only the specified routes will be observed
82
+ (also exclude_routes will be ignored).
83
+ :param exclude_routes: optional set of path templates to not observe.
84
+ If it's not empty (and include_routes is not specified), then the
85
+ specified routes will not be observed.
86
+ """
87
+ _PrometheusMiddleware .configure (include_routes , exclude_routes )
88
+ return _PrometheusMiddleware .dispatch
89
+
90
+
70
91
def get_http_response_metrics () -> Response :
71
92
"""Method returns HTTP Response with current metrics in prometheus format."""
72
93
content , content_type = export_to_http_response ()
0 commit comments