|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from opencensus.trace import time_event as time_event_module |
| 16 | +from opencensus.trace.span_context import generate_span_id |
| 17 | +from opencensus.trace.tracers import base |
| 18 | + |
| 19 | + |
| 20 | +class BlankSpan(object): |
| 21 | + """A BlankSpan is an individual timed event which forms a node of the trace |
| 22 | + tree. All operations are no-op. |
| 23 | +
|
| 24 | + :type name: str |
| 25 | + :param name: The name of the span. |
| 26 | +
|
| 27 | + :type parent_span: :class:`~opencensus.trace.blank_span.BlankSpan` |
| 28 | + :param parent_span: (Optional) Parent span. |
| 29 | +
|
| 30 | + :type status: :class: `~opencensus.trace.status.Status` |
| 31 | + :param status: (Optional) An optional final status for this span. |
| 32 | +
|
| 33 | + :type context_tracer: :class:`~opencensus.trace.tracers.noop_tracer. |
| 34 | + NoopTracer` |
| 35 | + :param context_tracer: The tracer that holds a stack of spans. If this is |
| 36 | + not None, then when exiting a span, use the end_span |
| 37 | + method in the tracer class to finish a span. If no |
| 38 | + tracer is passed in, then just finish the span using |
| 39 | + the finish method in the Span class. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + name=None, |
| 45 | + parent_span=None, |
| 46 | + attributes=None, |
| 47 | + start_time=None, |
| 48 | + end_time=None, |
| 49 | + span_id=None, |
| 50 | + stack_trace=None, |
| 51 | + time_events=None, |
| 52 | + links=None, |
| 53 | + status=None, |
| 54 | + same_process_as_parent_span=None, |
| 55 | + context_tracer=None, |
| 56 | + span_kind=None): |
| 57 | + self.name = name |
| 58 | + self.parent_span = parent_span |
| 59 | + self.start_time = start_time |
| 60 | + self.end_time = end_time |
| 61 | + |
| 62 | + self.span_id = generate_span_id() |
| 63 | + self.parent_span = base.NullContextManager() |
| 64 | + |
| 65 | + self.attributes = {} |
| 66 | + self.stack_trace = stack_trace |
| 67 | + self.time_events = time_events |
| 68 | + self.links = [] |
| 69 | + self.status = status |
| 70 | + self.same_process_as_parent_span = same_process_as_parent_span |
| 71 | + self._child_spans = [] |
| 72 | + self.context_tracer = context_tracer |
| 73 | + self.span_kind = span_kind |
| 74 | + |
| 75 | + @property |
| 76 | + def children(self): |
| 77 | + """The child spans of the current BlankSpan.""" |
| 78 | + return list() |
| 79 | + |
| 80 | + def span(self, name='child_span'): |
| 81 | + """Create a child span for the current span and append it to the child |
| 82 | + spans list. |
| 83 | +
|
| 84 | + :type name: str |
| 85 | + :param name: (Optional) The name of the child span. |
| 86 | +
|
| 87 | + :rtype: :class: `~opencensus.trace.blankspan.BlankSpan` |
| 88 | + :returns: A child Span to be added to the current span. |
| 89 | + """ |
| 90 | + child_span = BlankSpan(name, parent_span=self) |
| 91 | + self._child_spans.append(child_span) |
| 92 | + return child_span |
| 93 | + |
| 94 | + def add_attribute(self, attribute_key, attribute_value): |
| 95 | + """No-op implementation of this method. |
| 96 | +
|
| 97 | + :type attribute_key: str |
| 98 | + :param attribute_key: Attribute key. |
| 99 | +
|
| 100 | + :type attribute_value:str |
| 101 | + :param attribute_value: Attribute value. |
| 102 | + """ |
| 103 | + pass |
| 104 | + |
| 105 | + def add_annotation(self, description, **attrs): |
| 106 | + """No-op implementation of this method. |
| 107 | +
|
| 108 | + :type description: str |
| 109 | + :param description: A user-supplied message describing the event. |
| 110 | + The maximum length for the description is 256 bytes. |
| 111 | +
|
| 112 | + :type attrs: kwargs |
| 113 | + :param attrs: keyworded arguments e.g. failed=True, name='Caching' |
| 114 | + """ |
| 115 | + pass |
| 116 | + |
| 117 | + def add_time_event(self, time_event): |
| 118 | + """No-op implementation of this method. |
| 119 | +
|
| 120 | + :type time_event: :class: `~opencensus.trace.time_event.TimeEvent` |
| 121 | + :param time_event: A TimeEvent object. |
| 122 | + """ |
| 123 | + if not isinstance(time_event, time_event_module.TimeEvent): |
| 124 | + raise TypeError("Type Error: received {}, but requires TimeEvent.". |
| 125 | + format(type(time_event).__name__)) |
| 126 | + |
| 127 | + def add_link(self, link): |
| 128 | + """No-op implementation of this method. |
| 129 | +
|
| 130 | + :type link: :class: `~opencensus.trace.link.Link` |
| 131 | + :param link: A Link object. |
| 132 | + """ |
| 133 | + pass |
| 134 | + |
| 135 | + def start(self): |
| 136 | + """No-op implementation of this method.""" |
| 137 | + pass |
| 138 | + |
| 139 | + def finish(self): |
| 140 | + """No-op implementation of this method.""" |
| 141 | + pass |
| 142 | + |
| 143 | + def __iter__(self): |
| 144 | + """Iterate through the span tree.""" |
| 145 | + yield self |
0 commit comments