8000 add counter for internal paths calls and user agents · localstack/localstack@f78c553 · GitHub
[go: up one dir, main page]

Skip to content

Commit f78c553

Browse files
committed
add counter for internal paths calls and user agents
1 parent fd33d31 commit f78c553

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

localstack-core/localstack/aws/handlers/validation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
from typing import Any
67

78
from openapi_core import OpenAPI
89
from openapi_core.contrib.werkzeug import WerkzeugOpenAPIRequest, WerkzeugOpenAPIResponse
@@ -18,16 +19,43 @@
1819
from localstack.aws.chain import Handler, HandlerChain
1920
from localstack.constants import INTERNAL_RESOURCE_PATH
2021
from localstack.http import Response
22+
from localstack.utils.analytics.usage import UsageSetCounter
2123

2224
LOG = logging.getLogger(__name__)
2325

2426

27+
class UsageCollectorFactory:
28+
_collector_registry: dict[str, Any] = {}
29+
"""Registry for the different paths."""
30+
31+
NAMESPACE_PREFIX = "internal"
32+
"""Namespace prefix to track usage of public endpoints (_localstack/ and _aws/)."""
33+
34+
@classmethod
35+
def get_collector(cls, path: str):
36+
namespace = f"{cls.NAMESPACE_PREFIX}/{path}"
37+
if namespace not in cls._collector_registry:
38+
cls._collector_registry[namespace] = UsageSetCounter(namespace)
39+
return cls._collector_registry[namespace]
40+
41+
2542
class OpenAPIValidator(Handler):
2643
open_apis: list["OpenAPI"]
2744

2845
def __init__(self) -> None:
2946
self._load_specs()
3047

48+
def _record_usage(self, context: RequestContext) -> None:
49+
if config.DISABLE_EVENTS:
50+
return
51+
52+
request_path = context.request.path
53+
user_agent = context.request.headers.get("User-Agent")
54+
if not request_path or not user_agent:
55+
return
56+
collector = UsageCollectorFactory.get_collector(context.request.path)
57+
collector.record(user_agent)
58+
3159
def _load_specs(self) -> None:
3260
"""Load the openapi spec plugins iff at least one between request and response validation is set."""
3361
if not (config.OPENAPI_VALIDATE_REQUEST or config.OPENAPI_VALIDATE_RESPONSE):
@@ -45,6 +73,7 @@ class OpenAPIRequestValidator(OpenAPIValidator):
4573
"""
4674

4775
def __call__(self, chain: HandlerChain, context: RequestContext, response: Response):
76+
self._record_usage(context)
4877
if not config.OPENAPI_VALIDATE_REQUEST:
4978
return
5079

0 commit comments

Comments
 (0)
0