|
| 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 | +""" |
| 16 | +The **OpenTelemetry Rich Console Exporter** provides a span exporter from a batch span processor |
| 17 | +to print `OpenTelemetry`_ traces using `Rich`_. |
| 18 | +
|
| 19 | +Installation |
| 20 | +------------ |
| 21 | +
|
| 22 | +:: |
| 23 | +
|
| 24 | + pip install opentelemetry-exporter-richconsole |
| 25 | +
|
| 26 | +
|
| 27 | +Usage |
| 28 | +----- |
| 29 | +
|
| 30 | +The Rich Console Exporter is a console exporter that prints a tree view onto stdout of the traces |
| 31 | +with the related spans and properties as children of that tree. For the tree view, the Rich |
| 32 | +Console Exporter should be used with a BatchSpanProcessor. If used within a SimpleSpanProcessor, |
| 33 | +all spans will be printed in a list. |
| 34 | +
|
| 35 | +.. code:: python |
| 36 | +
|
| 37 | + from opentelemetry import trace |
| 38 | + from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 39 | + from opentelemetry.exporter.richconsole import RichConsoleExporter |
| 40 | + from opentelemetry.sdk.trace import TracerProvider |
| 41 | +
|
| 42 | + trace.set_tracer_provider(TracerProvider()) |
| 43 | + tracer = trace.get_tracer(__name__) |
| 44 | +
|
| 45 | + tracer.add_span_processor(BatchSpanProcessor(RichConsoleExporter())) |
| 46 | +
|
| 47 | +
|
| 48 | +API |
| 49 | +--- |
| 50 | +.. _Rich: https://rich.readthedocs.io/ |
| 51 | +.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/ |
| 52 | +""" |
| 53 | +# pylint: disable=import-error |
| 54 | + |
| 55 | +import datetime |
| 56 | +import typing |
| 57 | +from typing import Optional |
| 58 | + |
| 59 | +from rich.console import Console |
| 60 | +from rich.syntax import Syntax |
| 61 | +from rich.text import Text |
| 62 | +from rich.tree import Tree |
| 63 | + |
| 64 | +import opentelemetry.trace |
| 65 | +from opentelemetry.sdk.trace import ReadableSpan |
| 66 | +from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
| 67 | +from opentelemetry.semconv.trace import SpanAttributes |
| 68 | + |
| 69 | + |
| 70 | +def _ns_to_time(nanoseconds): |
| 71 | + ts = datetime.datetime.utcfromtimestamp(nanoseconds / 1e9) |
| 72 | + return ts.strftime("%H:%M:%S.%f") |
| 73 | + |
| 74 | + |
| 75 | +def _child_to_tree(child: Tree, span: ReadableSpan): |
| 76 | + child.add( |
| 77 | + Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}") |
| 78 | + ) |
| 79 | + if not span.status.is_unset: |
| 80 | + if not span.status.is_ok: |
| 81 | + child.add( |
| 82 | + Text.from_markup( |
| 83 | + f"[bold cyan]Status :[/bold cyan] [red]{span.status.status_code}[/red]" |
| 84 | + ) |
| 85 | + ) |
| 86 | + else: |
| 87 | + child.add( |
| 88 | + Text.from_markup( |
| 89 | + f"[bold cyan]Status :[/bold cyan] {span.status.status_code}" |
| 90 | + ) |
| 91 | + ) |
| 92 | + if span.status.description: |
| 93 | + child.add( |
| 94 | + Text.from_markup( |
| 95 | + f"[bold cyan]Description :[/bold cyan] {span.status.description}" |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + if span.events: |
| 100 | + events = child.add( |
| 101 | + label=Text.from_markup("[bold cyan]Events :[/bold cyan] ") |
| 102 | + ) |
| 103 | + for event in span.events: |
| 104 | + event_node = events.add(Text(event.name)) |
| 105 | + for key, val in event.attributes.items(): |
| 106 | + event_node.add( |
| 107 | + Text.from_markup(f"[bold cyan]{key} :[/bold cyan] {val}") |
| 108 | + ) |
| 109 | + if span.attributes: |
| 110 | + attributes = child.add( |
| 111 | + label=Text.from_markup("[bold cyan]Attributes :[/bold cyan] ") |
| 112 | + ) |
| 113 | + for attribute in span.attributes: |
| 114 | + if attribute == SpanAttributes.DB_STATEMENT: |
| 115 | + attributes.add( |
| 116 | + Text.from_markup(f"[bold cyan]{attribute} :[/bold cyan] ") |
| 117 | + ) |
| 118 | + attributes.add(Syntax(span.attributes[attribute], "sql")) |
| 119 | + else: |
| 120 | + attributes.add( |
| 121 | + Text.from_markup( |
| 122 | + f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}" |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +class RichConsoleSpanExporter(SpanExporter): |
| 128 | + """Implementation of :class:`SpanExporter` that prints spans to the |
| 129 | + console. |
| 130 | +
|
| 131 | + Should be used within a BatchSpanProcessor |
| 132 | + """ |
| 133 | + |
| 134 | + def __init__( |
| 135 | + self, service_name: Optional[str] = None, |
| 136 | + ): |
| 137 | + self.service_name = service_name |
| 138 | + self.console = Console() |
| 139 | + |
| 140 | + def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: |
| 141 | + if not spans: |
| 142 | + return SpanExportResult.SUCCESS |
| 143 | + tree = Tree( |
| 144 | + label=f"Trace {opentelemetry.trace.format_trace_id(spans[0].context.trace_id)}" |
| 145 | + ) |
| 146 | + parents = {} |
| 147 | + for span in spans: |
| 148 | + child = tree.add( |
| 149 | + label=Text.from_markup( |
| 150 | + f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" |
| 151 | + ) |
| 152 | + ) |
| 153 | + parents[span.context.span_id] = child |
| 154 | + _child_to_tree(child, span) |
| 155 | + |
| 156 | + for span in spans: |
| 157 | + if span.parent and span.parent.span_id not in parents: |
| 158 | + child = tree.add( |
| 159 | + label=Text.from_markup( |
| 160 | + f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" |
| 161 | + ) |
| 162 | + ) |
| 163 | + else: |
| 164 | + child = parents[span.parent.span_id].add( |
| 165 | + label=Text.from_markup( |
| 166 | + f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" |
| 167 | + ) |
| 168 | + ) |
| 169 | + parents[span.context.span_id] = child |
| 170 | + _child_to_tree(child, span) |
| 171 | + |
| 172 | + self.console.print(tree) |
| 173 | + return SpanExportResult.SUCCESS |
0 commit comments