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_tags_of_metric_take_priority(): """ Test that metric tags take priority """ mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token", APPTUIT_PY_TAGS: 'host: environ, ip: 1.1.1.1'}) mock_environ.start() registry = MetricsRegistry() reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"}) counter = registry.counter('counter {"host": "metric", "ip": "3.3.3.3"}') 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": "metric", "ip": "3.3.3.3"}) mock_environ.stop()
def test_env_tags_with_host_tag_disabled_param(): """ Test global tags from environment when host tag is disabled through the disable_host_tag parameter to the reporter """ token = "test_token" disable_host_tag_values = [True, False, None] test_cases = [ (" ", None), ("tagk1: 22, tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2"}), ("tagk1: 22, , tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2"}), (" tagk1 : 22,,tagk2 : tagv2 ", {"tagk1": "22", "tagk2": "tagv2"}), (",tagk1: 22, tagk2: tagv2,", {"tagk1": "22", "tagk2": "tagv2"}), (", , , , tagk1: 22, tagk2: tagv2, , , , ,", {"tagk1": "22", "tagk2": "tagv2"}), ("tagk1: tagv1, tagk2: tagv2, host: myhost", {"tagk1": "tagv1", "tagk2": "tagv2", "host": "myhost"}) ] for env_tags_value, expected_global_tags in test_cases: for disable_value in disable_host_tag_values: mock_environ = patch.dict(os.environ, {APPTUIT_PY_TAGS: env_tags_value}) mock_environ.start() reporter = ApptuitReporter(token=token, disable_host_tag=disable_value) if disable_value is None or disable_value is False: if expected_global_tags: if "host" not in expected_global_tags: expected_global_tags["host"] = socket.gethostname() else: expected_global_tags = {"host": socket.gethostname()} assert_equals(reporter.tags, expected_global_tags) mock_environ.stop()
def test_deprecated_token_variable(): """ Test that reporter and client work with the deprecated token env variable """ warnings.filterwarnings('error') with patch.dict(os.environ, {DEPRECATED_APPTUIT_PY_TOKEN: "test_token"}): registry = MetricsRegistry() with assert_raises(DeprecationWarning): ApptuitReporter(registry=registry, tags={'host': 'reporter'}) warnings.resetwarnings()
def test_reporter_tags_with_global_env_tags(): """ Test that reporter tags take priority TODO: We have 8 possible combinations - 1. global env tags: true, reporter tags: true, metric tags: true 2. global env tags: true, reporter tags: true, metric tags: false 3. global env tags: true, reporter tags: false, metric tags: true 4. global env tags: true, reporter tags: false, metric tags: false 5. global env tags: false, reporter tags: true, metric tags: true 6. global env tags: false, reporter tags: true, metric tags: false 7. global env tags: false, reporter tags: false, metric tags: true 8. global env tags: false, reporter tags: false, metric tags: false """ mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token", APPTUIT_PY_TAGS: 'host: environ, ip: 1.1.1.1'}) mock_environ.start() 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'}) reporter = ApptuitReporter(registry=registry) 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": "environ", "ip": "1.1.1.1"}) mock_environ.stop()
def test_env_tags_with_host_tag_disabled_env(): """ Test global tags from environment when host tag is disabled through the APPTUIT_DISABLE_HOST_TAG variable """ token = "test_token" disable_host_tag_true_values = ["True", "true"] disable_host_tag_other_values = ["False", "false"] test_cases = [ (" ", None), ("tagk1: 22, tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2"}), ("tagk1: 22, , tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2"}), (" tagk1 : 22,,tagk2 : tagv2 ", {"tagk1": "22", "tagk2": "tagv2"}), (",tagk1: 22, tagk2: tagv2,", {"tagk1": "22", "tagk2": "tagv2"}), (", , , , tagk1: 22, tagk2: tagv2, , , , ,", {"tagk1": "22", "tagk2": "tagv2"}), ("tagk1: tagv1, tagk2: tagv2, host: myhost", {"tagk1": "tagv1", "tagk2": "tagv2", "host": "myhost"}) ] for env_tags_value, expected_global_tags in test_cases: for disable_value in disable_host_tag_true_values: mock_environ = patch.dict(os.environ, {APPTUIT_PY_TAGS: env_tags_value, DISABLE_HOST_TAG: disable_value}) mock_environ.start() reporter = ApptuitReporter(token=token) assert_equals(reporter.tags, expected_global_tags) mock_environ.stop() for disable_value in disable_host_tag_other_values: mock_environ = patch.dict(os.environ, {APPTUIT_PY_TAGS: env_tags_value, DISABLE_HOST_TAG: disable_value}) expected_global_tags = expected_global_tags.copy() if expected_global_tags else {} if "host" not in expected_global_tags: expected_global_tags["host"] = socket.gethostname() mock_environ.start() reporter = ApptuitReporter(token=token) assert_equals(reporter.tags, expected_global_tags) mock_environ.stop()
def test_tags_env_variable_parsing_positive(): """ Test that we are able to parse the global tags from environment variable """ host = socket.gethostname() token = "test_token" test_cases = [ (" ", {"host": host}), ("tagk1: 22, tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2", "host": host}), ("tagk1: 22, , tagk2: tagv2", {"tagk1": "22", "tagk2": "tagv2", "host": host}), (" tagk1 : 22,,tagk2 : tagv2 ", {"tagk1": "22", "tagk2": "tagv2", "host": host}), (",tagk1: 22, tagk2: tagv2,", {"tagk1": "22", "tagk2": "tagv2", "host": host}), (", , , , tagk1: 22, tagk2: tagv2, , , , ,", {"tagk1": "22", "tagk2": "tagv2", "host": host}), ("tagk1: tagv1, tagk2: tagv2, host: myhost", {"tagk1": "tagv1", "tagk2": "tagv2", "host": "myhost"}) ] for env_tags_value, expected_global_tags in test_cases: mock_environ = patch.dict(os.environ, {APPTUIT_PY_TAGS: env_tags_value}) mock_environ.start() reporter = ApptuitReporter(token=token) assert_equals(reporter.tags, expected_global_tags) mock_environ.stop()