11
11
)
12
12
from datadog .dogstatsd .metric_types import MetricType
13
13
from datadog .dogstatsd .max_sample_metric_context import MaxSampleMetricContexts
14
+ from datadog .util .format import validate_cardinality
14
15
15
16
16
17
class Aggregator (object ):
17
- def __init__ (self , max_samples_per_context = 0 ):
18
+ def __init__ (self , max_samples_per_context = 0 , cardinality = None ):
18
19
self .max_samples_per_context = max_samples_per_context
19
20
self .metrics_map = {
20
21
MetricType .COUNT : {},
@@ -31,6 +32,7 @@ def __init__(self, max_samples_per_context=0):
31
32
MetricType .GAUGE : threading .RLock (),
32
33
MetricType .SET : threading .RLock (),
33
34
}
35
+ self ._cardinality = cardinality
34
36
35
37
def flush_aggregated_metrics (self ):
36
38
metrics = []
@@ -58,53 +60,59 @@ def get_context(self, name, tags):
58
60
tags_str = "," .join (tags ) if tags is not None else ""
59
61
return "{}:{}" .format (name , tags_str )
60
62
61
- def count (self , name , value , tags , rate , timestamp = 0 ):
63
+ def count (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
62
64
return self .add_metric (
63
- MetricType .COUNT , CountMetric , name , value , tags , rate , timestamp
65
+ MetricType .COUNT , CountMetric , name , value , tags , rate , timestamp , cardinality
64
66
)
65
67
66
- def gauge (self , name , value , tags , rate , timestamp = 0 ):
68
+ def gauge (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
67
69
return self .add_metric (
68
- MetricType .GAUGE , GaugeMetric , name , value , tags , rate , timestamp
70
+ MetricType .GAUGE , GaugeMetric , name , value , tags , rate , timestamp , cardinality
69
71
)
70
72
71
- def set (self , name , value , tags , rate , timestamp = 0 ):
73
+ def set (self , name , value , tags , rate , timestamp = 0 , cardinality = None ):
72
74
return self .add_metric (
73
- MetricType .SET , SetMetric , name , value , tags , rate , timestamp
75
+ MetricType .SET , SetMetric , name , value , tags , rate , timestamp , cardinality
74
76
)
75
77
76
78
def add_metric (
77
- self , metric_type , metric_class , name , value , tags , rate , timestamp = 0
79
+ self , metric_type , metric_class , name , value , tags , rate , timestamp = 0 , cardinality = None
78
80
):
79
81
context = self .get_context (name , tags )
80
82
with self ._locks [metric_type ]:
81
83
if context in self .metrics_map [metric_type ]:
82
84
self .metrics_map [metric_type ][context ].aggregate (value )
83
85
else :
86
+ if cardinality is None :
87
+ cardinality = self ._cardinality
88
+ validate_cardinality (cardinality )
84
89
self .metrics_map [metric_type ][context ] = metric_class (
85
- name , value , tags , rate , timestamp
90
+ name , value , tags , rate , timestamp , cardinality
86
91
)
87
92
88
- def histogram (self , name , value , tags , rate ):
93
+ def histogram (self , name , value , tags , rate , cardinality = None ):
89
94
return self .add_max_sample_metric (
90
- MetricType .HISTOGRAM , name , value , tags , rate
95
+ MetricType .HISTOGRAM , name , value , tags , rate , cardinality
91
96
)
92
97
93
- def distribution (self , name , value , tags , rate ):
98
+ def distribution (self , name , value , tags , rate , cardinality = None ):
94
99
return self .add_max_sample_metric (
95
- MetricType .DISTRIBUTION , name , value , tags , rate
100
+ MetricType .DISTRIBUTION , name , value , tags , rate , cardinality
96
101
)
97
102
98
- def timing (self , name , value , tags , rate ):
103
+ def timing (self , name , value , tags , rate , cardinality = None ):
99
104
return self .add_max_sample_metric (
100
- MetricType .TIMING , name , value , tags , rate
105
+ MetricType .TIMING , name , value , tags , rate , cardinality
101
106
)
102
107
103
108
def add_max_sample_metric (
104
- self , metric_type , name , value , tags , rate
109
+ self , metric_type , name , value , tags , rate , cardinality = None
105
110
):
106
111
if rate is None :
107
112
rate = 1
108
113
context_key = self .get_context (name , tags )
109
114
metric_context = self .max_sample_metric_map [metric_type ]
110
- return metric_context .sample (name , value , tags , rate , context_key , self .max_samples_per_context )
115
+ if cardinality is None :
116
+ cardinality = self ._cardinality
117
+ validate_cardinality (cardinality )
118
+ return metric_context .sample (name , value , tags , rate , context_key , self .max_samples_per_context , cardinality )
0 commit comments