8
8
class TestVariables (case .TestCase ):
9
9
10
10
def test_in_access_log_format (self ):
11
- conf_path = Path (__file__ ).parent / ' ./conf/in_access_log_format.conf'
11
+ conf_path = Path (__file__ ).parent / " ./conf/in_access_log_format.conf"
12
12
conf_text = conf_path .read_text ()
13
13
14
14
status , log_lines = self .orch .nginx_replace_config (
@@ -18,16 +18,17 @@ def test_in_access_log_format(self):
18
18
# Drain any old nginx log lines.
19
19
self .orch .sync_nginx_access_log ()
20
20
21
- status , _ , body = self .orch .send_nginx_http_request (' /http' )
21
+ status , _ , body = self .orch .send_nginx_http_request (" /http" )
22
22
self .assertEqual (200 , status , body )
23
23
response = json .loads (body )
24
- headers = response ['headers' ]
25
- trace_id , span_id = int (headers ['x-datadog-trace-id' ]), int (
26
- headers ['x-datadog-parent-id' ])
24
+ headers = response ["headers" ]
25
+ traceparent = headers ["traceparent" ]
26
+ assert traceparent
27
+ _ , trace_id , span_id , _ = traceparent .split ("-" )
27
28
28
29
log_lines = self .orch .sync_nginx_access_log ()
29
30
num_matching_lines = 0
30
- prefix = ' here is your access record: '
31
+ prefix = " here is your access record: "
31
32
for line in log_lines :
32
33
if not line .startswith (prefix ):
33
34
continue
@@ -37,36 +38,73 @@ def test_in_access_log_format(self):
37
38
self .assertEqual (trace_id , log_trace_id , line )
38
39
self .assertEqual (span_id , log_span_id , line )
39
40
self .assertEqual (dict , type (propagation ))
40
- self .assertEqual ('/https?[0-9]*' , location )
41
+ self .assertEqual ("/https?[0-9]*" , location )
42
+
43
+ self .assertEqual (1 , num_matching_lines )
44
+
45
+ def test_in_access_log_format_legacy (self ):
46
+ """Using the legacy 64bits trace ID and span ID"""
47
+ conf_path = Path (
48
+ __file__ ).parent / "./conf/in_access_log_format_legacy.conf"
49
+ conf_text = conf_path .read_text ()
50
+
51
+ status , log_lines = self .orch .nginx_replace_config (
52
+ conf_text , conf_path .name )
53
+ self .assertEqual (0 , status , log_lines )
54
+
55
+ # Drain any old nginx log lines.
56
+ self .orch .sync_nginx_access_log ()
57
+
58
+ status , _ , body = self .orch .send_nginx_http_request ("/http" )
59
+ self .assertEqual (200 , status , body )
60
+ response = json .loads (body )
61
+ headers = response ["headers" ]
62
+ trace_id = headers ["x-datadog-trace-id" ]
63
+ span_id = headers ["x-datadog-parent-id" ]
64
+
65
+ log_lines = self .orch .sync_nginx_access_log ()
66
+ num_matching_lines = 0
67
+ prefix = "here is your access record: "
68
+ for line in log_lines :
69
+ if not line .startswith (prefix ):
70
+ continue
71
+ num_matching_lines += 1
72
+ log_trace_id , log_span_id , propagation , location = json .loads (
73
+ line [len (prefix ):])
74
+ self .assertEqual (trace_id , log_trace_id , line )
75
+ self .assertEqual (span_id , log_span_id , line )
76
+ self .assertEqual (dict , type (propagation ))
77
+ self .assertEqual ("/https?[0-9]*" , location )
41
78
42
79
self .assertEqual (1 , num_matching_lines )
43
80
44
81
def test_in_request_headers (self ):
45
- conf_path = Path (__file__ ).parent / ' ./conf/in_request_headers.conf'
82
+ conf_path = Path (__file__ ).parent / " ./conf/in_request_headers.conf"
46
83
conf_text = conf_path .read_text ()
47
84
48
85
status , log_lines = self .orch .nginx_replace_config (
49
86
conf_text , conf_path .name )
50
87
self .assertEqual (0 , status , log_lines )
51
88
52
- status , _ , body = self .orch .send_nginx_http_request (' /http' )
89
+ status , _ , body = self .orch .send_nginx_http_request (" /http" )
53
90
self .assertEqual (200 , status )
54
91
response = json .loads (body )
55
- headers = response ['headers' ]
56
- trace_id , span_id = int (headers ['x-datadog-trace-id' ]), int (
57
- headers ['x-datadog-parent-id' ])
92
+ headers = response ["headers" ]
93
+ traceparent = headers ["traceparent" ]
94
+ assert traceparent
95
+ _ , trace_id , span_id , _ = traceparent .split ("-" )
58
96
59
97
# The service being reverse proxied by nginx returns a JSON response
60
98
# containing the request headers. By instructing nginx to add headers
61
99
# whose values depend on the variables, we can extract the values of
62
100
# the variables from the response.
63
- self .assertIn (' x-datadog-test-thingy' , headers )
101
+ self .assertIn (" x-datadog-test-thingy" , headers )
64
102
header_trace_id , header_span_id , propagation , location = json .loads (
65
- headers [' x-datadog-test-thingy' ])
103
+ headers [" x-datadog-test-thingy" ])
66
104
self .assertEqual (trace_id , header_trace_id )
67
105
self .assertEqual (span_id , header_span_id )
68
106
self .assertEqual (dict , type (propagation ))
69
- self .assertEqual (' /https?[0-9]*' , location )
107
+ self .assertEqual (" /https?[0-9]*" , location )
70
108
71
109
def test_which_span_id_in_headers (self ):
72
110
"""Verify that when `datadog_trace_locations` is `on`, the span
@@ -83,31 +121,31 @@ def test_which_span_id_in_headers(self):
83
121
nginx.
84
122
"""
85
123
conf_path = Path (
86
- __file__ ).parent / ' ./conf/which_span_id_in_headers.conf'
124
+ __file__ ).parent / " ./conf/which_span_id_in_headers.conf"
87
125
conf_text = conf_path .read_text ()
88
126
89
127
status , log_lines = self .orch .nginx_replace_config (
90
128
conf_text , conf_path .name )
91
129
self .assertEqual (0 , status , log_lines )
92
130
93
- self .orch .sync_service (' agent' )
131
+ self .orch .sync_service (" agent" )
94
132
95
133
headers = {
96
- ' X-Datadog-Trace-ID' : 123 ,
97
- ' X-Datadog-Parent-ID' : 456 ,
98
- ' X-Datadog-Sampling-Priority' : 2 # manual keep
134
+ " X-Datadog-Trace-ID" : 123 ,
135
+ " X-Datadog-Parent-ID" : 456 ,
136
+ " X-Datadog-Sampling-Priority" : 2 , # manual keep
99
137
}
100
- status , _ , body = self .orch .send_nginx_http_request (' /http' ,
138
+ status , _ , body = self .orch .send_nginx_http_request (" /http" ,
101
139
headers = headers )
102
140
self .assertEqual (200 , status )
103
141
response = json .loads (body )
104
- headers = response [' headers' ]
142
+ headers = response [" headers" ]
105
143
106
- self .assertIn (' x-datadog-test-thingy' , headers )
107
- variable_span_id = int ( json . loads ( headers [' x-datadog-test-thingy' ]))
144
+ self .assertIn (" x-datadog-test-thingy" , headers )
145
+ variable_span_id = headers [" x-datadog-test-thingy" ]
108
146
109
147
self .orch .reload_nginx ()
110
- log_lines = self .orch .sync_service (' agent' )
148
+ log_lines = self .orch .sync_service (" agent" )
111
149
112
150
# Map {span ID -> parent ID} for each span sent to the agent by nginx.
113
151
# This will allow us to check that $datadog_span_id is the _location_
@@ -122,10 +160,12 @@ def test_which_span_id_in_headers(self):
122
160
continue
123
161
for chunk in trace :
124
162
first , * rest = chunk
125
- if first [' service' ] == ' nginx' :
163
+ if first [" service" ] == " nginx" :
126
164
nginx_sent_a_trace = True
127
165
for span in chunk :
128
- parent_by_span_id [span ['span_id' ]] = span ['parent_id' ]
166
+ parent_by_span_id [format (span ["span_id" ],
167
+ "016x" )] = format (
168
+ span ["parent_id" ], "016x" )
129
169
130
170
self .assertTrue (nginx_sent_a_trace )
131
171
self .assertIn (variable_span_id , parent_by_span_id )
@@ -138,45 +178,45 @@ def test_which_span_id_in_access_log_format(self):
138
178
referred to by the `$datadog_span_id` variable in an access log format
139
179
string is the location span, not the request span.
140
180
"""
141
- conf_path = Path (
142
- __file__ ). parent / ' ./conf/which_span_id_in_access_log_format.conf'
181
+ conf_path = ( Path (__file__ ). parent /
182
+ " ./conf/which_span_id_in_access_log_format.conf" )
143
183
conf_text = conf_path .read_text ()
144
184
145
185
status , log_lines = self .orch .nginx_replace_config (
146
186
conf_text , conf_path .name )
147
187
self .assertEqual (0 , status , log_lines )
148
188
149
- self .orch .sync_service (' agent' )
189
+ self .orch .sync_service (" agent" )
150
190
151
191
# Drain any old nginx log lines.
152
192
self .orch .sync_nginx_access_log ()
153
193
154
194
headers = {
155
- ' X-Datadog-Trace-ID' : 123 ,
156
- ' X-Datadog-Parent-ID' : 456 ,
157
- ' X-Datadog-Sampling-Priority' : 2 # manual keep
195
+ " X-Datadog-Trace-ID" : 123 ,
196
+ " X-Datadog-Parent-ID" : 456 ,
197
+ " X-Datadog-Sampling-Priority" : 2 , # manual keep
158
198
}
159
- status , _ , body = self .orch .send_nginx_http_request (' /http' ,
199
+ status , _ , body = self .orch .send_nginx_http_request (" /http" ,
160
200
headers = headers )
161
201
self .assertEqual (200 , status , body )
162
202
response = json .loads (body )
163
- headers = response [' headers' ]
203
+ headers = response [" headers" ]
164
204
165
205
log_lines = self .orch .sync_nginx_access_log ()
166
- prefix = ' here is your span ID: '
206
+ prefix = " here is your span ID: "
167
207
logged_span_id = None
168
208
for line in log_lines :
169
209
if not line .startswith (prefix ):
170
210
continue
171
- logged_span_id = int ( line [len (prefix ):])
211
+ logged_span_id = line [len (prefix ) + 1 : - 1 ]
172
212
173
213
self .assertNotEqual (None , logged_span_id )
174
214
175
215
# Now look at the agent's logs to see the spans actually sent, and
176
216
# verify that `logged_span_id` is the _location_ span, not the request
177
217
# span.
178
218
self .orch .reload_nginx ()
179
- log_lines = self .orch .sync_service (' agent' )
219
+ log_lines = self .orch .sync_service (" agent" )
180
220
181
221
parent_by_span_id = {}
182
222
nginx_sent_a_trace = False
@@ -187,10 +227,12 @@ def test_which_span_id_in_access_log_format(self):
187
227
continue
188
228
for chunk in trace :
189
229
first , * rest = chunk
190
- if first [' service' ] == ' nginx' :
230
+ if first [" service" ] == " nginx" :
191
231
nginx_sent_a_trace = True
192
232
for span in chunk :
193
- parent_by_span_id [span ['span_id' ]] = span ['parent_id' ]
233
+ parent_by_span_id [format (span ["span_id" ],
234
+ "016x" )] = format (
235
+ span ["parent_id" ], "016x" )
194
236
195
237
self .assertTrue (nginx_sent_a_trace )
196
238
self .assertIn (logged_span_id , parent_by_span_id )
0 commit comments