Skip to content

Commit 9bfa236

Browse files
committed
feat(origindetection): implement cardinality common field
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
1 parent a7a7594 commit 9bfa236

File tree

5 files changed

+127
-41
lines changed

5 files changed

+127
-41
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,29 @@ def get_context(self, name, tags):
3434
tags_str = ",".join(tags) if tags is not None else ""
3535
return "{}:{}".format(name, tags_str)
3636

37-
def count(self, name, value, tags, rate, timestamp=0):
37+
def count(self, name, value, tags, rate, timestamp=0, cardinality=None):
3838
return self.add_metric(
39-
MetricType.COUNT, CountMetric, name, value, tags, rate, timestamp
39+
MetricType.COUNT, CountMetric, name, value, tags, rate, timestamp, cardinality
4040
)
4141

42-
def gauge(self, name, value, tags, rate, timestamp=0):
42+
def gauge(self, name, value, tags, rate, timestamp=0, cardinality=None):
4343
return self.add_metric(
44-
MetricType.GAUGE, GaugeMetric, name, value, tags, rate, timestamp
44+
MetricType.GAUGE, GaugeMetric, name, value, tags, rate, timestamp, cardinality
4545
)
4646

47-
def set(self, name, value, tags, rate, timestamp=0):
47+
def set(self, name, value, tags, rate, timestamp=0, cardinality=None):
4848
return self.add_metric(
49-
MetricType.SET, SetMetric, name, value, tags, rate, timestamp
49+
MetricType.SET, SetMetric, name, value, tags, rate, timestamp, cardinality
5050
)
5151

5252
def add_metric(
53-
self, metric_type, metric_class, name, value, tags, rate, timestamp=0
53+
self, metric_type, metric_class, name, value, tags, rate, timestamp=0, cardinality=None
5454
):
5555
context = self.get_context(name, tags)
5656
with self._locks[metric_type]:
5757
if context in self.metrics_map[metric_type]:
5858
self.metrics_map[metric_type][context].aggregate(value)
5959
else:
6060
self.metrics_map[metric_type][context] = metric_class(
61-
name, value, tags, rate, timestamp
61+
name, value, tags, rate, timestamp, cardinality
6262
)

0 commit comments

Comments
 (0)