Skip to content

Commit f0ac738

Browse files
authored
Fix a logic bug on parentless spans (#782)
* Fix a logic bug on parentless spans * Add a test * Simplify logic * Add changelog entry * Update tox configuration * Rename the fixtures to keep pylint happy
1 parent e6dff7e commit f0ac738

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.7.0-0.26b0...HEAD)
9+
10+
### Fixed
11+
12+
- `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans.
13+
([#782](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/782))
14+
915
- `opentelemetry-instrumentation-tornado` Add support instrumentation for Tornado 5.1.1
1016
([#812](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/812))
1117

exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
3737
from opentelemetry import trace
3838
from opentelemetry.sdk.trace.export import BatchSpanProcessor
39-
from opentelemetry.exporter.richconsole import RichConsoleExporter
39+
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
4040
from opentelemetry.sdk.trace import TracerProvider
4141
4242
trace.set_tracer_provider(TracerProvider())
4343
tracer = trace.get_tracer(__name__)
4444
45-
tracer.add_span_processor(BatchSpanProcessor(RichConsoleExporter()))
45+
tracer.add_span_processor(BatchSpanProcessor(RichConsoleSpanExporter()))
4646
4747
4848
API
@@ -155,18 +155,19 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
155155
_child_to_tree(child, span)
156156

157157
for span in spans:
158-
if span.parent and span.parent.span_id not in parents:
159-
child = tree.add(
158+
if span.parent and span.parent.span_id in parents:
159+
child = parents[span.parent.span_id].add(
160160
label=Text.from_markup(
161161
f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}"
162162
)
163163
)
164164
else:
165-
child = parents[span.parent.span_id].add(
165+
child = tree.add(
166166
label=Text.from_markup(
167167
f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}"
168168
)
169169
)
170+
170171
parents[span.context.span_id] = child
171172
_child_to_tree(child, span)
172173

exporter/opentelemetry-exporter-richconsole/tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright The OpenTelemetry 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 pytest
16+
17+
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
18+
from opentelemetry.sdk import trace
19+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
20+
21+
22+
@pytest.fixture(name="span_processor")
23+
def fixture_span_processor():
24+
exporter = RichConsoleSpanExporter()
25+
span_processor = BatchSpanProcessor(exporter)
26+
27+
yield span_processor
28+
29+
span_processor.shutdown()
30+
31+
32+
@pytest.fixture(name="tracer_provider")
33+
def fixture_tracer_provider(span_processor):
34+
tracer_provider = trace.TracerProvider()
35+
tracer_provider.add_span_processor(span_processor)
36+
37+
yield tracer_provider
38+
39+
40+
def test_span_exporter(tracer_provider, span_processor, capsys):
41+
tracer = tracer_provider.get_tracer(__name__)
42+
span = tracer.start_span("test_span")
43+
span.set_attribute("key", "V4LuE")
44+
span.end()
45+
span_processor.force_flush()
46+
captured = capsys.readouterr()
47+
assert "V4LuE" in captured.out

tox.ini

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ envlist =
9999
; opentelemetry-exporter-datadog
100100
py3{6,7,8,9,10}-test-exporter-datadog
101101

102+
; opentelemetry-exporter-richconsole
103+
py3{6,7,8,9,10}-test-exporter-richconsole
104+
102105
; opentelemetry-instrumentation-mysql
103106
py3{6,7,8,9,10}-test-instrumentation-mysql
104107
pypy3-test-instrumentation-mysql
@@ -266,6 +269,7 @@ changedir =
266269
test-propagator-aws: propagator/opentelemetry-propagator-aws-xray/tests
267270
test-propagator-ot-trace: propagator/opentelemetry-propagator-ot-trace/tests
268271
test-exporter-datadog: exporter/opentelemetry-exporter-datadog/tests
272+
test-exporter-richconsole: exporter/opentelemetry-exporter-richconsole/tests
269273

270274
commands_pre =
271275
; Install without -e to test the actual installation
@@ -345,6 +349,8 @@ commands_pre =
345349

346350
datadog: pip install flaky {toxinidir}/exporter/opentelemetry-exporter-datadog[test]
347351

352+
richconsole: pip install flaky {toxinidir}/exporter/opentelemetry-exporter-richconsole[test]
353+
348354
sklearn: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sklearn[test]
349355

350356
sqlalchemy{11,14}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sqlalchemy[test]
@@ -442,6 +448,7 @@ commands_pre =
442448
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-httpx[test]
443449
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-aws-lambda[test]
444450
python -m pip install -e {toxinidir}/exporter/opentelemetry-exporter-datadog[test]
451+
python -m pip install -e {toxinidir}/exporter/opentelemetry-exporter-richconsole[test]
445452
python -m pip install -e {toxinidir}/sdk-extension/opentelemetry-sdk-extension-aws[test]
446453
python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-aws-xray[test]
447454
python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-ot-trace[test]

0 commit comments

Comments
 (0)