Skip to content

Add UsageMultiSetCounter (up for discussion) #11934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions localstack-core/localstack/utils/analytics/usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import math
import threading
from collections import defaultdict
from itertools import count
from typing import Any
Expand Down Expand Up @@ -49,6 +50,63 @@ def aggregate(self) -> dict:
return self.state


class UsageMultiSetCounter:
"""
Use this counter to count occurrences of unique values for multiple dimensions.
This dynamically creates UsageSetCounters and should be used with care (i.e., with limited keys).
Example:
my_feature_counter = UsageMultiSetCounter("pipes:invocation")
my_feature_counter.record("aws:sqs", "aws:lambda")
my_feature_counter.record("aws:sqs", "aws:lambda")
my_feature_counter.record("aws:sqs", "aws:stepfunctions")
my_feature_counter.record("aws:kinesis", "aws:lambda")
aggregate is implemented for each counter individually
my_feature_counter.aggregate() is available for testing purposes:
{
"aws:sqs": {
"aws:lambda": 2,
"aws:stepfunctions": 1,
},
"aws:kinesis": {
"aws:lambda": 1
}
}
"""

namespace: str
_counters: dict[str, UsageSetCounter]
lock = threading.Lock()

def __init__(self, namespace: str):
self._counters = {}
self.namespace = namespace

def record(self, key: str, value: str):
namespace = f"{self.namespace}:{key}"
if namespace in self._counters:
set_counter = self._counters[namespace]
else:
with self.lock:
if namespace in self._counters:
set_counter = self._counters[namespace]
else:
# We cannot use setdefault here because Python always instantiates a new UsageSetCounter,
# which overwrites the collector_registry
set_counter = UsageSetCounter(namespace)
self._counters[namespace] = set_counter

self._counters[namespace] = set_counter
set_counter.record(value)

def aggregate(self) -> dict:
"""aggregate is invoked on a per UsageSetCounter level because each counter is registered individually.
This utility is only for testing!"""
merged_dict = {}
for namespace, counter in self._counters.items():
merged_dict[namespace] = counter.aggregate()
return merged_dict


class UsageCounter:
"""
Use this counter to count numeric values
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/utils/analytics/test_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from localstack.utils.analytics.usage import UsageMultiSetCounter


def test_multi_set_counter():
my_feature_counter = UsageMultiSetCounter("pipes:invocation")
my_feature_counter.record("aws:sqs", "aws:lambda")
my_feature_counter.record("aws:sqs", "aws:lambda")
my_feature_counter.record("aws:sqs", "aws:stepfunctions")
my_feature_counter.record("aws:kinesis", "aws:lambda")
assert my_feature_counter.aggregate() == {
"pipes:invocation:aws:sqs": {
"aws:lambda": 2,
"aws:stepfunctions": 1,
},
"pipes:invocation:aws:kinesis": {"aws:lambda": 1},
}
assert my_feature_counter._counters["pipes:invocation:aws:sqs"].state == {
"aws:lambda": 2,
"aws:stepfunctions": 1,
}
assert my_feature_counter._counters["pipes:invocation:aws:kinesis"].state == {"aws:lambda": 1}
Loading