66
66
by `tracer_source`.
67
67
"""
68
68
69
+ import abc
69
70
import enum
70
71
import types as python_types
71
72
import typing
@@ -151,9 +152,10 @@ class SpanKind(enum.Enum):
151
152
CONSUMER = 4
152
153
153
154
154
- class Span :
155
+ class Span ( abc . ABC ) :
155
156
"""A span represents a single operation within a trace."""
156
157
158
+ @abc .abstractmethod
157
159
def end (self , end_time : typing .Optional [int ] = None ) -> None :
158
160
"""Sets the current time as the span's end time.
159
161
@@ -163,6 +165,7 @@ def end(self, end_time: typing.Optional[int] = None) -> None:
163
165
implementations are free to ignore or raise on further calls.
164
166
"""
165
167
168
+ @abc .abstractmethod
166
169
def get_context (self ) -> "SpanContext" :
167
170
"""Gets the span's SpanContext.
168
171
@@ -172,15 +175,15 @@ def get_context(self) -> "SpanContext":
172
175
Returns:
173
176
A :class:`.SpanContext` with a copy of this span's immutable state.
174
177
"""
175
- # pylint: disable=no-self-use
176
- return INVALID_SPAN_CONTEXT
177
178
179
+ @abc .abstractmethod
178
180
def set_attribute (self , key : str , value : types .AttributeValue ) -> None :
179
181
"""Sets an Attribute.
180
182
181
183
Sets a single Attribute with the key and value passed as arguments.
182
184
"""
183
185
186
+ @abc .abstractmethod
184
187
def add_event (
185
188
self ,
186
189
name : str ,
@@ -194,12 +197,14 @@ def add_event(
194
197
timestamp if the `timestamp` argument is omitted.
195
198
"""
196
199
200
+ @abc .abstractmethod
197
201
def add_lazy_event (self , event : Event ) -> None :
198
202
"""Adds an `Event`.
199
203
200
204
Adds an `Event` that has previously been created.
201
205
"""
202
206
207
+ @abc .abstractmethod
203
208
def update_name (self , name : str ) -> None :
204
209
"""Updates the `Span` name.
205
210
@@ -209,15 +214,15 @@ def update_name(self, name: str) -> None:
209
214
on the implementation.
210
215
"""
211
216
217
+ @abc .abstractmethod
212
218
def is_recording_events (self ) -> bool :
213
219
"""Returns whether this span will be recorded.
214
220
215
221
Returns true if this Span is active and recording information like
216
222
events with the add_event operation and attributes using set_attribute.
217
223
"""
218
- # pylint: disable=no-self-use
219
- return False
220
224
225
+ @abc .abstractmethod
221
226
def set_status (self , status : Status ) -> None :
222
227
"""Sets the Status of the Span. If used, this will override the default
223
228
Span status, which is OK.
@@ -362,6 +367,29 @@ def get_context(self) -> "SpanContext":
362
367
def is_recording_events (self ) -> bool :
363
368
return False
364
369
370
+ def end (self , end_time : typing .Optional [int ] = None ) -> None :
371
+ pass
372
+
373
+ def set_attribute (self , key : str , value : types .AttributeValue ) -> None :
374
+ pass
375
+
376
+ def add_event (
377
+ self ,
378
+ name : str ,
379
+ attributes : types .Attributes = None ,
380
+ timestamp : typing .Optional [int ] = None ,
381
+ ) -> None :
382
+ pass
383
+
384
+ def add_lazy_event (self , event : Event ) -> None :
385
+ pass
386
+
387
+ def update_name (self , name : str ) -> None :
388
+ pass
389
+
390
+ def set_status (self , status : Status ) -> None :
391
+ pass
392
+
365
393
366
394
INVALID_SPAN_ID = 0x0000000000000000
367
395
INVALID_TRACE_ID = 0x00000000000000000000000000000000
@@ -374,8 +402,8 @@ def is_recording_events(self) -> bool:
374
402
INVALID_SPAN = DefaultSpan (INVALID_SPAN_CONTEXT )
375
403
376
404
377
- class TracerSource :
378
- # pylint:disable=no-self-use,unused-argument
405
+ class TracerSource ( abc . ABC ) :
406
+ @ abc . abstractmethod
379
407
def get_tracer (
380
408
self ,
381
409
instrumenting_module_name : str ,
@@ -402,10 +430,24 @@ def get_tracer(
402
430
instrumenting library. Usually this should be the same as
403
431
``pkg_resources.get_distribution(instrumenting_library_name).version``.
404
432
"""
405
- return Tracer ()
406
433
407
434
408
- class Tracer :
435
+ class DefaultTracerSource (TracerSource ):
436
+ """The default TracerSource, used when no implementation is available.
437
+
438
+ All operations are no-op.
439
+ """
440
+
441
+ def get_tracer (
442
+ self ,
443
+ instrumenting_module_name : str ,
444
+ instrumenting_library_version : str = "" ,
445
+ ) -> "Tracer" :
446
+ # pylint:disable=no-self-use,unused-argument
447
+ return DefaultTracer ()
448
+
449
+
450
+ class Tracer (abc .ABC ):
409
451
"""Handles span creation and in-process context propagation.
410
452
411
453
This class provides methods for manipulating the context, creating spans,
@@ -414,8 +456,9 @@ class Tracer:
414
456
415
457
# Constant used to represent the current span being used as a parent.
416
458
# This is the default behavior when creating spans.
417
- CURRENT_SPAN = Span ( )
459
+ CURRENT_SPAN = DefaultSpan ( INVALID_SPAN_CONTEXT )
418
460
461
+ @abc .abstractmethod
419
462
def get_current_span (self ) -> "Span" :
420
463
"""Gets the currently active span from the context.
421
464
@@ -426,9 +469,8 @@ def get_current_span(self) -> "Span":
426
469
The currently active :class:`.Span`, or a placeholder span with an
427
470
invalid :class:`.SpanContext`.
428
471
"""
429
- # pylint: disable=no-self-use
430
- return INVALID_SPAN
431
472
473
+ @abc .abstractmethod
432
474
def start_span (
433
475
self ,
434
476
name : str ,
@@ -478,10 +520,9 @@ def start_span(
478
520
Returns:
479
521
The newly-created span.
480
522
"""
481
- # pylint: disable=unused-argument,no-self-use
482
- return INVALID_SPAN
483
523
484
524
@contextmanager # type: ignore
525
+ @abc .abstractmethod
485
526
def start_as_current_span (
486
527
self ,
487
528
name : str ,
@@ -531,10 +572,8 @@ def start_as_current_span(
531
572
The newly-created span.
532
573
"""
533
574
534
- # pylint: disable=unused-argument,no-self-use
535
- yield INVALID_SPAN
536
-
537
575
@contextmanager # type: ignore
576
+ @abc .abstractmethod
538
577
def use_span (
539
578
self , span : "Span" , end_on_exit : bool = False
540
579
) -> typing .Iterator [None ]:
@@ -552,6 +591,47 @@ def use_span(
552
591
end_on_exit: Whether to end the span automatically when leaving the
553
592
context manager.
554
593
"""
594
+
595
+
596
+ class DefaultTracer (Tracer ):
597
+ """The default Tracer, used when no Tracer implementation is available.
598
+
599
+ All operations are no-op.
600
+ """
601
+
602
+ def get_current_span (self ) -> "Span" :
603
+ # pylint: disable=no-self-use
604
+ return INVALID_SPAN
605
+
606
+ def start_span (
607
+ self ,
608
+ name : str ,
609
+ parent : ParentSpan = Tracer .CURRENT_SPAN ,
610
+ kind : SpanKind = SpanKind .INTERNAL ,
611
+ attributes : typing .Optional [types .Attributes ] = None ,
612
+ links : typing .Sequence [Link ] = (),
613
+ start_time : typing .Optional [int ] = None ,
614
+ set_status_on_exception : bool = True ,
615
+ ) -> "Span" :
616
+ # pylint: disable=unused-argument,no-self-use
617
+ return INVALID_SPAN
618
+
619
+ @contextmanager # type: ignore
620
+ def start_as_current_span (
621
+ self ,
622
+ name : str ,
623
+ parent : ParentSpan = Tracer .CURRENT_SPAN ,
624
+ kind : SpanKind = SpanKind .INTERNAL ,
625
+ attributes : typing .Optional [types .Attributes ] = None ,
626
+ links : typing .Sequence [Link ] = (),
627
+ ) -> typing .Iterator ["Span" ]:
628
+ # pylint: disable=unused-argument,no-self-use
629
+ yield INVALID_SPAN
630
+
631
+ @contextmanager # type: ignore
632
+ def use_span (
633
+ self , span : "Span" , end_on_exit : bool = False
634
+ ) -> typing .Iterator [None ]:
555
635
# pylint: disable=unused-argument,no-self-use
556
636
yield
557
637
@@ -576,9 +656,14 @@ def tracer_source() -> TracerSource:
576
656
577
657
if _TRACER_SOURCE is None :
578
658
# pylint:disable=protected-access
579
- _TRACER_SOURCE = loader ._load_impl (
580
- TracerSource , _TRACER_SOURCE_FACTORY
581
- )
659
+ try :
660
+ _TRACER_SOURCE = loader ._load_impl (
661
+ TracerSource , _TRACER_SOURCE_FACTORY # type: ignore
662
+ )
663
+ except TypeError :
664
+ # if we raised an exception trying to instantiate an
665
+ # abstract class, default to no-op tracer impl
666
+ _TRACER_SOURCE = DefaultTracerSource ()
582
667
del _TRACER_SOURCE_FACTORY
583
668
584
669
return _TRACER_SOURCE
0 commit comments