From cc9614d8b49adb2413e32e684d4806b4fac88644 Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Thu, 13 Mar 2025 11:50:52 +0100 Subject: [PATCH 1/4] migrate to new Counter type for APIGW REST API analytics --- .../next_gen/execute_api/handlers/analytics.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py index b93a611fed2f6..3a25fd747d76d 100644 --- a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py +++ b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py @@ -1,7 +1,7 @@ import logging from localstack.http import Response -from localstack.utils.analytics.usage import UsageSetCounter +from localstack.utils.analytics.metrics import Counter, _LabeledCounterMetric from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain from ..context import RestApiInvocationContext @@ -10,10 +10,12 @@ class IntegrationUsageCounter(RestApiGatewayHandler): - counter: UsageSetCounter + counter: _LabeledCounterMetric - def __init__(self, counter: UsageSetCounter = None): - self.counter = counter or UsageSetCounter(namespace="apigateway:invokedrest") + def __init__(self, counter: _LabeledCounterMetric = None): + self.counter = counter or Counter( + namespace="apigateway", name="rest_api_execute", labels=["invocation_type"] + ) def __call__( self, @@ -31,7 +33,7 @@ def __call__( # hence we should count it as a NOT_FOUND invocation invocation_type = "NOT_FOUND" - self.counter.record(invocation_type) + self.counter.labels(invocation_type=invocation_type).increment() @staticmethod def _get_aws_integration_service(integration_uri: str) -> str: From 4f5d9ac6bff4d36c85c794f21e36532d8cde7f1a Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Thu, 13 Mar 2025 13:27:07 +0100 Subject: [PATCH 2/4] update typing --- .../apigateway/next_gen/execute_api/handlers/analytics.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py index 3a25fd747d76d..741381a7eed5f 100644 --- a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py +++ b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py @@ -1,7 +1,7 @@ import logging from localstack.http import Response -from localstack.utils.analytics.metrics import Counter, _LabeledCounterMetric +from localstack.utils.analytics.metrics import Counter from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain from ..context import RestApiInvocationContext @@ -10,9 +10,7 @@ class IntegrationUsageCounter(RestApiGatewayHandler): - counter: _LabeledCounterMetric - - def __init__(self, counter: _LabeledCounterMetric = None): + def __init__(self, counter: Counter = None): self.counter = counter or Counter( namespace="apigateway", name="rest_api_execute", labels=["invocation_type"] ) From e0ee15e9285dc322b45018ef590ad4b2b47f5cbd Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Fri, 14 Mar 2025 19:12:49 +0100 Subject: [PATCH 3/4] re-use typing --- .../apigateway/next_gen/execute_api/handlers/analytics.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py index 741381a7eed5f..96b433a504737 100644 --- a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py +++ b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py @@ -1,7 +1,7 @@ import logging from localstack.http import Response -from localstack.utils.analytics.metrics import Counter +from localstack.utils.analytics.metrics import Counter, LabeledCounterMetric from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain from ..context import RestApiInvocationContext @@ -10,7 +10,9 @@ class IntegrationUsageCounter(RestApiGatewayHandler): - def __init__(self, counter: Counter = None): + counter: LabeledCounterMetric + + def __init__(self, counter: LabeledCounterMetric = None): self.counter = counter or Counter( namespace="apigateway", name="rest_api_execute", labels=["invocation_type"] ) From cdb5635d88052eb86b778c9ac3e34520cd122bf5 Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Tue, 18 Mar 2025 13:38:45 +0100 Subject: [PATCH 4/4] fix -api routes splitting --- .../apigateway/next_gen/execute_api/handlers/analytics.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py index 96b433a504737..82ba2b7d2593c 100644 --- a/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py +++ b/localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py @@ -43,4 +43,8 @@ def _get_aws_integration_service(integration_uri: str) -> str: if len(split_arn := integration_uri.split(":", maxsplit=5)) < 4: return "null" - return split_arn[4] + service = split_arn[4] + # the URI can also contain some .-api kind of route like `execute-api` or `appsync-api` + # we need to make sure we do not pass the full value back + service = service.split(".")[-1] + return service