|
1 | 1 | import datetime |
2 | 2 | import math |
3 | | -import threading |
4 | 3 | from collections import defaultdict |
5 | 4 | from itertools import count |
6 | 5 | from typing import Any |
@@ -50,66 +49,6 @@ def aggregate(self) -> dict: |
50 | 49 | return self.state |
51 | 50 |
|
52 | 51 |
|
53 | | -class UsageMultiSetCounter: |
54 | | - """ |
55 | | - Use this counter to count occurrences of unique values for multiple dimensions. |
56 | | - This dynamically creates UsageSetCounters and should be used with care (i.e., with limited keys). |
57 | | -
|
58 | | - Example: |
59 | | -
|
60 | | - my_feature_counter = UsageMultiSetCounter("pipes:invocation") |
61 | | - my_feature_counter.record("aws:sqs", "aws:lambda") |
62 | | - my_feature_counter.record("aws:sqs", "aws:lambda") |
63 | | - my_feature_counter.record("aws:sqs", "aws:stepfunctions") |
64 | | - my_feature_counter.record("aws:kinesis", "aws:lambda") |
65 | | - aggregate is implemented for each counter individually |
66 | | -
|
67 | | - my_feature_counter.aggregate() is available for testing purposes: |
68 | | - { |
69 | | - "aws:sqs": { |
70 | | - "aws:lambda": 2, |
71 | | - "aws:stepfunctions": 1, |
72 | | - }, |
73 | | - "aws:kinesis": { |
74 | | - "aws:lambda": 1 |
75 | | - } |
76 | | - } |
77 | | - """ |
78 | | - |
79 | | - namespace: str |
80 | | - _counters: dict[str, UsageSetCounter] |
81 | | - lock = threading.Lock() |
82 | | - |
83 | | - def __init__(self, namespace: str): |
84 | | - self._counters = {} |
85 | | - self.namespace = namespace |
86 | | - |
87 | | - def record(self, key: str, value: str): |
88 | | - namespace = f"{self.namespace}:{key}" |
89 | | - if namespace in self._counters: |
90 | | - set_counter = self._counters[namespace] |
91 | | - else: |
92 | | - with self.lock: |
93 | | - if namespace in self._counters: |
94 | | - set_counter = self._counters[namespace] |
95 | | - else: |
96 | | - # We cannot use setdefault here because Python always instantiates a new UsageSetCounter, |
97 | | - # which overwrites the collector_registry |
98 | | - set_counter = UsageSetCounter(namespace) |
99 | | - self._counters[namespace] = set_counter |
100 | | - |
101 | | - self._counters[namespace] = set_counter |
102 | | - set_counter.record(value) |
103 | | - |
104 | | - def aggregate(self) -> dict: |
105 | | - """aggregate is invoked on a per UsageSetCounter level because each counter is registered individually. |
106 | | - This utility is only for testing!""" |
107 | | - merged_dict = {} |
108 | | - for namespace, counter in self._counters.items(): |
109 | | - merged_dict[namespace] = counter.aggregate() |
110 | | - return merged_dict |
111 | | - |
112 | | - |
113 | 52 | class UsageCounter: |
114 | 53 | """ |
115 | 54 | Use this counter to count numeric values |
|
0 commit comments