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 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import collections
import logging
import os
import sys
import threading
import typing
Expand Down Expand Up @@ -270,12 +271,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)
+ os.linesep,
):
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
4 changes: 3 additions & 1 deletion opentelemetry-sdk/tests/trace/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import time
import unittest
from logging import WARNING
Expand Down Expand Up @@ -288,8 +289,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) + os.linesep)
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