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

Add io and formatter options to console exporter #412

Merged
merged 7 commits into from
Feb 13, 2020
Merged
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
11 changes: 10 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import collections
import logging
import sys
import threading
import typing
from enum import Enum
Expand Down Expand Up @@ -266,7 +267,15 @@ class ConsoleSpanExporter(SpanExporter):
spans to the console STDOUT.
"""

def __init__(
self,
out: typing.IO = sys.stdout,
formatter: typing.Callable[[Span], str] = str,
c24t marked this conversation as resolved.
Show resolved Hide resolved
):
self.out = out
self.formatter = formatter
c24t marked this conversation as resolved.
Show resolved Hide resolved

def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
for span in spans:
print(span)
self.out.write(self.formatter(span))
c24t marked this conversation as resolved.
Show resolved Hide resolved
return SpanExportResult.SUCCESS
28 changes: 28 additions & 0 deletions opentelemetry-sdk/tests/trace/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,31 @@ def test_batch_span_processor_parameters(self):
max_queue_size=256,
max_export_batch_size=512,
)


class TestConsoleSpanExporter(unittest.TestCase):
def test_export(self): # pylint: disable=no-self-use
"""Check that the console exporter prints spans."""
exporter = export.ConsoleSpanExporter()

# Mocking stdout interferes with debugging and test reporting, mock on
# the exporter instance instead.
span = trace.Span("span name", mock.Mock())
with mock.patch.object(exporter, "out") as mock_stdout:
exporter.export([span])
mock_stdout.write.assert_called_once_with(str(span))
self.assertEqual(mock_stdout.write.call_count, 1)

def test_export_custom(self): # pylint: disable=no-self-use
"""Check that console exporter uses custom io, formatter."""
mock_span_str = mock.Mock(str)

def formatter(span): # pylint: disable=unused-argument
return mock_span_str

mock_stdout = mock.Mock()
exporter = export.ConsoleSpanExporter(
out=mock_stdout, formatter=formatter
)
exporter.export([trace.Span("span name", mock.Mock())])
mock_stdout.write.assert_called_once_with(mock_span_str)