Skip to content

Commit fcc4de7

Browse files
authored
APIGW: migrate to new Counter type for REST API analytics (#12383)
1 parent 2f84ab4 commit fcc4de7

File tree

1 file changed

+12
-6
lines changed
  • localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers

1 file changed

+12
-6
lines changed

localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/analytics.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22

33
from localstack.http import Response
4-
from localstack.utils.analytics.usage import UsageSetCounter
4+
from localstack.utils.analytics.metrics import Counter, LabeledCounterMetric
55

66
from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain
77
from ..context import RestApiInvocationContext
@@ -10,10 +10,12 @@
1010

1111

1212
class IntegrationUsageCounter(RestApiGatewayHandler):
13-
counter: UsageSetCounter
13+
counter: LabeledCounterMetric
1414

15-
def __init__(self, counter: UsageSetCounter = None):
16-
self.counter = counter or UsageSetCounter(namespace="apigateway:invokedrest")
15+
def __init__(self, counter: LabeledCounterMetric = None):
16+
self.counter = counter or Counter(
17+
namespace="apigateway", name="rest_api_execute", labels=["invocation_type"]
18+
)
1719

1820
def __call__(
1921
self,
@@ -31,7 +33,7 @@ def __call__(
3133
# hence we should count it as a NOT_FOUND invocation
3234
invocation_type = "NOT_FOUND"
3335

34-
self.counter.record(invocation_type)
36+
self.counter.labels(invocation_type=invocation_type).increment()
3537

3638
@staticmethod
3739
def _get_aws_integration_service(integration_uri: str) -> str:
@@ -41,4 +43,8 @@ def _get_aws_integration_service(integration_uri: str) -> str:
4143
if len(split_arn := integration_uri.split(":", maxsplit=5)) < 4:
4244
return "null"
4345

44-
return split_arn[4]
46+
service = split_arn[4]
47+
# the URI can also contain some <api-id>.<service>-api kind of route like `execute-api` or `appsync-api`
48+
# we need to make sure we do not pass the full value back
49+
service = service.split(".")[-1]
50+
return service

0 commit comments

Comments
 (0)