diff --git a/CHANGELOG.md b/CHANGELOG.md index ed0b6256be..46f42fd58b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py index e373ab87b2..494857bcab 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -36,13 +36,13 @@ 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 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: _child_to_tree(child, span) for span in spans: - if span.parent and span.parent.span_id not in parents: - child = tree.add( + 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 = parents[span.parent.span_id].add( + 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) diff --git a/exporter/opentelemetry-exporter-richconsole/tests/__init__.py b/exporter/opentelemetry-exporter-richconsole/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py b/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py new file mode 100644 index 0000000000..fe897537e7 --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py @@ -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 diff --git a/tox.ini b/tox.ini index e5a92fa331..97673d1a47 100644 --- a/tox.ini +++ b/tox.ini @@ -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]