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

sdk: fix ConsoleSpanExporter #455

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,14 @@ class ConsoleSpanExporter(SpanExporter):
def __init__(
self,
out: typing.IO = sys.stdout,
formatter: typing.Callable[[Span], str] = str,
formatter: typing.Callable[[Span], str] = lambda span: str(span)
+ "\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
formatter: typing.Callable[[Span], str] = lambda span: str(span)
+ "\n",
formatter: typing.Callable[[Span], str] = str,

Along with the other comment suggestion only.

):
self.out = out
self.formatter = formatter

def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
for span in spans:
self.out.write(self.formatter(span))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider doing this to avoid two linebreaks for formatters that do end in a newline:

Suggested change
self.out.write(self.formatter(span))
self.out.write(endline(self.formatter(span)))

with this in the module:

def endline(line):
    if line.endswith(os.linesep):
        return line
    return line + os.linesep

I haven't checked for windows, but suspect you do want os.linesep here instead of "\n".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure about this one, I think we should let full control to the user, for instance, there could be a case where the user doesn't want to have line breaks at all and use another separator.

I totally agree about the os.linesep, I'm too focus on Linux now 😝

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, if a formatter ends with a linebreak there would not be two linebreaks, I only adding a linebreak to the default formatter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think agreed to not force lineseps, formatter should control that (and the flush handles this anyway).

self.out.flush()
return SpanExportResult.SUCCESS
3 changes: 2 additions & 1 deletion opentelemetry-sdk/tests/trace/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ def test_export(self): # pylint: disable=no-self-use
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))
mock_stdout.write.assert_called_once_with(str(span) + "\n")
self.assertEqual(mock_stdout.write.call_count, 1)
self.assertEqual(mock_stdout.flush.call_count, 1)

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