forked from DataDog/dd-trace-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_global_config.py
243 lines (204 loc) · 8.03 KB
/
test_global_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import mock
from unittest import TestCase
from nose.tools import eq_, ok_, assert_raises
from ddtrace import config as global_config
from ddtrace.settings import Config
from .test_tracer import get_dummy_tracer
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)
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_key(self):
# ensure a meaningful exception is raised when an integration
# that is not available is retrieved in the configuration
# object
with assert_raises(KeyError) as e:
self.config.new_integration['some_key']
ok_(isinstance(e.exception, KeyError))
def test_global_configuration(self):
# ensure a global configuration is available in the `ddtrace` module
ok_(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))
eq_(self.config.requests['split_by_domain'], 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)
eq_(self.config.requests['split_by_domain'], 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,
),
),
))
eq_(self.config.requests['a']['b']['c'], True)
eq_(self.config.requests['a']['b']['d'], 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')
ok_('web.request' not in span.meta)
# Emit the span
self.config.web.hooks._emit('request', span)
# Assert we updated the span as expected
eq_(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')
ok_('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
eq_(span.get_tag('web.request'), 'request')
eq_(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')
ok_('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
ok_('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')
ok_('web.request' not in span.meta)
ok_('web.status' not in span.meta)
ok_('web.method' not in span.meta)
# Emit the span
self.config.web.hooks._emit('request', span)
# Assert we updated the span as expected
eq_(span.get_tag('web.request'), '/')
eq_(span.get_tag('web.status'), '200')
eq_(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)