@@ -213,30 +213,15 @@ def test_start_as_current_span_explicit(self):
213
213
214
214
215
215
class TestSpan (unittest .TestCase ):
216
+ def setUp (self ):
217
+ self .tracer = trace .Tracer ("test_span" )
218
+
216
219
def test_basic_span (self ):
217
220
span = trace .Span ("name" , mock .Mock (spec = trace_api .SpanContext ))
218
221
self .assertEqual (span .name , "name" )
219
222
220
- def test_span_members (self ):
221
- tracer = trace .Tracer ("test_span_members" )
222
-
223
- other_context1 = trace_api .SpanContext (
224
- trace_id = trace .generate_trace_id (),
225
- span_id = trace .generate_span_id (),
226
- )
227
- other_context2 = trace_api .SpanContext (
228
- trace_id = trace .generate_trace_id (),
229
- span_id = trace .generate_span_id (),
230
- )
231
- other_context3 = trace_api .SpanContext (
232
- trace_id = trace .generate_trace_id (),
233
- span_id = trace .generate_span_id (),
234
- )
235
-
236
- self .assertIsNone (tracer .get_current_span ())
237
-
238
- with tracer .start_as_current_span ("root" ) as root :
239
- # attributes
223
+ def test_attributes (self ):
224
+ with self .tracer .start_as_current_span ("root" ) as root :
240
225
root .set_attribute ("component" , "http" )
241
226
root .set_attribute ("http.method" , "GET" )
242
227
root .set_attribute (
@@ -263,30 +248,57 @@ def test_span_members(self):
263
248
self .assertEqual (root .attributes ["misc.pi" ], 3.14 )
264
249
self .assertEqual (root .attributes ["attr-key" ], "attr-value2" )
265
250
266
- # events
251
+ def test_events (self ):
252
+ self .assertIsNone (self .tracer .get_current_span ())
253
+
254
+ with self .tracer .start_as_current_span ("root" ) as root :
255
+ # only event name
267
256
root .add_event ("event0" )
257
+
258
+ # event name and attributes
268
259
now = time_ns ()
269
- root .add_event (
270
- "event1" , timestamp = now , attributes = {"name" : "birthday" }
271
- )
260
+ root .add_event ("event1" , {"name" : "pluto" })
261
+
262
+ # event name, attributes and timestamp
263
+ now = time_ns ()
264
+ root .add_event ("event2" , {"name" : "birthday" }, now )
265
+
266
+ # lazy event
272
267
root .add_lazy_event (
273
- trace_api .Event ("event2 " , now , {"name" : "hello" })
268
+ trace_api .Event ("event3 " , {"name" : "hello" }, now )
274
269
)
275
270
276
- self .assertEqual (len (root .events ), 3 )
271
+ self .assertEqual (len (root .events ), 4 )
277
272
278
273
self .assertEqual (root .events [0 ].name , "event0" )
279
274
self .assertEqual (root .events [0 ].attributes , {})
280
275
281
276
self .assertEqual (root .events [1 ].name , "event1" )
282
- self .assertEqual (root .events [1 ].attributes , {"name" : "birthday" })
283
- self .assertEqual (root .events [1 ].timestamp , now )
277
+ self .assertEqual (root .events [1 ].attributes , {"name" : "pluto" })
284
278
285
279
self .assertEqual (root .events [2 ].name , "event2" )
286
- self .assertEqual (root .events [2 ].attributes , {"name" : "hello " })
280
+ self .assertEqual (root .events [2 ].attributes , {"name" : "birthday " })
287
281
self .assertEqual (root .events [2 ].timestamp , now )
288
282
289
- # links
283
+ self .assertEqual (root .events [3 ].name , "event3" )
284
+ self .assertEqual (root .events [3 ].attributes , {"name" : "hello" })
285
+ self .assertEqual (root .events [3 ].timestamp , now )
286
+
287
+ def test_links (self ):
288
+ other_context1 = trace_api .SpanContext (
289
+ trace_id = trace .generate_trace_id (),
290
+ span_id = trace .generate_span_id (),
291
+ )
292
+ other_context2 = trace_api .SpanContext (
293
+ trace_id = trace .generate_trace_id (),
294
+ span_id = trace .generate_span_id (),
295
+ )
296
+ other_context3 = trace_api .SpanContext (
297
+ trace_id = trace .generate_trace_id (),
298
+ span_id = trace .generate_span_id (),
299
+ )
300
+
301
+ with self .tracer .start_as_current_span ("root" ) as root :
290
302
root .add_link (other_context1 )
291
303
root .add_link (other_context2 , {"name" : "neighbor" })
292
304
root .add_lazy_link (
@@ -313,6 +325,8 @@ def test_span_members(self):
313
325
)
314
326
self .assertEqual (root .links [2 ].attributes , {"component" : "http" })
315
327
328
+ def test_update_name (self ):
329
+ with self .tracer .start_as_current_span ("root" ) as root :
316
330
# name
317
331
root .update_name ("toor" )
318
332
self .assertEqual (root .name , "toor" )
@@ -359,14 +373,13 @@ def test_span_override_start_and_end_time(self):
359
373
360
374
def test_ended_span (self ):
361
375
""""Events, attributes are not allowed after span is ended"""
362
- tracer = trace .Tracer ("test_ended_span" )
363
376
364
377
other_context1 = trace_api .SpanContext (
365
378
trace_id = trace .generate_trace_id (),
366
379
span_id = trace .generate_span_id (),
367
380
)
368
381
369
- with tracer .start_as_current_span ("root" ) as root :
382
+ with self . tracer .start_as_current_span ("root" ) as root :
370
383
# everything should be empty at the beginning
371
384
self .assertEqual (len (root .attributes ), 0 )
372
385
self .assertEqual (len (root .events ), 0 )
0 commit comments