Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 68569f2

Browse files
wkiserliyanhui1228
authored andcommitted
Add SpanData class (#136)
1 parent ad75bf5 commit 68569f2

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

opencensus/trace/span_data.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
import collections
16+
17+
_SpanData = collections.namedtuple(
18+
'_SpanData',
19+
(
20+
'name',
21+
'span_id',
22+
'parent_span_id',
23+
'attributes',
24+
'start_time',
25+
'end_time',
26+
'stack_trace',
27+
'time_events',
28+
'links',
29+
'status',
30+
'same_process_as_parent_span',
31+
),
32+
)
33+
34+
35+
class SpanData(_SpanData):
36+
"""Immutable representation of all data collected by a
37+
:class: `~opencensus.trace.span.Span`.
38+
39+
:type name: str
40+
:param name: The name of the span.
41+
42+
:type span_id: int
43+
:param span_id: Identifier for the span, unique within a trace.
44+
45+
:type parent_span_id: int
46+
:param parent_span_id: (Optional) Parent span id.
47+
48+
:type attributes: dict
49+
:param attributes: Collection of attributes associated with the span.
50+
51+
:type start_time: str
52+
:param start_time: (Optional) Start of the time interval (inclusive)
53+
during which the trace data was collected from the
54+
application.
55+
56+
:type end_time: str
57+
:param end_time: (Optional) End of the time interval (inclusive) during
58+
which the trace data was collected from the application.
59+
60+
:type child_span_count: int
61+
:param child_span_count: the number of child spans that were
62+
generated while the span was active.
63+
64+
:type stack_trace: :class: `~opencensus.trace.stack_trace.StackTrace`
65+
:param stack_trace: (Optional) A call stack appearing in a trace
66+
67+
:type time_events: list
68+
:param time_events: (Optional) A set of time events. You can have up to 32
69+
annotations and 128 message events per span.
70+
71+
:type links: list
72+
:param links: (Optional) Links associated with the span. You can have up
73+
to 128 links per Span.
74+
75+
:type status: :class: `~opencensus.trace.status.Status`
76+
:param status: (Optional) An optional final status for this span.
77+
78+
:type same_process_as_parent_span: bool
79+
:param same_process_as_parent_span: (Optional) A highly recommended but not
80+
required flag that identifies when a
81+
trace crosses a process boundary.
82+
True when the parent_span belongs to
83+
the same process as the current span.
84+
85+
"""
86+
__slots__ = ()

tests/unit/trace/test_span_data.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import datetime
16+
import unittest
17+
18+
from opencensus.trace import span_data as span_data_module
19+
20+
21+
class TestSpan(unittest.TestCase):
22+
def test_create_span_data(self):
23+
span_data_module.SpanData(
24+
name='root',
25+
span_id=2,
26+
parent_span_id=1,
27+
attributes={'key1': 'value1'},
28+
start_time=datetime.datetime.utcnow().isoformat() + 'Z',
29+
end_time=datetime.datetime.utcnow().isoformat() + 'Z',
30+
stack_trace=None,
31+
links=None,
32+
status=None,
33+
time_events=None,
34+
same_process_as_parent_span=None
35+
)
36+
37+
def test_span_data_immutable(self):
38+
span_data = span_data_module.SpanData(
39+
name='root',
40+
span_id=2,
41+
parent_span_id=1,
42+
attributes={'key1': 'value1'},
43+
start_time=datetime.datetime.utcnow().isoformat() + 'Z',
44+
end_time=datetime.datetime.utcnow().isoformat() + 'Z',
45+
stack_trace=None,
46+
links=None,
47+
status=None,
48+
time_events=None,
49+
same_process_as_parent_span=None
50+
)
51+
with self.assertRaises(AttributeError):
52+
span_data.name = 'child'
53+
54+
with self.assertRaises(AttributeError):
55+
span_data.new_attr = 'a'

0 commit comments

Comments
 (0)