26
26
to use the API package alone without a supporting implementation.
27
27
28
28
The tracer supports creating spans that are "attached" or "detached" from the
29
- context. By default, new spans are "attached" to the context in that they are
29
+ context. New spans are "attached" to the context in that they are
30
30
created as children of the currently active span, and the newly-created span
31
- becomes the new active span::
31
+ can optionally become the new active span::
32
32
33
33
from opentelemetry.trace import tracer
34
34
35
35
# Create a new root span, set it as the current span in context
36
- with tracer.start_span ("parent"):
36
+ with tracer.start_as_current_span ("parent"):
37
37
# Attach a new child and update the current span
38
- with tracer.start_span ("child"):
38
+ with tracer.start_as_current_span ("child"):
39
39
do_work():
40
40
# Close child span, set parent as current
41
41
# Close parent span, set default span as current
62
62
"""
63
63
64
64
import enum
65
+ import types as python_types
65
66
import typing
66
67
from contextlib import contextmanager
67
68
@@ -226,6 +227,26 @@ def is_recording_events(self) -> bool:
226
227
events with the add_event operation and attributes using set_attribute.
227
228
"""
228
229
230
+ def __enter__ (self ) -> "Span" :
231
+ """Invoked when `Span` is used as a context manager.
232
+
233
+ Returns the `Span` itself.
234
+ """
235
+ return self
236
+
237
+ def __exit__ (
238
+ self ,
239
+ exc_type : typing .Optional [typing .Type [BaseException ]],
240
+ exc_val : typing .Optional [BaseException ],
241
+ exc_tb : typing .Optional [python_types .TracebackType ],
242
+ ) -> typing .Optional [bool ]:
243
+ """Ends context manager and calls `end` on the `Span`.
244
+
245
+ Returns False.
246
+ """
247
+ self .end ()
248
+ return False
249
+
229
250
230
251
class TraceOptions (int ):
231
252
"""A bitmask that represents options specific to the trace.
@@ -376,46 +397,79 @@ def get_current_span(self) -> "Span":
376
397
# pylint: disable=no-self-use
377
398
return INVALID_SPAN
378
399
379
- @contextmanager # type: ignore
380
400
def start_span (
381
401
self ,
382
402
name : str ,
383
403
parent : ParentSpan = CURRENT_SPAN ,
384
404
kind : SpanKind = SpanKind .INTERNAL ,
385
- ) -> typing . Iterator [ "Span" ] :
386
- """Context manager for span creation .
405
+ ) -> "Span" :
406
+ """Starts a span.
387
407
388
- Create a new span. Start the span and set it as the current span in
389
- this tracer's context.
408
+ Create a new span. Start the span without setting it as the current
409
+ span in this tracer's context.
390
410
391
411
By default the current span will be used as parent, but an explicit
392
412
parent can also be specified, either a `Span` or a `SpanContext`. If
393
413
the specified value is `None`, the created span will be a root span.
394
414
395
- On exiting the context manager stop the span and set its parent as the
415
+ The span can be used as context manager. On exiting, the span will be
416
+ ended.
417
+
418
+ Example::
419
+
420
+ # tracer.get_current_span() will be used as the implicit parent.
421
+ # If none is found, the created span will be a root instance.
422
+ with tracer.start_span("one") as child:
423
+ child.add_event("child's event")
424
+
425
+ Applications that need to set the newly created span as the current
426
+ instance should use :meth:`start_as_current_span` instead.
427
+
428
+ Args:
429
+ name: The name of the span to be created.
430
+ parent: The span's parent. Defaults to the current span.
431
+ kind: The span's kind (relationship to parent). Note that is
432
+ meaningful even if there is no parent.
433
+
434
+ Returns:
435
+ The newly-created span.
436
+ """
437
+ # pylint: disable=unused-argument,no-self-use
438
+ return INVALID_SPAN
439
+
440
+ @contextmanager # type: ignore
441
+ def start_as_current_span (
442
+ self ,
443
+ name : str ,
444
+ parent : ParentSpan = CURRENT_SPAN ,
445
+ kind : SpanKind = SpanKind .INTERNAL ,
446
+ ) -> typing .Iterator ["Span" ]:
447
+ """Context manager for creating a new span and set it
448
+ as the current span in this tracer's context.
449
+
450
+ On exiting the context manager stops the span and set its parent as the
396
451
current span.
397
452
398
453
Example::
399
454
400
- with tracer.start_span ("one") as parent:
455
+ with tracer.start_as_current_span ("one") as parent:
401
456
parent.add_event("parent's event")
402
- with tracer.start_span ("two") as child:
457
+ with tracer.start_as_current_span ("two") as child:
403
458
child.add_event("child's event")
404
459
tracer.get_current_span() # returns child
405
460
tracer.get_current_span() # returns parent
406
461
tracer.get_current_span() # returns previously active span
407
462
408
463
This is a convenience method for creating spans attached to the
409
464
tracer's context. Applications that need more control over the span
410
- lifetime should use :meth:`create_span ` instead. For example::
465
+ lifetime should use :meth:`start_span ` instead. For example::
411
466
412
- with tracer.start_span (name) as span:
467
+ with tracer.start_as_current_span (name) as span:
413
468
do_work()
414
469
415
470
is equivalent to::
416
471
417
- span = tracer.create_span(name)
418
- span.start()
472
+ span = tracer.start_span(name)
419
473
with tracer.use_span(span, end_on_exit=True):
420
474
do_work()
421
475
@@ -428,6 +482,7 @@ def start_span(
428
482
Yields:
429
483
The newly-created span.
430
484
"""
485
+
431
486
# pylint: disable=unused-argument,no-self-use
432
487
yield INVALID_SPAN
433
488
@@ -451,7 +506,7 @@ def create_span(
451
506
Applications that need to create spans detached from the tracer's
452
507
context should use this method.
453
508
454
- with tracer.start_span (name) as span:
509
+ with tracer.start_as_current_span (name) as span:
455
510
do_work()
456
511
457
512
This is equivalent to::
0 commit comments