Skip to content

Commit 768c5ee

Browse files
typing: add typing for dogstatsd
1 parent 156114e commit 768c5ee

11 files changed

Lines changed: 193 additions & 57 deletions

File tree

‎datadog/__init__.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# stdlib
1414
import logging
1515
import os
16-
import os.path
17-
from typing import Any, List, Optional
16+
from typing import Any, List, Optional # noqa: F401
1817

1918
# datadog
2019
from datadog import api
@@ -43,7 +42,7 @@ def initialize(
4342
statsd_use_default_route=False, # type: bool
4443
statsd_socket_path=None, # type: Optional[str]
4544
statsd_namespace=None, # type: Optional[str]
46-
statsd_max_samples_per_context=0, # type: Optional[int]
45+
statsd_max_samples_per_context=0, # type: int
4746
statsd_constant_tags=None, # type: Optional[List[str]]
4847
return_raw_response=False, # type: bool
4948
hostname_from_config=True, # type: bool

‎datadog/dogstatsd/aggregator.py‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import threading
2+
from typing import Any, Dict, List, Optional
3+
24
from datadog.dogstatsd.metrics import (
35
CountMetric,
46
GaugeMetric,
57
SetMetric,
8+
MetricAggregator,
69
)
710
from datadog.dogstatsd.max_sample_metric import (
811
HistogramMetric,
912
DistributionMetric,
10-
TimingMetric
13+
TimingMetric,
1114
)
1215
from datadog.dogstatsd.metric_types import MetricType
1316
from datadog.dogstatsd.max_sample_metric_context import MaxSampleMetricContexts
@@ -16,12 +19,13 @@
1619

1720
class Aggregator(object):
1821
def __init__(self, max_samples_per_context=0, cardinality=None):
22+
# type: (int, Optional[str]) -> None
1923
self.max_samples_per_context = max_samples_per_context
2024
self.metrics_map = {
2125
MetricType.COUNT: {},
2226
MetricType.GAUGE: {},
2327
MetricType.SET: {},
24-
}
28+
} # type: Dict[str, Dict[str, MetricAggregator]]
2529
self.max_sample_metric_map = {
2630
MetricType.HISTOGRAM: MaxSampleMetricContexts(HistogramMetric),
2731
MetricType.DISTRIBUTION: MaxSampleMetricContexts(DistributionMetric),
@@ -35,7 +39,8 @@ def __init__(self, max_samples_per_context=0, cardinality=None):
3539
self.cardinality = cardinality
3640

3741
def flush_aggregated_metrics(self):
38-
metrics = []
42+
# type: () -> List[MetricAggregator]
43+
metrics = [] # type: List[MetricAggregator]
3944
for metric_type in self.metrics_map.keys():
4045
with self._locks[metric_type]:
4146
current_metrics = self.metrics_map[metric_type]
@@ -46,38 +51,45 @@ def flush_aggregated_metrics(self):
4651
return metrics
4752

4853
def set_max_samples_per_context(self, max_samples_per_context=0):
54+
# type: (int) -> None
4955
self.max_samples_per_context = max_samples_per_context
5056

5157
def flush_aggregated_sampled_metrics(self):
52-
metrics = []
58+
# type: () -> List[MetricAggregator]
59+
metrics = [] # type: List[MetricAggregator]
5360
for metric_type in self.max_sample_metric_map.keys():
5461
metric_context = self.max_sample_metric_map[metric_type]
5562
for metricList in metric_context.flush():
5663
metrics.extend(metricList)
5764
return metrics
5865

5966
def get_context(self, name, tags):
67+
# type: (str, Optional[List[str]]) -> str
6068
tags_str = u",".join(tags) if tags is not None else ""
6169
return u"{}:{}".format(name, tags_str)
6270

6371
def count(self, name, value, tags, rate, timestamp=0, cardinality=None):
72+
# type: (str, Any, Optional[List[str]], Optional[float], int, Optional[str]) -> None
6473
return self.add_metric(
6574
MetricType.COUNT, CountMetric, name, value, tags, rate, timestamp, cardinality
6675
)
6776

6877
def gauge(self, name, value, tags, rate, timestamp=0, cardinality=None):
78+
# type: (str, Any, Optional[List[str]], Optional[float], int, Optional[str]) -> None
6979
return self.add_metric(
7080
MetricType.GAUGE, GaugeMetric, name, value, tags, rate, timestamp, cardinality
7181
)
7282

7383
def set(self, name, value, tags, rate, timestamp=0, cardinality=None):
84+
# type: (str, Any, Optional[List[str]], Optional[float], int, Optional[str]) -> None
7485
return self.add_metric(
7586
MetricType.SET, SetMetric, name, value, tags, rate, timestamp, cardinality
7687
)
7788

7889
def add_metric(
7990
self, metric_type, metric_class, name, value, tags, rate, timestamp=0, cardinality=None
8091
):
92+
# type: (str, Any, str, Any, Optional[List[str]], Optional[float], int, Optional[str]) -> None
8193
context = self.get_context(name, tags)
8294
with self._locks[metric_type]:
8395
if context in self.metrics_map[metric_type]:
@@ -91,23 +103,27 @@ def add_metric(
91103
)
92104

93105
def histogram(self, name, value, tags, rate, cardinality=None):
106+
# type: (str, Any, Optional[List[str]], Optional[float], Optional[str]) -> None
94107
return self.add_max_sample_metric(
95108
MetricType.HISTOGRAM, name, value, tags, rate, cardinality
96109
)
97110

98111
def distribution(self, name, value, tags, rate, cardinality=None):
112+
# type: (str, Any, Optional[List[str]], Optional[float], Optional[str]) -> None
99113
return self.add_max_sample_metric(
100114
MetricType.DISTRIBUTION, name, value, tags, rate, cardinality
101115
)
102116

103117
def timing(self, name, value, tags, rate, cardinality=None):
118+
# type: (str, Any, Optional[List[str]], Optional[float], Optional[str]) -> None
104119
return self.add_max_sample_metric(
105120
MetricType.TIMING, name, value, tags, rate, cardinality
106121
)
107122

108123
def add_max_sample_metric(
109124
self, metric_type, name, value, tags, rate, cardinality=None
110125
):
126+
# type: (str, str, Any, Optional[List[str]], Optional[float], Optional[str]) -> None
111127
if rate is None:
112128
rate = 1
113129
context_key = self.get_context(name, tags)

0 commit comments

Comments
 (0)