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
This commit is contained in:
Anthony Shaw
2021-11-19 22:55:33 +11:00
committed by GitHub
parent e6dff7e5d1
commit f0ac738ee1
5 changed files with 70 additions and 9 deletions

View File

@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.7.0-0.26b0...HEAD)
### Fixed
- `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans.
([#782](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/782))
- `opentelemetry-instrumentation-tornado` Add support instrumentation for Tornado 5.1.1
([#812](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/812))

View File

@ -36,13 +36,13 @@ all spans will be printed in a list.
from opentelemetry import trace
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.richconsole import RichConsoleExporter
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
from opentelemetry.sdk.trace import TracerProvider
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
tracer.add_span_processor(BatchSpanProcessor(RichConsoleExporter()))
tracer.add_span_processor(BatchSpanProcessor(RichConsoleSpanExporter()))
API
@ -155,18 +155,19 @@ class RichConsoleSpanExporter(SpanExporter):
_child_to_tree(child, span)
for span in spans:
if span.parent and span.parent.span_id not in parents:
child = tree.add(
label=Text.from_markup(
f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}"
)
)
else:
if span.parent and span.parent.span_id in parents:
child = parents[span.parent.span_id].add(
label=Text.from_markup(
f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}"
)
)
else:
child = tree.add(
label=Text.from_markup(
f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}"
)
)
parents[span.context.span_id] = child
_child_to_tree(child, span)

View File

@ -0,0 +1,47 @@
# Copyright The OpenTelemetry 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 pytest
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
from opentelemetry.sdk import trace
from opentelemetry.sdk.trace.export import BatchSpanProcessor
@pytest.fixture(name="span_processor")
def fixture_span_processor():
exporter = RichConsoleSpanExporter()
span_processor = BatchSpanProcessor(exporter)
yield span_processor
span_processor.shutdown()
@pytest.fixture(name="tracer_provider")
def fixture_tracer_provider(span_processor):
tracer_provider = trace.TracerProvider()
tracer_provider.add_span_processor(span_processor)
yield tracer_provider
def test_span_exporter(tracer_provider, span_processor, capsys):
tracer = tracer_provider.get_tracer(__name__)
span = tracer.start_span("test_span")
span.set_attribute("key", "V4LuE")
span.end()
span_processor.force_flush()
captured = capsys.readouterr()
assert "V4LuE" in captured.out

View File

@ -99,6 +99,9 @@ envlist =
; opentelemetry-exporter-datadog
py3{6,7,8,9,10}-test-exporter-datadog
; opentelemetry-exporter-richconsole
py3{6,7,8,9,10}-test-exporter-richconsole
; opentelemetry-instrumentation-mysql
py3{6,7,8,9,10}-test-instrumentation-mysql
pypy3-test-instrumentation-mysql
@ -266,6 +269,7 @@ changedir =
test-propagator-aws: propagator/opentelemetry-propagator-aws-xray/tests
test-propagator-ot-trace: propagator/opentelemetry-propagator-ot-trace/tests
test-exporter-datadog: exporter/opentelemetry-exporter-datadog/tests
test-exporter-richconsole: exporter/opentelemetry-exporter-richconsole/tests
commands_pre =
; Install without -e to test the actual installation
@ -345,6 +349,8 @@ commands_pre =
datadog: pip install flaky {toxinidir}/exporter/opentelemetry-exporter-datadog[test]
richconsole: pip install flaky {toxinidir}/exporter/opentelemetry-exporter-richconsole[test]
sklearn: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sklearn[test]
sqlalchemy{11,14}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sqlalchemy[test]
@ -442,6 +448,7 @@ commands_pre =
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-httpx[test]
python -m pip install -e {toxinidir}/instrumentation/opentelemetry-instrumentation-aws-lambda[test]
python -m pip install -e {toxinidir}/exporter/opentelemetry-exporter-datadog[test]
python -m pip install -e {toxinidir}/exporter/opentelemetry-exporter-richconsole[test]
python -m pip install -e {toxinidir}/sdk-extension/opentelemetry-sdk-extension-aws[test]
python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-aws-xray[test]
python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-ot-trace[test]