def test_env_var_values(self): with EnvVars( env_vars={ "DATADOG_API_KEY": "API_KEY_ENV", "DATADOG_APP_KEY": "APP_KEY_ENV", "DATADOG_HOST": "HOST_ENV", }): initialize() self.assertEqual(api._api_key, "API_KEY_ENV") self.assertEqual(api._application_key, "APP_KEY_ENV") self.assertEqual(api._api_host, "HOST_ENV") self.assertEqual( api._host_name, util.hostname.get_hostname(api._hostname_from_config)) del os.environ["DATADOG_API_KEY"] del os.environ["DATADOG_APP_KEY"] del os.environ["DATADOG_HOST"] with EnvVars(env_vars={ "DD_API_KEY": "API_KEY_ENV_DD", "DD_APP_KEY": "APP_KEY_ENV_DD", }): api._api_key = None api._application_key = None initialize() self.assertEqual(api._api_key, "API_KEY_ENV_DD") self.assertEqual(api._application_key, "APP_KEY_ENV_DD")
def test_default_values(self): with EnvVars(ignore=[ "DATADOG_API_KEY", "DATADOG_APP_KEY", "DD_API_KEY", "DD_APP_KEY" ]): initialize() self.assertIsNone(api._api_key) self.assertIsNone(api._application_key) self.assertEqual(api._api_host, "https://api.datadoghq.com") self.assertEqual(api._host_name, util.hostname.get_hostname())
def test_dogstatsd_initialization_with_dd_env_service_version(self): """ Dogstatsd should automatically use DD_ENV, DD_SERVICE, and DD_VERSION (if present) to set {env, service, version} as global tags for all metrics emitted. """ cases = [ # Test various permutations of setting DD_* env vars, as well as other global tag configuration. # An empty string signifies that the env var either isn't set or that it is explicitly set to empty string. ('', '', '', '', [], []), ('prod', '', '', '', [], ['env:prod']), ('prod', 'dog', '', '', [], ['env:prod', 'service:dog']), ('prod', 'dog', 'abc123', '', [], ['env:prod', 'service:dog', 'version:abc123']), ('prod', 'dog', 'abc123', 'env:prod,type:app', [], ['env:prod', 'env:prod', 'service:dog', 'type:app', 'version:abc123']), ('prod', 'dog', 'abc123', 'env:prod2,type:app', [], ['env:prod', 'env:prod2', 'service:dog', 'type:app', 'version:abc123']), ('prod', 'dog', 'abc123', '', ['env:prod', 'type:app'], ['env:prod', 'env:prod', 'service:dog', 'type:app', 'version:abc123']), ('prod', 'dog', 'abc123', '', ['env:prod2', 'type:app'], ['env:prod', 'env:prod2', 'service:dog', 'type:app', 'version:abc123']), ('prod', 'dog', 'abc123', 'env:prod3,custom_tag:cat', ['env:prod2', 'type:app'], ['custom_tag:cat', 'env:prod', 'env:prod2', 'env:prod3', 'service:dog', 'type:app', 'version:abc123']), ] for case in cases: dd_env, dd_service, dd_version, datadog_tags, constant_tags, global_tags = case with EnvVars( env_vars={ 'DATADOG_TAGS': datadog_tags, 'DD_ENV': dd_env, 'DD_SERVICE': dd_service, 'DD_VERSION': dd_version, } ): dogstatsd = DogStatsd(constant_tags=constant_tags, telemetry_min_flush_interval=0) dogstatsd.socket = FakeSocket() # Guarantee consistent ordering, regardless of insertion order. dogstatsd.constant_tags.sort() self.assertEqual(global_tags, dogstatsd.constant_tags) # Make call with no tags passed; only the globally configured tags will be used. global_tags_str = ','.join([t for t in global_tags]) dogstatsd.gauge('gt', 123.4) # Protect against the no tags case. metric = 'gt:123.4|g|#{}'.format(global_tags_str) if global_tags_str else 'gt:123.4|g' self.assertEqual(metric, dogstatsd.socket.recv()) self.assertEqual(telemetry_metrics(tags=global_tags_str, bytes_sent=len(metric)), dogstatsd.socket.recv()) dogstatsd._reset_telemetry() # Make another call with local tags passed. passed_tags = ['env:prod', 'version:def456', 'custom_tag:toad'] all_tags_str = ','.join([t for t in passed_tags + global_tags]) dogstatsd.gauge('gt', 123.4, tags=passed_tags) metric = 'gt:123.4|g|#{}'.format(all_tags_str) self.assertEqual(metric, dogstatsd.socket.recv()) self.assertEqual(telemetry_metrics(tags=global_tags_str, bytes_sent=len(metric)), dogstatsd.socket.recv())
def test_precedence(self): # Initialize first with env vars with EnvVars(env_vars={ "DD_API_KEY": "API_KEY_ENV_DD", "DD_APP_KEY": "APP_KEY_ENV_DD", }): os.environ["DATADOG_API_KEY"] = "API_KEY_ENV" os.environ["DATADOG_APP_KEY"] = "APP_KEY_ENV" os.environ["DATADOG_HOST"] = "HOST_ENV" initialize() self.assertEqual(api._api_key, "API_KEY_ENV") self.assertEqual(api._application_key, "APP_KEY_ENV") self.assertEqual(api._api_host, "HOST_ENV") self.assertEqual( api._host_name, util.hostname.get_hostname(api._hostname_from_config)) # Initialize again to check given parameters take precedence over already set value and env vars initialize(api_key="API_KEY", app_key="APP_KEY", api_host="HOST", host_name="HOSTNAME") self.assertEqual(api._api_key, "API_KEY") self.assertEqual(api._application_key, "APP_KEY") self.assertEqual(api._api_host, "HOST") self.assertEqual(api._host_name, "HOSTNAME") # Initialize again without specifying attributes to check that already initialized value takes precedence initialize() self.assertEqual(api._api_key, "API_KEY") self.assertEqual(api._application_key, "APP_KEY") self.assertEqual(api._api_host, "HOST") self.assertEqual(api._host_name, "HOSTNAME") del os.environ["DATADOG_API_KEY"] del os.environ["DATADOG_APP_KEY"] del os.environ["DATADOG_HOST"]
def test_tags_from_environment_env_service_version(self): test_tags = set(['env:staging', 'service:food', 'version:1.2.3']) with EnvVars(env_vars={ "DD_ENV": "staging", "DD_VERSION": "1.2.3", "DD_SERVICE": "food", }): dog = ThreadStats() dog.start(roll_up_interval=10, flush_in_thread=False) reporter = dog.reporter = MemoryReporter() # Add two events event1_title = "Event 1 title" event1_text = "Event 1 text" dog.event(event1_title, event1_text) # Flush and test dog.flush() [event1] = reporter.events assert event1['title'] == event1_title assert event1['text'] == event1_text assert set(event1['tags']) == test_tags