-
Notifications
You must be signed in to change notification settings - Fork 656
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
Custom output function in console exporter #411
Comments
I see a lot of value in this! In fact, some time ago I had modified the exporter in place to print the output in a dictionary/JSON-like output where the parent-child relationship of spans could be displayed as they were nested. I think it is better to leave Sure, open a PR with your code, please 👍 |
Hey @AntonKueltz, welcome to the project. I agree wth making class ConsoleSpanExporter(SpanExporter):
"""Implementation of :class:`SpanExporter` that prints spans to the
console.
This class can be used for diagnostic purposes. It prints the exported
spans to the console STDOUT.
"""
def __init__(
self,
out: typing.IO = sys.stdout,
munge: typing.Callable[[Span], str] = str,
):
self.out = out
self.munge = format
def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
for span in spans:
self.out.write(self.munge(span))
return SpanExportResult.SUCCESS It's a pretty short diff, and doesn't affect the default behavior of the exporter: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
index aa8efac3..d64d9323 100644
--- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
@@ -14,6 +14,7 @@
import collections
import logging
+import sys
import threading
import typing
from enum import Enum
@@ -268,7 +269,15 @@ class ConsoleSpanExporter(SpanExporter):
spans to the console STDOUT.
"""
+ def __init__(
+ self,
+ out: typing.IO = sys.stdout,
+ munge: typing.Callable[[Span], str] = str,
+ ):
+ self.out = out
+ self.munge = format
+
def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
for span in spans:
- print(span)
+ self.out.write(self.munge(span))
return SpanExportResult.SUCCESS |
Thanks for the feedback @ocelotl and @c24t. The code @c24t provided is pretty much what I had in mind (with the nice addition of also being able to define the output stream). Regarding the I'll go ahead and PR the changes I have (to include the custom output stream as well) unless anyone has any further comments or concerns? Edit - @c24t beat me to the PR haha |
Nope, this looks good to me, thanks for the quick turnaround! |
What do folks think of making the output function used by
ConsoleSpanExporter.export
customizable? Currently it usesprint(span)
but there are situations where a user may want to perform a transform onspan
before outputting it, or to output another data structure entirely.To be more concrete, we currently have downstream infrastructure that requires our telemetry data to be in a specific dictionary format when written to stdout. We'd like to still use the
ConsoleSpanExporter
to write to stdout, but need to convert thespan
to ourdict
object before doing so.Currently we are addressing this by writing a custom exporter class that inherits from
SpanExporter
and writes our custom data to stdout in itsexport
method. Another solution though would be to allowConsoleSpanExporter
to take a keyword argument to its__init__
method that would default toprint
but would also allow custom output functions. It would then use the passed function inConsoleSpanExporter.export
to output to stdout.Do folks see any value in adding this functionality here, rather than requiring users who want to do custom console output to write their own exporter classes? Also happy to write up the proposed solution as a PR if folks think that would be helpful.
The text was updated successfully, but these errors were encountered: