Skip to content

Commit d3b3d72

Browse files
committed
feat(origindetection): implement cardinality common field
Signed-off-by: Wassim DHIF <[email protected]>
1 parent a7a7594 commit d3b3d72

File tree

6 files changed

+157
-44
lines changed

6 files changed

+157
-44
lines changed

datadog/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def initialize(
4646
statsd_constant_tags=None, # type: Optional[List[str]]
4747
return_raw_response=False, # type: bool
4848
hostname_from_config=True, # type: bool
49+
cardinality=None, # type: Optional[str]
4950
**kwargs # type: Any
5051
):
5152
# type: (...) -> None
@@ -112,6 +113,12 @@ def initialize(
112113
113114
:param hostname_from_config: Set the hostname from the Datadog agent config (agent 5). Will be deprecated
114115
:type hostname_from_config: boolean
116+
117+
:param cardinality: Set the global cardinality for all metrics. \
118+
Possible values are "none", "low", "orchestrator" and "high".
119+
Can also be set via the DATADOG_CARDINALITY or DD_CARDINALITY environment variables.
120+
:type cardinality: string
121+
115122
"""
116123
# API configuration
117124
api._api_key = api_key or api._api_key or os.environ.get("DATADOG_API_KEY", os.environ.get("DD_API_KEY"))
@@ -146,6 +153,9 @@ def initialize(
146153
statsd.disable_buffering = statsd_disable_buffering
147154
api._return_raw_response = return_raw_response
148155

156+
# Set the global cardinality for all metrics
157+
statsd.cardinality = cardinality or os.environ.get("DATADOG_CARDINALITY", os.environ.get("DD_CARDINALITY"))
158+
149159
# HTTP client and API options
150160
for key, value in iteritems(kwargs):
151161
attribute = "_{}".format(key)

datadog/dogstatsd/aggregator.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
SetMetric,
66
)
77
from datadog.dogstatsd.metric_types import MetricType
8+
from datadog.util.format import validate_cardinality
89

910

1011
class 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

Comments
 (0)