def test_dd_service_mapping(self):
        c = Config()
        assert c.service_mapping == {}

        with override_env(dict(DD_SERVICE_MAPPING="foobar:bar,snafu:foo")):
            c = Config()
            assert c.service_mapping == {"foobar": "bar", "snafu": "foo"}
Esempio n. 2
0
    def test_environment_analytics_enabled(self):
        # default
        self.assertFalse(self.config.analytics_enabled)
        self.assertIsNone(self.config.foo.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED='True')):
            config = Config()
            self.assertTrue(config.analytics_enabled)
            self.assertIsNone(config.foo.analytics_enabled)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED='True')):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 1.0)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED='False')):
            config = Config()
            self.assertFalse(config.foo.analytics_enabled)

        with self.override_env(
                dict(DD_FOO_ANALYTICS_ENABLED='True',
                     DD_FOO_ANALYTICS_SAMPLE_RATE='0.5')):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 0.5)
    def test_get_analytics_sample_rate(self):
        """" Check method for accessing sample rate based on configuration """
        ic = IntegrationConfig(self.config, "foo", analytics_enabled=True, analytics_sample_rate=0.5)
        self.assertEqual(ic.get_analytics_sample_rate(), 0.5)

        ic = IntegrationConfig(self.config, "foo", analytics_enabled=True)
        self.assertEqual(ic.get_analytics_sample_rate(), 1.0)

        ic = IntegrationConfig(self.config, "foo", analytics_enabled=False)
        self.assertIsNone(ic.get_analytics_sample_rate())

        with self.override_env(dict(DD_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertEqual(ic.get_analytics_sample_rate(use_global_config=True), 1.0)

        with self.override_env(dict(DD_TRACE_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertEqual(ic.get_analytics_sample_rate(use_global_config=True), 1.0)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertIsNone(ic.get_analytics_sample_rate(use_global_config=True))

        with self.override_env(dict(DD_TRACE_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertIsNone(ic.get_analytics_sample_rate(use_global_config=True))
Esempio n. 4
0
    def test_environment_analytics_enabled(self):
        with self.override_env(dict(DD_ANALYTICS_ENABLED='True')):
            config = Config()
            self.assertTrue(config.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED='False')):
            config = Config()
            self.assertFalse(config.analytics_enabled)
    def test_logs_injection(self):
        with self.override_env(dict(DD_LOGS_INJECTION="True")):
            config = Config()
            self.assertTrue(config.logs_injection)

        with self.override_env(dict(DD_LOGS_INJECTION="false")):
            config = Config()
            self.assertFalse(config.logs_injection)
    def test_service(self):
        # If none is provided the default should be ``None``
        with self.override_env(dict()):
            config = Config()
            self.assertEqual(config.service, None)

        with self.override_env(dict(DD_SERVICE="my-service")):
            config = Config()
            self.assertEqual(config.service, "my-service")
Esempio n. 7
0
    def test_dd_version(self):
        c = Config()
        assert c.version is None

        with override_env(dict(DD_VERSION="1.2.3")):
            c = Config()
            assert c.version == "1.2.3"

            c.version = "4.5.6"
            assert c.version == "4.5.6"
    def test_allow_exist_both_global_and_integration_config(self):
        global_config = Config()
        integration_config = IntegrationConfig(global_config)

        global_config.trace_headers('global_header')
        assert integration_config.header_is_traced('global_header')

        integration_config.http.trace_headers('integration_header')
        assert integration_config.header_is_traced('integration_header')
        assert not integration_config.header_is_traced('global_header')
        assert not global_config.header_is_traced('integration_header')
Esempio n. 9
0
    def test_dd_env(self):
        c = Config()
        assert c.env is None

        with override_env(dict(DD_ENV="prod")):
            c = Config()
            assert c.env == "prod"

            # manual override still possible
            c.env = "prod-staging"
            assert c.env == "prod-staging"
Esempio n. 10
0
def test_environment_header_tags():
    with override_env(
            dict(
                DD_TRACE_HEADER_TAGS="Host:http.host,User-agent:http.user_agent"
            )):
        config = Config()

    assert config.http.is_header_tracing_configured
    assert config._header_tag_name("Host") == "http.host"
    assert config._header_tag_name("User-agent") == "http.user_agent"
    # Case insensitive
    assert config._header_tag_name("User-Agent") == "http.user_agent"
Esempio n. 11
0
def test_config_is_header_tracing_configured(global_headers, int_headers,
                                             expected):
    config = Config()
    integration_config = config.myint

    if global_headers is not None:
        config.trace_headers(global_headers)
    if int_headers is not None:
        integration_config.http.trace_headers(int_headers)

    assert (
        config.http.is_header_tracing_configured,
        integration_config.http.is_header_tracing_configured,
        integration_config.is_header_tracing_configured,
    ) == expected
Esempio n. 12
0
    def test_allow_attr_access(self):
        config = IntegrationConfig(Config())
        config.setting = 'value'

        # Can be accessed both as item and attr accessor
        assert config.setting == 'value'
        assert config['setting'] == 'value'
Esempio n. 13
0
    def test_environment_analytics_overrides(self):
        with self.override_env(dict(DD_ANALYTICS_ENABLED="False", DD_TRACE_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="False", DD_TRACE_ANALYTICS_ENABLED="False")):
            config = Config()
            self.assertFalse(config.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="True", DD_TRACE_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="True", DD_TRACE_ANALYTICS_ENABLED="False")):
            config = Config()
            self.assertFalse(config.analytics_enabled)
Esempio n. 14
0
    def test_allow_both_access(self):
        config = IntegrationConfig(Config())

        config.setting = 'value'
        assert config['setting'] == 'value'
        assert config.setting == 'value'

        config['setting'] = 'new-value'
        assert config.setting == 'new-value'
        assert config['setting'] == 'new-value'
Esempio n. 15
0
class GlobalConfigTestCase(TestCase):
    """Test the `Configuration` class that stores integration settings"""
    def setUp(self):
        self.config = Config()

    def test_registration(self):
        # ensure an integration can register a new list of settings
        settings = {
            'distributed_tracing': True,
        }
        self.config._add('requests', settings)
        ok_(self.config.requests['distributed_tracing'] is True)

    def test_settings_copy(self):
        # ensure that once an integration is registered, a copy
        # of the settings is stored to avoid side-effects
        experimental = {
            'request_enqueuing': True,
        }
        settings = {
            'distributed_tracing': True,
            'experimental': experimental,
        }
        self.config._add('requests', settings)

        settings['distributed_tracing'] = False
        experimental['request_enqueuing'] = False
        ok_(self.config.requests['distributed_tracing'] is True)
        ok_(self.config.requests['experimental']['request_enqueuing'] is True)

    def test_missing_integration(self):
        # ensure a meaningful exception is raised when an integration
        # that is not available is retrieved in the configuration
        # object
        with assert_raises(ConfigException) as e:
            self.config.new_integration['some_key']

        ok_(isinstance(e.exception, ConfigException))

    def test_global_configuration(self):
        # ensure a global configuration is available in the `ddtrace` module
        ok_(isinstance(global_config, Config))
Esempio n. 16
0
    def test_get_analytics_sample_rate_deprecated_name(self):
        """Check method for accessing sample rate based on configuration"""
        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertEqual(ic.get_analytics_sample_rate(), 1.0)

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertEqual(ic.get_analytics_sample_rate(), 1.0)

        with self.override_env(
                dict(DD_FOO_ANALYTICS_ENABLED="True",
                     DD_FOO_ANALYTICS_SAMPLE_RATE="0.5")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertEqual(ic.get_analytics_sample_rate(), 0.5)

        with self.override_env(
                dict(DD_FOO_ANALYTICS_ENABLED="True",
                     DD_TRACE_FOO_ANALYTICS_SAMPLE_RATE="0.5")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertEqual(ic.get_analytics_sample_rate(), 0.5)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertIsNone(ic.get_analytics_sample_rate())

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "bar", _deprecated_name="foo")
            self.assertIsNone(ic.get_analytics_sample_rate())
Esempio n. 17
0
    def test_http_config(self):
        config = Config()
        config._add("django", dict())
        assert config.django.trace_query_string is None
        config.http.trace_query_string = True
        assert config.http.trace_query_string is True
        assert config.django.trace_query_string is True

        # Integration usage
        config = Config()
        config._add("django", dict())
        config.django.http.trace_query_string = True
        assert config.http.trace_query_string is None
        assert config.django.trace_query_string is True
        assert config.django.http.trace_query_string is True
Esempio n. 18
0
    def test_dd_env(self):
        c = Config()
        assert c.env is None

        with override_env(dict(DD_ENV="prod")):
            c = Config()
            assert c.env == "prod"

            # manual override still possible
            c.env = "prod-staging"
            assert c.env == "prod-staging"

        # between DD_ENV and DATADOG_ENV, the former takes priority
        with override_env(dict(DATADOG_ENV="prod", DD_ENV="prod-staging")):
            c = Config()
            assert c.env == "prod-staging"
Esempio n. 19
0
 def test_is_a_dict(self):
     integration_config = IntegrationConfig(Config())
     assert isinstance(integration_config, dict)
Esempio n. 20
0
 def config(self):
     yield Config()
def config():
    c = Config()
    c._add("myint", dict())
    return c
Esempio n. 22
0
 def setUp(self):
     self.config = Config()
Esempio n. 23
0
class GlobalConfigTestCase(TestCase):
    """Test the `Configuration` class that stores integration settings"""
    def setUp(self):
        self.config = Config()
        self.tracer = get_dummy_tracer()

    def test_registration(self):
        # ensure an integration can register a new list of settings
        settings = {
            'distributed_tracing': True,
        }
        self.config._add('requests', settings)
        assert self.config.requests['distributed_tracing'] is True

    def test_settings_copy(self):
        # ensure that once an integration is registered, a copy
        # of the settings is stored to avoid side-effects
        experimental = {
            'request_enqueuing': True,
        }
        settings = {
            'distributed_tracing': True,
            'experimental': experimental,
        }
        self.config._add('requests', settings)

        settings['distributed_tracing'] = False
        experimental['request_enqueuing'] = False
        assert self.config.requests['distributed_tracing'] is True
        assert self.config.requests['experimental']['request_enqueuing'] is True

    def test_missing_integration_key(self):
        # ensure a meaningful exception is raised when an integration
        # that is not available is retrieved in the configuration
        # object
        with pytest.raises(KeyError) as e:
            self.config.new_integration['some_key']

        assert isinstance(e.value, KeyError)

    def test_global_configuration(self):
        # ensure a global configuration is available in the `ddtrace` module
        assert isinstance(global_config, Config)

    def test_settings_merge(self):
        """
        When calling `config._add()`
            when existing settings exist
                we do not overwrite the existing settings
        """
        self.config.requests['split_by_domain'] = True
        self.config._add('requests', dict(split_by_domain=False))
        assert self.config.requests['split_by_domain'] is True

    def test_settings_overwrite(self):
        """
        When calling `config._add(..., merge=False)`
            when existing settings exist
                we overwrite the existing settings
        """
        self.config.requests['split_by_domain'] = True
        self.config._add('requests', dict(split_by_domain=False), merge=False)
        assert self.config.requests['split_by_domain'] is False

    def test_settings_merge_deep(self):
        """
        When calling `config._add()`
            when existing "deep" settings exist
                we do not overwrite the existing settings
        """
        self.config.requests['a'] = dict(
            b=dict(
                c=True,
            ),
        )
        self.config._add('requests', dict(
            a=dict(
                b=dict(
                    c=False,
                    d=True,
                ),
            ),
        ))
        assert self.config.requests['a']['b']['c'] is True
        assert self.config.requests['a']['b']['d'] is True

    def test_settings_hook(self):
        """
        When calling `Hooks._emit()`
            When there is a hook registered
                we call the hook as expected
        """
        # Setup our hook
        @self.config.web.hooks.on('request')
        def on_web_request(span):
            span.set_tag('web.request', '/')

        # Create our span
        span = self.tracer.start_span('web.request')
        assert 'web.request' not in span.meta

        # Emit the span
        self.config.web.hooks._emit('request', span)

        # Assert we updated the span as expected
        assert span.get_tag('web.request') == '/'

    def test_settings_hook_args(self):
        """
        When calling `Hooks._emit()` with arguments
            When there is a hook registered
                we call the hook as expected
        """
        # Setup our hook
        @self.config.web.hooks.on('request')
        def on_web_request(span, request, response):
            span.set_tag('web.request', request)
            span.set_tag('web.response', response)

        # Create our span
        span = self.tracer.start_span('web.request')
        assert 'web.request' not in span.meta

        # Emit the span
        # DEV: The actual values don't matter, we just want to test args + kwargs usage
        self.config.web.hooks._emit('request', span, 'request', response='response')

        # Assert we updated the span as expected
        assert span.get_tag('web.request') == 'request'
        assert span.get_tag('web.response') == 'response'

    def test_settings_hook_args_failure(self):
        """
        When calling `Hooks._emit()` with arguments
            When there is a hook registered that is missing parameters
                we do not raise an exception
        """
        # Setup our hook
        # DEV: We are missing the required "response" argument
        @self.config.web.hooks.on('request')
        def on_web_request(span, request):
            span.set_tag('web.request', request)

        # Create our span
        span = self.tracer.start_span('web.request')
        assert 'web.request' not in span.meta

        # Emit the span
        # DEV: This also asserts that no exception was raised
        self.config.web.hooks._emit('request', span, 'request', response='response')

        # Assert we did not update the span
        assert 'web.request' not in span.meta

    def test_settings_multiple_hooks(self):
        """
        When calling `Hooks._emit()`
            When there are multiple hooks registered
                we do not raise an exception
        """
        # Setup our hooks
        @self.config.web.hooks.on('request')
        def on_web_request(span):
            span.set_tag('web.request', '/')

        @self.config.web.hooks.on('request')
        def on_web_request2(span):
            span.set_tag('web.status', 200)

        @self.config.web.hooks.on('request')
        def on_web_request3(span):
            span.set_tag('web.method', 'GET')

        # Create our span
        span = self.tracer.start_span('web.request')
        assert 'web.request' not in span.meta
        assert 'web.status' not in span.metrics
        assert 'web.method' not in span.meta

        # Emit the span
        self.config.web.hooks._emit('request', span)

        # Assert we updated the span as expected
        assert span.get_tag('web.request') == '/'
        assert span.get_metric('web.status') == 200
        assert span.get_tag('web.method') == 'GET'

    def test_settings_hook_failure(self):
        """
        When calling `Hooks._emit()`
            When the hook raises an exception
                we do not raise an exception
        """
        # Setup our failing hook
        on_web_request = mock.Mock(side_effect=Exception)
        self.config.web.hooks.register('request')(on_web_request)

        # Create our span
        span = self.tracer.start_span('web.request')

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks._emit('request', span)
        on_web_request.assert_called()

    def test_settings_no_hook(self):
        """
        When calling `Hooks._emit()`
            When no hook is registered
                we do not raise an exception
        """
        # Create our span
        span = self.tracer.start_span('web.request')

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks._emit('request', span)

    def test_settings_no_span(self):
        """
        When calling `Hooks._emit()`
            When no span is provided
                we do not raise an exception
        """
        # Setup our hooks
        @self.config.web.hooks.on('request')
        def on_web_request(span):
            span.set_tag('web.request', '/')

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks._emit('request', None)
Esempio n. 24
0
 def test_allow_configuring_http(self):
     global_config = Config()
     integration_config = IntegrationConfig(global_config)
     integration_config.http.trace_headers('integration_header')
     assert integration_config.http.header_is_traced('integration_header')
     assert not integration_config.http.header_is_traced('other_header')
 def setUp(self):
     self.config = Config()
     self.tracer = DummyTracer()
Esempio n. 26
0
class TestIntegrationConfig(BaseTestCase):
    def setUp(self):
        self.config = Config()
        self.integration_config = IntegrationConfig(self.config, "test")

    def test_is_a_dict(self):
        assert isinstance(self.integration_config, dict)

    def test_allow_item_access(self):
        self.integration_config["setting"] = "value"

        # Can be accessed both as item and attr accessor
        assert self.integration_config.setting == "value"
        assert self.integration_config["setting"] == "value"

    def test_allow_attr_access(self):
        self.integration_config.setting = "value"

        # Can be accessed both as item and attr accessor
        assert self.integration_config.setting == "value"
        assert self.integration_config["setting"] == "value"

    def test_allow_both_access(self):
        self.integration_config.setting = "value"
        assert self.integration_config["setting"] == "value"
        assert self.integration_config.setting == "value"

        self.integration_config["setting"] = "new-value"
        assert self.integration_config.setting == "new-value"
        assert self.integration_config["setting"] == "new-value"

    def test_allow_configuring_http(self):
        self.integration_config.http.trace_headers("integration_header")
        assert self.integration_config.http.header_is_traced("integration_header")
        assert not self.integration_config.http.header_is_traced("other_header")

    def test_allow_exist_both_global_and_integration_config(self):
        self.config.trace_headers("global_header")
        assert self.integration_config.header_is_traced("global_header")

        self.integration_config.http.trace_headers("integration_header")
        assert self.integration_config.header_is_traced("integration_header")
        assert not self.integration_config.header_is_traced("global_header")
        assert not self.config.header_is_traced("integration_header")

    def test_environment_analytics_enabled(self):
        # default
        self.assertFalse(self.config.analytics_enabled)
        self.assertIsNone(self.config.foo.analytics_enabled)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.analytics_enabled)
            self.assertIsNone(config.foo.analytics_enabled)

        with self.override_env(dict(DD_TRACE_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.analytics_enabled)
            self.assertIsNone(config.foo.analytics_enabled)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 1.0)

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="True")):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 1.0)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="False")):
            config = Config()
            self.assertFalse(config.foo.analytics_enabled)

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="False")):
            config = Config()
            self.assertFalse(config.foo.analytics_enabled)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="True", DD_FOO_ANALYTICS_SAMPLE_RATE="0.5")):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 0.5)

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="True", DD_TRACE_FOO_ANALYTICS_SAMPLE_RATE="0.5")):
            config = Config()
            self.assertTrue(config.foo.analytics_enabled)
            self.assertEqual(config.foo.analytics_sample_rate, 0.5)

    def test_analytics_enabled_attribute(self):
        """" Confirm environment variable and kwargs are handled properly """
        ic = IntegrationConfig(self.config, "foo", analytics_enabled=True)
        self.assertTrue(ic.analytics_enabled)

        ic = IntegrationConfig(self.config, "foo", analytics_enabled=False)
        self.assertFalse(ic.analytics_enabled)

        with self.override_env(dict(DD_FOO_ANALYTICS_ENABLED="True")):
            ic = IntegrationConfig(self.config, "foo", analytics_enabled=False)
            self.assertFalse(ic.analytics_enabled)

        with self.override_env(dict(DD_TRACE_FOO_ANALYTICS_ENABLED="True")):
            ic = IntegrationConfig(self.config, "foo", analytics_enabled=False)
            self.assertFalse(ic.analytics_enabled)

    def test_get_analytics_sample_rate(self):
        """" Check method for accessing sample rate based on configuration """
        ic = IntegrationConfig(self.config, "foo", analytics_enabled=True, analytics_sample_rate=0.5)
        self.assertEqual(ic.get_analytics_sample_rate(), 0.5)

        ic = IntegrationConfig(self.config, "foo", analytics_enabled=True)
        self.assertEqual(ic.get_analytics_sample_rate(), 1.0)

        ic = IntegrationConfig(self.config, "foo", analytics_enabled=False)
        self.assertIsNone(ic.get_analytics_sample_rate())

        with self.override_env(dict(DD_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertEqual(ic.get_analytics_sample_rate(use_global_config=True), 1.0)

        with self.override_env(dict(DD_TRACE_ANALYTICS_ENABLED="True")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertEqual(ic.get_analytics_sample_rate(use_global_config=True), 1.0)

        with self.override_env(dict(DD_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertIsNone(ic.get_analytics_sample_rate(use_global_config=True))

        with self.override_env(dict(DD_TRACE_ANALYTICS_ENABLED="False")):
            config = Config()
            ic = IntegrationConfig(config, "foo")
            self.assertIsNone(ic.get_analytics_sample_rate(use_global_config=True))

    def test_service(self):
        ic = IntegrationConfig(self.config, "foo")
        assert ic.service is None

    @BaseTestCase.run_in_subprocess(env_overrides=dict(DD_FOO_SERVICE="foo-svc"))
    def test_service_env_var(self):
        ic = IntegrationConfig(self.config, "foo")
        assert ic.service == "foo-svc"

    @BaseTestCase.run_in_subprocess(env_overrides=dict(DATADOG_FOO_SERVICE="foo-svc"))
    def test_service_env_var_legacy(self):
        ic = IntegrationConfig(self.config, "foo")
        assert ic.service == "foo-svc"

    @BaseTestCase.run_in_subprocess(env_overrides=dict(DD_FOO_SERVICE_NAME="foo-svc"))
    def test_service_name_env_var(self):
        ic = IntegrationConfig(self.config, "foo")
        assert ic.service == "foo-svc"

    @BaseTestCase.run_in_subprocess(env_overrides=dict(DATADOG_FOO_SERVICE_NAME="foo-svc"))
    def test_service_name_env_var_legacy(self):
        ic = IntegrationConfig(self.config, "foo")
        assert ic.service == "foo-svc"
Esempio n. 27
0
class GlobalConfigTestCase(TestCase):
    """Test the `Configuration` class that stores integration settings"""

    def setUp(self):
        self.config = Config()
        self.tracer = get_dummy_tracer()

    def test_registration(self):
        # ensure an integration can register a new list of settings
        settings = {
            "distributed_tracing": True,
        }
        self.config._add("requests", settings)
        assert self.config.requests["distributed_tracing"] is True

    def test_settings_copy(self):
        # ensure that once an integration is registered, a copy
        # of the settings is stored to avoid side-effects
        experimental = {
            "request_enqueuing": True,
        }
        settings = {
            "distributed_tracing": True,
            "experimental": experimental,
        }
        self.config._add("requests", settings)

        settings["distributed_tracing"] = False
        experimental["request_enqueuing"] = False
        assert self.config.requests["distributed_tracing"] is True
        assert self.config.requests["experimental"]["request_enqueuing"] is True

    def test_missing_integration_key(self):
        # ensure a meaningful exception is raised when an integration
        # that is not available is retrieved in the configuration
        # object
        with pytest.raises(KeyError) as e:
            self.config.new_integration["some_key"]

        assert isinstance(e.value, KeyError)

    def test_global_configuration(self):
        # ensure a global configuration is available in the `ddtrace` module
        assert isinstance(global_config, Config)

    def test_settings_merge(self):
        """
        When calling `config._add()`
            when existing settings exist
                we do not overwrite the existing settings
        """
        self.config.requests["split_by_domain"] = True
        self.config._add("requests", dict(split_by_domain=False))
        assert self.config.requests["split_by_domain"] is True

    def test_settings_overwrite(self):
        """
        When calling `config._add(..., merge=False)`
            when existing settings exist
                we overwrite the existing settings
        """
        self.config.requests["split_by_domain"] = True
        self.config._add("requests", dict(split_by_domain=False), merge=False)
        assert self.config.requests["split_by_domain"] is False

    def test_settings_merge_deep(self):
        """
        When calling `config._add()`
            when existing "deep" settings exist
                we do not overwrite the existing settings
        """
        self.config.requests["a"] = dict(
            b=dict(
                c=True,
            ),
        )
        self.config._add(
            "requests",
            dict(
                a=dict(
                    b=dict(
                        c=False,
                        d=True,
                    ),
                ),
            ),
        )
        assert self.config.requests["a"]["b"]["c"] is True
        assert self.config.requests["a"]["b"]["d"] is True

    def test_settings_hook(self):
        """
        When calling `Hooks.emit()`
            When there is a hook registered
                we call the hook as expected
        """
        # Setup our hook
        @self.config.web.hooks.on("request")
        def on_web_request(span):
            span.set_tag("web.request", "/")

        # Create our span
        span = self.tracer.start_span("web.request")
        assert "web.request" not in span.meta

        # Emit the span
        self.config.web.hooks.emit("request", span)

        # Assert we updated the span as expected
        assert span.get_tag("web.request") == "/"

    def test_settings_hook_args(self):
        """
        When calling `Hooks.emit()` with arguments
            When there is a hook registered
                we call the hook as expected
        """
        # Setup our hook
        @self.config.web.hooks.on("request")
        def on_web_request(span, request, response):
            span.set_tag("web.request", request)
            span.set_tag("web.response", response)

        # Create our span
        span = self.tracer.start_span("web.request")
        assert "web.request" not in span.meta

        # Emit the span
        # DEV: The actual values don't matter, we just want to test args + kwargs usage
        self.config.web.hooks.emit("request", span, "request", response="response")

        # Assert we updated the span as expected
        assert span.get_tag("web.request") == "request"
        assert span.get_tag("web.response") == "response"

    def test_settings_hook_args_failure(self):
        """
        When calling `Hooks.emit()` with arguments
            When there is a hook registered that is missing parameters
                we do not raise an exception
        """
        # Setup our hook
        # DEV: We are missing the required "response" argument
        @self.config.web.hooks.on("request")
        def on_web_request(span, request):
            span.set_tag("web.request", request)

        # Create our span
        span = self.tracer.start_span("web.request")
        assert "web.request" not in span.meta

        # Emit the span
        # DEV: This also asserts that no exception was raised
        self.config.web.hooks.emit("request", span, "request", response="response")

        # Assert we did not update the span
        assert "web.request" not in span.meta

    def test_settings_multiple_hooks(self):
        """
        When calling `Hooks.emit()`
            When there are multiple hooks registered
                we do not raise an exception
        """
        # Setup our hooks
        @self.config.web.hooks.on("request")
        def on_web_request(span):
            span.set_tag("web.request", "/")

        @self.config.web.hooks.on("request")
        def on_web_request2(span):
            span.set_tag("web.status", 200)

        @self.config.web.hooks.on("request")
        def on_web_request3(span):
            span.set_tag("web.method", "GET")

        # Create our span
        span = self.tracer.start_span("web.request")
        assert "web.request" not in span.meta
        assert "web.status" not in span.metrics
        assert "web.method" not in span.meta

        # Emit the span
        self.config.web.hooks.emit("request", span)

        # Assert we updated the span as expected
        assert span.get_tag("web.request") == "/"
        assert span.get_metric("web.status") == 200
        assert span.get_tag("web.method") == "GET"

    def test_settings_hook_failure(self):
        """
        When calling `Hooks.emit()`
            When the hook raises an exception
                we do not raise an exception
        """
        # Setup our failing hook
        on_web_request = mock.Mock(side_effect=Exception)
        self.config.web.hooks.register("request")(on_web_request)

        # Create our span
        span = self.tracer.start_span("web.request")

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks.emit("request", span)
        on_web_request.assert_called()

    def test_settings_no_hook(self):
        """
        When calling `Hooks.emit()`
            When no hook is registered
                we do not raise an exception
        """
        # Create our span
        span = self.tracer.start_span("web.request")

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks.emit("request", span)

    def test_settings_no_span(self):
        """
        When calling `Hooks.emit()`
            When no span is provided
                we do not raise an exception
        """
        # Setup our hooks
        @self.config.web.hooks.on("request")
        def on_web_request(span):
            span.set_tag("web.request", "/")

        # Emit the span
        # DEV: This is the test, to ensure no exceptions are raised
        self.config.web.hooks.emit("request", None)

    def test_dd_version(self):
        c = Config()
        assert c.version is None

        with override_env(dict(DD_VERSION="1.2.3")):
            c = Config()
            assert c.version == "1.2.3"

            c.version = "4.5.6"
            assert c.version == "4.5.6"

    def test_dd_env(self):
        c = Config()
        assert c.env is None

        with override_env(dict(DD_ENV="prod")):
            c = Config()
            assert c.env == "prod"

            # manual override still possible
            c.env = "prod-staging"
            assert c.env == "prod-staging"

        # between DD_ENV and DATADOG_ENV, the former takes priority
        with override_env(dict(DATADOG_ENV="prod", DD_ENV="prod-staging")):
            c = Config()
            assert c.env == "prod-staging"
Esempio n. 28
0
 def setUp(self):
     self.config = Config()
     self.tracer = get_dummy_tracer()
Esempio n. 29
0
 def setUp(self):
     self.config = Config()
     self.integration_config = IntegrationConfig(self.config, "test")
Esempio n. 30
0
 def override_service_mapping(service_mapping):
     with override_env(dict(DD_SERVICE_MAPPING=service_mapping)):
         assert ddtrace.config.service_mapping == {}
         ddtrace.config.service_mapping = Config().service_mapping
         yield
         ddtrace.config.service_mapping = {}