def test_no_environ_tags(): """ Test tags work even if no global tags present as env variable """ timestamp = int(time.time()) test_val = math.pi mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token"}) mock_environ.start() client = Apptuit() dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1}, timestamp=timestamp, value=test_val) dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val) payload = client._create_payload_from_datapoints([dp1, dp2]) assert_equals(len(payload), 2) assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1}) assert_equals(payload[1]["tags"], {"test": 2}) registry = MetricsRegistry() reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"}) counter = registry.counter("counter") counter.inc(1) payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry)) assert_equals(len(payload), 1) assert_equals(payload[0]["tags"], {'host': 'reporter', 'ip': '2.2.2.2'}) mock_environ.stop()
def test_datapoint_tags_and_envtags(): """ Test that datapoint tags take priority when global tags env variable is present """ mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token", APPTUIT_PY_TAGS: 'host: host1, ip: 1.1.1.1'}) mock_environ.start() client = Apptuit(None) timestamp = int(time.time()) test_val = math.pi dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1}, timestamp=timestamp, value=test_val) dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val) dp3 = DataPoint(metric="test_metric", tags={}, timestamp=timestamp, value=test_val) dp4 = DataPoint(metric="test_metric", tags=None, timestamp=timestamp, value=test_val) payload = client._create_payload_from_datapoints([dp1, dp2, dp3, dp4]) assert_equals(len(payload), 4) assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1}) assert_equals(payload[1]["tags"], {"host": "host1", "ip": "1.1.1.1", "test": 2}) assert_equals(payload[2]["tags"], {"host": "host1", "ip": "1.1.1.1"}) assert_equals(payload[3]["tags"], {"host": "host1", "ip": "1.1.1.1"}) assert_equals(client._global_tags, {"host": "host1", "ip": "1.1.1.1"}) mock_environ.stop()