Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3885](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3885))
- `opentelemetry-instrumentation-django`: improve readthedocs for sqlcommenter configuration.
([#3884](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3884))
- `opentelemetry-exporter-richconsole`: Add support for suppressing resource information
([#3898](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3898))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ def _ns_to_time(nanoseconds):
return ts.strftime("%H:%M:%S.%f")


def _child_to_tree(child: Tree, span: ReadableSpan):
def _child_to_tree(
child: Tree, span: ReadableSpan, *, suppress_resource: bool
):
child.add(
Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}")
)
_add_status(child, span)
_child_add_optional_attributes(child, span)
_child_add_optional_attributes(
child, span, suppress_resource=suppress_resource
)


def _add_status(child: Tree, span: ReadableSpan):
Expand All @@ -106,7 +110,9 @@ def _add_status(child: Tree, span: ReadableSpan):
)


def _child_add_optional_attributes(child: Tree, span: ReadableSpan):
def _child_add_optional_attributes(
child: Tree, span: ReadableSpan, *, suppress_resource: bool
):
if span.events:
events = child.add(
label=Text.from_markup("[bold cyan]Events :[/bold cyan] ")
Expand All @@ -133,7 +139,7 @@ def _child_add_optional_attributes(child: Tree, span: ReadableSpan):
f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}"
)
)
if span.resource:
if span.resource and not suppress_resource:
resources = child.add(
label=Text.from_markup("[bold cyan]Resources :[/bold cyan] ")
)
Expand All @@ -155,21 +161,29 @@ class RichConsoleSpanExporter(SpanExporter):
def __init__(
self,
service_name: Optional[str] = None,
suppress_resource: bool = False,
):
self.service_name = service_name
self.suppress_resource = suppress_resource
self.console = Console()

def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
if not spans:
return SpanExportResult.SUCCESS

for tree in self.spans_to_tree(spans).values():
for tree in self.spans_to_tree(
spans, suppress_resource=self.suppress_resource
).values():
self.console.print(tree)

return SpanExportResult.SUCCESS

@staticmethod
def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
def spans_to_tree(
spans: typing.Sequence[ReadableSpan],
*,
suppress_resource: bool = False,
) -> Dict[str, Tree]:
trees = {}
parents = {}
spans = list(spans)
Expand All @@ -189,7 +203,9 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
)
)
parents[span.context.span_id] = child
_child_to_tree(child, span)
_child_to_tree(
child, span, suppress_resource=suppress_resource
)
spans.remove(span)
elif span.parent and span.parent.span_id in parents:
child = parents[span.parent.span_id].add(
Expand All @@ -198,6 +214,8 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
)
)
parents[span.context.span_id] = child
_child_to_tree(child, span)
_child_to_tree(
child, span, suppress_resource=suppress_resource
)
spans.remove(span)
return trees
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
# limitations under the License.

import pytest
from rich.text import Text
from rich.tree import Tree

import opentelemetry.trace
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
from opentelemetry.sdk import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor


Expand Down Expand Up @@ -108,3 +110,30 @@ def test_no_deadlock(tracer_provider):
pass

RichConsoleSpanExporter.spans_to_tree((child,))


def test_suppress_resource(span_processor):
attributes = {"resource.key": "resource.value"}
resource = Resource(attributes)
tracer_provider = trace.TracerProvider(resource=resource)
tracer_provider.add_span_processor(span_processor)
tracer = tracer_provider.get_tracer(__name__)

with tracer.start_as_current_span("parent") as parent:
with tracer.start_as_current_span("child") as child:
pass

trees = RichConsoleSpanExporter.spans_to_tree(
(parent, child), suppress_resource=True
)
assert len(trees) == 1

nodes = [next(t for t in trees.values())]
for node in nodes:
label = node.label
if isinstance(label, Text):
label = label.plain

assert "resource" not in label.lower()

nodes.extend(node.children)
Loading