From f7e59ebfc004f1963dbee20307eb5d029272cdc2 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Mon, 20 Sep 2021 08:53:02 +1000 Subject: [PATCH 01/11] Add a rich console exporter --- .../README.rst | 28 +++ .../setup.cfg | 50 ++++++ .../setup.py | 27 +++ .../exporter/richconsole/__init__.py | 168 ++++++++++++++++++ .../exporter/richconsole/version.py | 15 ++ 5 files changed, 288 insertions(+) create mode 100644 exporter/opentelemetry-exporter-richconsole/README.rst create mode 100644 exporter/opentelemetry-exporter-richconsole/setup.cfg create mode 100644 exporter/opentelemetry-exporter-richconsole/setup.py create mode 100644 exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py create mode 100644 exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/version.py diff --git a/exporter/opentelemetry-exporter-richconsole/README.rst b/exporter/opentelemetry-exporter-richconsole/README.rst new file mode 100644 index 0000000000..34b5f030db --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/README.rst @@ -0,0 +1,28 @@ +OpenTelemetry Rich Console Exporter +=================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-exporter-richconsole.svg + :target: https://pypi.org/project/opentelemetry-exporter-richconsole/ + +This library is a console exporter using the Rich tree view. When used with a batch span processor, the rich console exporter will show the trace as a +tree and all related spans as children within the tree, including properties. + +Installation +------------ + +:: + + pip install opentelemetry-exporter-richconsole + + +.. _Rich: https://rich.readthedocs.io/ +.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/ + + +References +---------- + +* `Rich `_ +* `OpenTelemetry Project `_ diff --git a/exporter/opentelemetry-exporter-richconsole/setup.cfg b/exporter/opentelemetry-exporter-richconsole/setup.cfg new file mode 100644 index 0000000000..709c016fe5 --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/setup.cfg @@ -0,0 +1,50 @@ +# 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. +# +[metadata] +name = opentelemetry-exporter-richconsole +description = Rich Console Exporter for OpenTelemetry +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python-contrib/exporter/opentelemetry-exporter-richconsole +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.6 +package_dir= + =src +packages=find_namespace: +install_requires = + rich>=10.0.0 + opentelemetry-api ~= 1.3 + opentelemetry-sdk ~= 1.3 + opentelemetry-semantic-conventions == 0.24b0 + +[options.packages.find] +where = src + +[options.extras_require] +test = diff --git a/exporter/opentelemetry-exporter-richconsole/setup.py b/exporter/opentelemetry-exporter-richconsole/setup.py new file mode 100644 index 0000000000..4237e6f2f0 --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/setup.py @@ -0,0 +1,27 @@ +# 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 os + +import setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, "src", "opentelemetry", "exporter", "richconsole", "version.py" +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py new file mode 100644 index 0000000000..91284189c9 --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -0,0 +1,168 @@ +# 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. + +""" +The **OpenTelemetry Rich Console Exporter** provides a span exporter from a batch span processor +to print `OpenTelemetry`_ traces using `Rich`_. + +Installation +------------ + +:: + + pip install opentelemetry-exporter-richconsole + + +Usage +----- + +The Rich Console Exporter is a console exporter that prints a tree view onto stdout of the traces +with the related spans and properties as children of that tree. The Rich Console Exporter must be +used with a BatchSpanProcessor. + +.. code:: python + + from opentelemetry import trace + from opentelemetry.sdk.trace.export import BatchSpanProcessor + from opentelemetry.exporter.richconsole import RichConsoleExporter + from opentelemetry.sdk.trace import TracerProvider + + trace.set_tracer_provider(TracerProvider()) + tracer = trace.get_tracer(__name__) + + tracer.add_span_processor(BatchSpanProcessor(RichConsoleExporter())) + + +API +--- +.. _Rich: https://rich.readthedocs.io/ +.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/ +""" +# pylint: disable=import-error + +import datetime +import typing +from typing import Optional + +import opentelemetry.trace +from opentelemetry.sdk.trace import ReadableSpan +from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult +from opentelemetry.semconv.trace import SpanAttributes +from rich.console import Console +from rich.syntax import Syntax +from rich.text import Text +from rich.tree import Tree + + +def ns_to_time(nanoseconds): + ts = datetime.datetime.utcfromtimestamp(nanoseconds / 1e9) + return ts.strftime("%H:%M:%S.%f") + + +class RichConsoleExporter(SpanExporter): + """Implementation of :class:`SpanExporter` that prints spans to the + console. + + Must be used within a BatchSpanProcessor + """ + + def __init__( + self, + service_name: Optional[str] = None, + ): + self.service_name = service_name + self.console = Console() + + def _child_to_tree(self, child: Tree, span: ReadableSpan): + child.add(Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}")) + if not span.status.is_unset: + if not span.status.is_ok: + child.add( + Text.from_markup( + f"[bold cyan]Status :[/bold cyan] [red]{span.status.status_code}[/red]" + ) + ) + else: + child.add( + Text.from_markup( + f"[bold cyan]Status :[/bold cyan] {span.status.status_code}" + ) + ) + if span.status.description: + child.add( + Text.from_markup( + f"[bold cyan]Description :[/bold cyan] {span.status.description}" + ) + ) + + if span.events: + events = child.add( + label=Text.from_markup(f"[bold cyan]Events :[/bold cyan] ") + ) + for event in span.events: + event_node = events.add(Text(event.name)) + for k, v in event.attributes.items(): + event_node.add( + Text.from_markup(f"[bold cyan]{k} :[/bold cyan] {v}") + ) + if span.attributes: + attributes = child.add( + label=Text.from_markup(f"[bold cyan]Attributes :[/bold cyan] ") + ) + for attribute in span.attributes: + if attribute == SpanAttributes.DB_STATEMENT: + attributes.add( + Text.from_markup(f"[bold cyan]{attribute} :[/bold cyan] ") + ) + attributes.add(Syntax(span.attributes[attribute], "sql")) + else: + attributes.add( + Text.from_markup( + f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}" + ) + ) + + def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: + if not spans: + return SpanExportResult.SUCCESS + tree = Tree( + label=f"Trace {opentelemetry.trace.format_trace_id(spans[0].context.trace_id)}" + ) + parents = {} + root_spans = [span for span in spans if not span.parent] + for span in root_spans: + 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 + self._child_to_tree(child, span) + + child_spans = [span for span in spans if span.parent] + for span in child_spans: + if span.parent.span_id not in parents: + raise ValueError( + "This exporter needs to be used from a BatchSpanProcessor" + ) + 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)}" + ) + ) + parents[span.context.span_id] = child + self._child_to_tree(child, span) + + self.console.print(tree) + return SpanExportResult.SUCCESS diff --git a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/version.py b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/version.py new file mode 100644 index 0000000000..d33bd87ce4 --- /dev/null +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/version.py @@ -0,0 +1,15 @@ +# 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. + +__version__ = "0.24b0" From 1d5f8bef830026807b599446aba33414edffc915 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Mon, 20 Sep 2021 14:00:41 +1000 Subject: [PATCH 02/11] be more lenient on missing parent spans --- .../exporter/richconsole/__init__.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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 91284189c9..40243df662 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -153,14 +153,17 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: child_spans = [span for span in spans if span.parent] for span in child_spans: if span.parent.span_id not in parents: - raise ValueError( - "This exporter needs to be used from a BatchSpanProcessor" + 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)}" + ) ) - 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( + 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 self._child_to_tree(child, span) From b20b0ab7b8ea8ba240ce4db47c21ee6ce4dbb4b2 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 07:09:12 +1000 Subject: [PATCH 03/11] Apply suggestions from code review Co-authored-by: Aaron Abbott --- .../opentelemetry/exporter/richconsole/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 40243df662..8dc602b60b 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -70,7 +70,7 @@ def ns_to_time(nanoseconds): return ts.strftime("%H:%M:%S.%f") -class RichConsoleExporter(SpanExporter): +class RichConsoleSpanExporter(SpanExporter): """Implementation of :class:`SpanExporter` that prints spans to the console. @@ -140,8 +140,9 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: label=f"Trace {opentelemetry.trace.format_trace_id(spans[0].context.trace_id)}" ) parents = {} - root_spans = [span for span in spans if not span.parent] - for span in root_spans: + for span in spans: + if not span.parent: + ... 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)}" @@ -150,9 +151,8 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: parents[span.context.span_id] = child self._child_to_tree(child, span) - child_spans = [span for span in spans if span.parent] - for span in child_spans: - if span.parent.span_id not in parents: + 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)}" From a58998a83b873aa5c8b9d00a4559bd55ce552b40 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 08:04:04 +1000 Subject: [PATCH 04/11] run black over source --- .../exporter/richconsole/__init__.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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 8dc602b60b..7322143a3a 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -55,15 +55,16 @@ import typing from typing import Optional -import opentelemetry.trace -from opentelemetry.sdk.trace import ReadableSpan -from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult -from opentelemetry.semconv.trace import SpanAttributes from rich.console import Console from rich.syntax import Syntax from rich.text import Text from rich.tree import Tree +import opentelemetry.trace +from opentelemetry.sdk.trace import ReadableSpan +from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult +from opentelemetry.semconv.trace import SpanAttributes + def ns_to_time(nanoseconds): ts = datetime.datetime.utcfromtimestamp(nanoseconds / 1e9) @@ -85,7 +86,9 @@ def __init__( self.console = Console() def _child_to_tree(self, child: Tree, span: ReadableSpan): - child.add(Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}")) + child.add( + Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}") + ) if not span.status.is_unset: if not span.status.is_ok: child.add( @@ -123,7 +126,9 @@ def _child_to_tree(self, child: Tree, span: ReadableSpan): for attribute in span.attributes: if attribute == SpanAttributes.DB_STATEMENT: attributes.add( - Text.from_markup(f"[bold cyan]{attribute} :[/bold cyan] ") + Text.from_markup( + f"[bold cyan]{attribute} :[/bold cyan] " + ) ) attributes.add(Syntax(span.attributes[attribute], "sql")) else: From 4d280bc5bf8e044fa66432aac86097a4d6c567ee Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 08:10:39 +1000 Subject: [PATCH 05/11] patch change by hand --- .../src/opentelemetry/exporter/richconsole/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 7322143a3a..5f87699474 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -79,8 +79,7 @@ class RichConsoleSpanExporter(SpanExporter): """ def __init__( - self, - service_name: Optional[str] = None, + self, service_name: Optional[str] = None, ): self.service_name = service_name self.console = Console() From f2e40fbc7240899c9ca5ff9be01754670d01b825 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 08:16:25 +1000 Subject: [PATCH 06/11] update changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d44be1acd..c0ab430fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `opentelemetry-instrumentation-elasticsearch` Added `response_hook` and `request_hook` callbacks ([#670](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/670)) - -### Added - `opentelemetry-instrumentation-redis` added request_hook and response_hook callbacks passed as arguments to the instrument method. ([#669](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/669)) +- `opentelemetry-exporter-richconsole` Initial release + ([#686](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/686)) ### Changed - `opentelemetry-instrumentation-botocore` Unpatch botocore Endpoint.prepare_request on uninstrument From c73298f9e06141c0e290ba0bb176730023c235ec Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 08:19:32 +1000 Subject: [PATCH 07/11] remove defunct statement --- .../src/opentelemetry/exporter/richconsole/__init__.py | 2 -- 1 file changed, 2 deletions(-) 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 5f87699474..3ca3255131 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -145,8 +145,6 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: ) parents = {} for span in spans: - if not span.parent: - ... 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)}" From 6151f816e650e750a972bf8b04be86e37ab095ee Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 08:21:16 +1000 Subject: [PATCH 08/11] Clarify the simple/batch span processor --- .../src/opentelemetry/exporter/richconsole/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 3ca3255131..2e45327aa2 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -28,8 +28,9 @@ ----- The Rich Console Exporter is a console exporter that prints a tree view onto stdout of the traces -with the related spans and properties as children of that tree. The Rich Console Exporter must be -used with a BatchSpanProcessor. +with the related spans and properties as children of that tree. For the tree view, the Rich +Console Exporter must be used with a BatchSpanProcessor. If used within a SimpleSpanProcessor, +all spans will be printed in a list. .. code:: python @@ -75,7 +76,7 @@ class RichConsoleSpanExporter(SpanExporter): """Implementation of :class:`SpanExporter` that prints spans to the console. - Must be used within a BatchSpanProcessor + Should be used within a BatchSpanProcessor """ def __init__( From 160bcb04b0ff1dd61a5c503bb62ddd466b09285d Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 09:40:49 +1000 Subject: [PATCH 09/11] fix f-strings that dont have formatting --- .../src/opentelemetry/exporter/richconsole/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 2e45327aa2..d1fde398c3 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -111,7 +111,7 @@ def _child_to_tree(self, child: Tree, span: ReadableSpan): if span.events: events = child.add( - label=Text.from_markup(f"[bold cyan]Events :[/bold cyan] ") + label=Text.from_markup("[bold cyan]Events :[/bold cyan] ") ) for event in span.events: event_node = events.add(Text(event.name)) @@ -121,7 +121,7 @@ def _child_to_tree(self, child: Tree, span: ReadableSpan): ) if span.attributes: attributes = child.add( - label=Text.from_markup(f"[bold cyan]Attributes :[/bold cyan] ") + label=Text.from_markup("[bold cyan]Attributes :[/bold cyan] ") ) for attribute in span.attributes: if attribute == SpanAttributes.DB_STATEMENT: From 1cdb7709306cc9e8a9c5047ba5f91fe58d951757 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 10:05:10 +1000 Subject: [PATCH 10/11] clarify span usage and update classifiers --- exporter/opentelemetry-exporter-richconsole/setup.cfg | 1 + .../src/opentelemetry/exporter/richconsole/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-richconsole/setup.cfg b/exporter/opentelemetry-exporter-richconsole/setup.cfg index 709c016fe5..436628583d 100644 --- a/exporter/opentelemetry-exporter-richconsole/setup.cfg +++ b/exporter/opentelemetry-exporter-richconsole/setup.cfg @@ -31,6 +31,7 @@ classifiers = Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 [options] python_requires = >=3.6 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 d1fde398c3..6d2410bd8e 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -29,7 +29,7 @@ The Rich Console Exporter is a console exporter that prints a tree view onto stdout of the traces with the related spans and properties as children of that tree. For the tree view, the Rich -Console Exporter must be used with a BatchSpanProcessor. If used within a SimpleSpanProcessor, +Console Exporter should be used with a BatchSpanProcessor. If used within a SimpleSpanProcessor, all spans will be printed in a list. .. code:: python From e39b8b0fe1aa6351dd7398c73d343e12e17d0789 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 28 Sep 2021 10:47:32 +1000 Subject: [PATCH 11/11] make child_to_tree a private function and rename some variables --- .../exporter/richconsole/__init__.py | 117 +++++++++--------- 1 file changed, 58 insertions(+), 59 deletions(-) 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 6d2410bd8e..38cfac0f68 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -67,11 +67,63 @@ from opentelemetry.semconv.trace import SpanAttributes -def ns_to_time(nanoseconds): +def _ns_to_time(nanoseconds): ts = datetime.datetime.utcfromtimestamp(nanoseconds / 1e9) return ts.strftime("%H:%M:%S.%f") +def _child_to_tree(child: Tree, span: ReadableSpan): + child.add( + Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}") + ) + if not span.status.is_unset: + if not span.status.is_ok: + child.add( + Text.from_markup( + f"[bold cyan]Status :[/bold cyan] [red]{span.status.status_code}[/red]" + ) + ) + else: + child.add( + Text.from_markup( + f"[bold cyan]Status :[/bold cyan] {span.status.status_code}" + ) + ) + if span.status.description: + child.add( + Text.from_markup( + f"[bold cyan]Description :[/bold cyan] {span.status.description}" + ) + ) + + if span.events: + events = child.add( + label=Text.from_markup("[bold cyan]Events :[/bold cyan] ") + ) + for event in span.events: + event_node = events.add(Text(event.name)) + for key, val in event.attributes.items(): + event_node.add( + Text.from_markup(f"[bold cyan]{key} :[/bold cyan] {val}") + ) + if span.attributes: + attributes = child.add( + label=Text.from_markup("[bold cyan]Attributes :[/bold cyan] ") + ) + for attribute in span.attributes: + if attribute == SpanAttributes.DB_STATEMENT: + attributes.add( + Text.from_markup(f"[bold cyan]{attribute} :[/bold cyan] ") + ) + attributes.add(Syntax(span.attributes[attribute], "sql")) + else: + attributes.add( + Text.from_markup( + f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}" + ) + ) + + class RichConsoleSpanExporter(SpanExporter): """Implementation of :class:`SpanExporter` that prints spans to the console. @@ -85,59 +137,6 @@ def __init__( self.service_name = service_name self.console = Console() - def _child_to_tree(self, child: Tree, span: ReadableSpan): - child.add( - Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}") - ) - if not span.status.is_unset: - if not span.status.is_ok: - child.add( - Text.from_markup( - f"[bold cyan]Status :[/bold cyan] [red]{span.status.status_code}[/red]" - ) - ) - else: - child.add( - Text.from_markup( - f"[bold cyan]Status :[/bold cyan] {span.status.status_code}" - ) - ) - if span.status.description: - child.add( - Text.from_markup( - f"[bold cyan]Description :[/bold cyan] {span.status.description}" - ) - ) - - if span.events: - events = child.add( - label=Text.from_markup("[bold cyan]Events :[/bold cyan] ") - ) - for event in span.events: - event_node = events.add(Text(event.name)) - for k, v in event.attributes.items(): - event_node.add( - Text.from_markup(f"[bold cyan]{k} :[/bold cyan] {v}") - ) - if span.attributes: - attributes = child.add( - label=Text.from_markup("[bold cyan]Attributes :[/bold cyan] ") - ) - for attribute in span.attributes: - if attribute == SpanAttributes.DB_STATEMENT: - attributes.add( - Text.from_markup( - f"[bold cyan]{attribute} :[/bold cyan] " - ) - ) - attributes.add(Syntax(span.attributes[attribute], "sql")) - else: - attributes.add( - Text.from_markup( - f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}" - ) - ) - def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: if not spans: return SpanExportResult.SUCCESS @@ -148,27 +147,27 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: for span in spans: 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)}" + 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 - self._child_to_tree(child, span) + _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)}" + 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( 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)}" + 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 - self._child_to_tree(child, span) + _child_to_tree(child, span) self.console.print(tree) return SpanExportResult.SUCCESS