Skip to content

Commit e9dcf06

Browse files
authored
Merge pull request #6 from Lightricks/fix/blank_requests
Blank requests: Avoid sending blank metrics.
2 parents 903577a + fe6fc03 commit e9dcf06

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

pyformance/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.5"
1+
__version__ = "1.0.6"

pyformance/reporters/influx.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ def report_now(self, registry=None, timestamp=None):
8989
self._create_database()
9090
timestamp = timestamp or int(round(self.clock.time()))
9191
metrics = (registry or self.registry).dump_metrics(key_is_metric=True)
92-
post_data = "\n".join(self._get_influx_protocol_lines(metrics, timestamp))
93-
url = self._get_url()
94-
self._try_send(url, post_data)
92+
93+
influx_lines = self._get_influx_protocol_lines(metrics, timestamp)
94+
# If you don't have anything nice to say than don't say nothing
95+
if influx_lines:
96+
post_data = "\n".join(influx_lines)
97+
url = self._get_url()
98+
self._try_send(url, post_data)
9599

96100
def _get_table_name(self, metric_key):
97101
if not self.prefix:

tests/test__influx_reporter.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ def setUp(self):
2121
def tearDown(self):
2222
super(TestInfluxReporter, self).tearDown()
2323

24-
def test_report_now(self):
24+
def test_not_called_on_blank(self):
2525
influx_reporter = InfluxReporter(registry=self.registry)
2626

2727
with mock.patch("pyformance.reporters.influx.urlopen") as patch:
2828
influx_reporter.report_now()
29-
patch.assert_called()
29+
patch.assert_not_called()
3030

3131
def test_create_database(self):
3232
r1 = InfluxReporter(registry=self.registry, autocreate_database=True)
3333
with mock.patch("pyformance.reporters.influx.urlopen") as patch:
3434
r1.report_now()
35-
if patch.call_count != 2:
35+
if patch.call_count != 1:
3636
raise AssertionError(
37-
"Expected 2 calls to 'urlopen'. Received: {}".format(
37+
"Expected 1 calls to 'urlopen'. Received: {}".format(
3838
patch.call_count
3939
)
4040
)
@@ -47,8 +47,7 @@ def test_gauge_without_tags(self):
4747
autocreate_database=False
4848
)
4949

50-
with mock.patch.object(influx_reporter, "_try_send",
51-
wraps=influx_reporter._try_send) as send_mock:
50+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
5251
influx_reporter.report_now()
5352

5453
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -64,8 +63,7 @@ def test_gauge_with_tags(self):
6463
autocreate_database=False
6564
)
6665

67-
with mock.patch.object(influx_reporter, "_try_send",
68-
wraps=influx_reporter._try_send) as send_mock:
66+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
6967
influx_reporter.report_now()
7068

7169
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -83,8 +81,7 @@ def test_gauge_with_global_tags(self):
8381
global_tags={"stage": "dev", "region": "override"}
8482
)
8583

86-
with mock.patch.object(influx_reporter, "_try_send",
87-
wraps=influx_reporter._try_send) as send_mock:
84+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
8885
influx_reporter.report_now()
8986

9087
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -105,8 +102,7 @@ def test_counter_with_tags(self):
105102
autocreate_database=False
106103
)
107104

108-
with mock.patch.object(influx_reporter, "_try_send",
109-
wraps=influx_reporter._try_send) as send_mock:
105+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
110106
influx_reporter.report_now()
111107

112108
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -127,8 +123,7 @@ def test_events_with_tags(self):
127123
autocreate_database=False
128124
)
129125

130-
with mock.patch.object(influx_reporter, "_try_send",
131-
wraps=influx_reporter._try_send) as send_mock:
126+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
132127
influx_reporter.report_now()
133128

134129
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -152,8 +147,7 @@ def test_combined_events_with_counter(self):
152147
autocreate_database=False
153148
)
154149

155-
with mock.patch.object(influx_reporter, "_try_send",
156-
wraps=influx_reporter._try_send) as send_mock:
150+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
157151
influx_reporter.report_now()
158152

159153
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"
@@ -177,8 +171,7 @@ def test_count_calls_with_tags(self):
177171
autocreate_database=False
178172
)
179173

180-
with mock.patch.object(influx_reporter, "_try_send",
181-
wraps=influx_reporter._try_send) as send_mock:
174+
with mock.patch.object(influx_reporter, "_try_send") as send_mock:
182175
influx_reporter.report_now()
183176

184177
expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s"

0 commit comments

Comments
 (0)