Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a logic bug on parentless spans #782

Merged
merged 9 commits into from
Nov 19, 2021
Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down