From d691412d338f16fa118c78a4b94317d6f063e6c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Mon, 2 Mar 2020 08:00:19 -0500 Subject: [PATCH 1/4] sdk: fix ConsoleSpanExporter 19d573af0275 ("Add io and formatter options to console exporter (#412)") changed the way spans are printed by using write() instead of print(). In Python 3.x sys.stdout is line-buffered, so the spans were not being printed to the console at the right timing. This commit fixes that by adding an explicit flush() call at the end of the export function , it also changes the default formatter to include a line break. To be precise, only one of the changes was needed to solve the problem, but as a matter of completness both are included, i.e, to handle the case where the formatter chosen by the user doesn't append a line break. --- .../src/opentelemetry/sdk/trace/export/__init__.py | 4 +++- opentelemetry-sdk/tests/trace/export/test_export.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 0f96808ea88..12fc84656e8 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -270,7 +270,8 @@ 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", ): self.out = out self.formatter = formatter @@ -278,4 +279,5 @@ def __init__( def export(self, spans: typing.Sequence[Span]) -> SpanExportResult: for span in spans: self.out.write(self.formatter(span)) + self.out.flush() return SpanExportResult.SUCCESS diff --git a/opentelemetry-sdk/tests/trace/export/test_export.py b/opentelemetry-sdk/tests/trace/export/test_export.py index e1c709719a3..f36c79ee910 100644 --- a/opentelemetry-sdk/tests/trace/export/test_export.py +++ b/opentelemetry-sdk/tests/trace/export/test_export.py @@ -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.""" From 17bfd52751aad3f8c660b556e6d00612748e42c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Mon, 2 Mar 2020 21:07:30 -0500 Subject: [PATCH 2/4] use os.linesep instead of \n --- .../src/opentelemetry/sdk/trace/export/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 12fc84656e8..58afa5be115 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. import collections +import os import logging import sys import threading @@ -271,7 +272,7 @@ def __init__( self, out: typing.IO = sys.stdout, formatter: typing.Callable[[Span], str] = lambda span: str(span) - + "\n", + + os.linesep, ): self.out = out self.formatter = formatter From 4b214d8d05c2cd32fdb97012d164a39ff9c2b943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Mon, 2 Mar 2020 21:29:56 -0500 Subject: [PATCH 3/4] fix import order --- .../src/opentelemetry/sdk/trace/export/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 58afa5be115..e5d96eff9e9 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -13,8 +13,8 @@ # limitations under the License. import collections -import os import logging +import os import sys import threading import typing From 9d19288d73c6c9a3927e8eb272597056bdd7f17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Tue, 3 Mar 2020 11:14:53 -0500 Subject: [PATCH 4/4] use os.linesep also in tests --- opentelemetry-sdk/tests/trace/export/test_export.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opentelemetry-sdk/tests/trace/export/test_export.py b/opentelemetry-sdk/tests/trace/export/test_export.py index f36c79ee910..cedb5967666 100644 --- a/opentelemetry-sdk/tests/trace/export/test_export.py +++ b/opentelemetry-sdk/tests/trace/export/test_export.py @@ -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 @@ -288,7 +289,7 @@ 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) + "\n") + 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)