55 SetMetric ,
66)
77from datadog .dogstatsd .metric_types import MetricType
8+ from datadog .util .format import validate_cardinality
89
910
1011class Aggregator (object ):
11- def __init__ (self ):
12+ def __init__ (self , cardinality = None ):
1213 self .metrics_map = {
1314 MetricType .COUNT : {},
1415 MetricType .GAUGE : {},
@@ -19,6 +20,7 @@ def __init__(self):
1920 MetricType .GAUGE : threading .RLock (),
2021 MetricType .SET : threading .RLock (),
2122 }
23+ self ._cardinality = cardinality
2224
2325 def flush_aggregated_metrics (self ):
2426 metrics = []
@@ -34,29 +36,32 @@ def get_context(self, name, tags):
3436 tags_str = "," .join (tags ) if tags is not None else ""
3537 return "{}:{}" .format (name , tags_str )
3638
37- def count (self , name , value , tags , rate , timestamp = 0 ):
39+ def count (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
3840 return self .add_metric (
39- MetricType .COUNT , CountMetric , name , value , tags , rate , timestamp
41+ MetricType .COUNT , CountMetric , name , value , tags , rate , timestamp , cardinality
4042 )
4143
42- def gauge (self , name , value , tags , rate , timestamp = 0 ):
44+ def gauge (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
4345 return self .add_metric (
44- MetricType .GAUGE , GaugeMetric , name , value , tags , rate , timestamp
46+ MetricType .GAUGE , GaugeMetric , name , value , tags , rate , timestamp , cardinality
4547 )
4648
47- def set (self , name , value , tags , rate , timestamp = 0 ):
49+ def set (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
4850 return self .add_metric (
49- MetricType .SET , SetMetric , name , value , tags , rate , timestamp
51+ MetricType .SET , SetMetric , name , value , tags , rate , timestamp , cardinality
5052 )
5153
5254 def add_metric (
53- self , metric_type , metric_class , name , value , tags , rate , timestamp = 0
55+ self , metric_type , metric_class , name , value , tags , rate , timestamp = 0 , cardinality = None
5456 ):
5557 context = self .get_context (name , tags )
5658 with self ._locks [metric_type ]:
5759 if context in self .metrics_map [metric_type ]:
5860 self .metrics_map [metric_type ][context ].aggregate (value )
5961 else :
62+ if cardinality is None :
63+ cardinality = self ._cardinality
64+ validate_cardinality (cardinality )
6065 self .metrics_map [metric_type ][context ] = metric_class (
61- name , value , tags , rate , timestamp
66+ name , value , tags , rate , timestamp , cardinality
6267 )
0 commit comments