This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathtest_blank_span.py
More file actions
176 lines (144 loc) · 5.49 KB
/
test_blank_span.py
File metadata and controls
176 lines (144 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2018, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import unittest
import mock
from opencensus.common import utils
from opencensus.trace.link import Link
from opencensus.trace.span import format_span_json
from opencensus.trace.status import Status
from opencensus.trace.time_event import MessageEvent
class TestBlankSpan(unittest.TestCase):
@staticmethod
def _get_target_class():
from opencensus.trace.blank_span import BlankSpan
return BlankSpan
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_do_not_crash(self):
span_id = 'test_span_id'
span_name = 'test_span_name'
patch = mock.patch(
'opencensus.trace.blank_span.generate_span_id',
return_value=span_id)
with patch:
span = self._make_one(span_name)
self.assertEqual(span.name, span_name)
self.assertEqual(span.span_id, span_id)
self.assertIsNotNone(span.parent_span)
self.assertIsNotNone(span.parent_span.span())
self.assertEqual(span.attributes, {})
self.assertIsNone(span.start_time)
self.assertIsNone(span.end_time)
self.assertEqual(span.children, [])
self.assertIsNone(span.context_tracer)
span.add_attribute('attribute_key', 'attribute_value')
span.add_annotation('This is a test', name='blank-span')
link = Link(span_id='1234', trace_id='4567')
span.add_link(link)
status = Status(0, 'Ok', {'details': 'ok'})
span.set_status(status)
message_event = mock.Mock()
message_event = MessageEvent(datetime.datetime.now(datetime.UTC), mock.Mock())
span.add_message_event(message_event)
span_iter_list = list(iter(span))
self.assertEqual(span_iter_list, [span])
expected_span_json = {
'spanId': 'test_span_id',
'startTime': None,
'endTime': None,
'displayName': {
'truncated_byte_count': 0,
'value': 'test_span_name'
},
'childSpanCount': 0,
}
span_json = format_span_json(span)
self.assertEqual(span_json, expected_span_json)
span.start()
span.finish()
def test_constructor_explicit(self):
span_id = 'test_span_id'
span_name = 'test_span_name'
parent_span = mock.Mock()
start_time = utils.to_iso_str()
end_time = utils.to_iso_str()
attributes = {
'http.status_code': '200',
'component': 'HTTP load balancer',
}
message_events = [mock.Mock()]
links = mock.Mock()
stack_trace = mock.Mock()
status = mock.Mock()
context_tracer = mock.Mock()
span = self._make_one(
name=span_name,
parent_span=parent_span,
attributes=attributes,
start_time=start_time,
end_time=end_time,
span_id=span_id,
stack_trace=stack_trace,
message_events=message_events,
links=links,
status=status,
context_tracer=context_tracer)
self.assertEqual(span.name, span_name)
self.assertIsNotNone(span.span_id)
self.assertEqual(span.attributes, {})
self.assertEqual(span.start_time, start_time)
self.assertEqual(span.end_time, end_time)
self.assertEqual(list(span.message_events), message_events)
self.assertEqual(span.stack_trace, stack_trace)
self.assertEqual(span.links, [])
self.assertEqual(span.status, status)
self.assertEqual(span.children, [])
self.assertEqual(span.context_tracer, context_tracer)
def test_start(self):
span_name = 'root_span'
span = self._make_one(span_name)
self.assertIsNone(span.start_time)
span.start()
self.assertIsNone(span.start_time)
def test_finish_without_context_tracer(self):
span_name = 'root_span'
span = self._make_one(span_name)
self.assertIsNone(span.end_time)
span.finish()
self.assertIsNone(span.end_time)
def test_finish(self):
span_name = 'root_span'
span = self._make_one(span_name)
self.assertIsNone(span.end_time)
span.finish()
self.assertIsNone(span.end_time)
def test_on_create(self):
from opencensus.trace.blank_span import BlankSpan
self.on_create_called = False
self._make_one('span1')
self.assertFalse(self.on_create_called)
try:
@BlankSpan.on_create
def callback(span):
self.on_create_called = True
self._make_one('span2')
finally:
BlankSpan._on_create_callbacks = []
self.assertFalse(self.on_create_called)
def test_context_manager(self):
span_name = 'root_span'
with self._make_one(span_name) as s:
self.assertIsNotNone(s)
self.assertEqual(s.name, span_name)