Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
18 changes: 18 additions & 0 deletions opencensus/trace/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ def export(self, span_datas):
SpanData tuples to export
"""
raise NotImplementedError


def init_transport(exporter, transport, config=None):
"""Initiate a transport instance to be used by the exporter.

:type exporter: :class: `~opencensus.trace.exporters.base.Exporter`
:param exporter exporter instance.

:type transport: :class:
`~opencensus.trace.exporters.transports.base.Transport`
:param transport class to use for the exporter.

:type config: :class:`dict`
:param dict of config options to initiate the transport with.
"""
if config is None:
return transport(exporter)
return transport(exporter, **config)
17 changes: 9 additions & 8 deletions opencensus/trace/exporters/file_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ class FileExporter(base.Exporter):
:type file_name: str
:param file_name: The name of the output file.

:type file_mode: str
:param file_mode: The file mode to open the output file with.
Defaults to w+

:type transport: :class:`type`
:param transport: Class for creating new transport objects. It should
extend from the base :class:`.Transport` type and
implement :meth:`.Transport.export`. Defaults to
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type file_mode: str
:param file_mode: The file mode to open the output file with.
Defaults to w+

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.
"""

def __init__(self, file_name=DEFAULT_FILENAME,
transport=sync.SyncTransport,
file_mode='w+'):
def __init__(self, file_name=DEFAULT_FILENAME, file_mode='w+',
transport=sync.SyncTransport, transport_config=None):
self.file_name = file_name
self.transport = transport(self)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder why we're initializing the transport here instead of passing the exporter constructor a preconfigured transport. This may be a simpler solution to your problem.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@c24t, I suggested this in #249 (pass an object instead), but add a configuration parameter could be interesting for backwards compatibility.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with @felippe-mendonca, this is mostly to keep backwards compatibility while giving more options. Maybe we can think of a more global reformat later on.

self.file_mode = file_mode
self.transport = base.init_transport(self, transport, transport_config)

def emit(self, span_datas):
"""
Expand Down
8 changes: 6 additions & 2 deletions opencensus/trace/exporters/jaeger_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class JaegerExporter(base.Exporter):
implement :meth:`.Transport.export`. Defaults to
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.
"""

def __init__(
Expand All @@ -85,8 +88,8 @@ def __init__(
agent_host_name=DEFAULT_HOST_NAME,
agent_port=DEFAULT_AGENT_PORT,
agent_endpoint=DEFAULT_ENDPOINT,
transport=sync.SyncTransport):
self.transport = transport(self)
transport=sync.SyncTransport,
transport_config=None):
self.service_name = service_name
self.host_name = host_name
self.agent_host_name = agent_host_name
Expand All @@ -97,6 +100,7 @@ def __init__(
self.password = password
self._agent_client = None
self._collector = None
self.transport = base.init_transport(self, transport, transport_config)

@property
def agent_client(self):
Expand Down
8 changes: 6 additions & 2 deletions opencensus/trace/exporters/logging_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class LoggingExporter(base.Exporter):
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.

Example:

.. code-block:: python
Expand All @@ -53,7 +56,8 @@ class LoggingExporter(base.Exporter):
will be exported to logging when finished.
"""

def __init__(self, handler=None, transport=sync.SyncTransport):
def __init__(self, handler=None, transport=sync.SyncTransport,
transport_config=None):
self.logger = logging.getLogger()

if handler is None:
Expand All @@ -62,7 +66,7 @@ def __init__(self, handler=None, transport=sync.SyncTransport):
self.handler = handler
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)
self.transport = transport(self)
self.transport = base.init_transport(self, transport, transport_config)

def emit(self, span_datas):
"""
Expand Down
7 changes: 5 additions & 2 deletions opencensus/trace/exporters/print_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class PrintExporter(base.Exporter):
implement :meth:`.Transport.export`. Defaults to
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.
"""

def __init__(self, transport=sync.SyncTransport):
self.transport = transport(self)
def __init__(self, transport=sync.SyncTransport, transport_config=None):
self.transport = base.init_transport(self, transport, transport_config)

def emit(self, span_datas):
"""
Expand Down
7 changes: 5 additions & 2 deletions opencensus/trace/exporters/stackdriver_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,20 @@ class StackdriverExporter(base.Exporter):
implement :meth:`.Transport.export`. Defaults to
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.
"""

def __init__(self, client=None, project_id=None,
transport=sync.SyncTransport):
transport=sync.SyncTransport, transport_config=None):
# The client will handle the case when project_id is None
if client is None:
client = Client(project=project_id)

self.client = client
self.project_id = client.project
self.transport = transport(self)
self.transport = base.init_transport(self, transport, transport_config)

def emit(self, span_datas):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(self, exporter, grace_period=_DEFAULT_GRACE_PERIOD,
self.worker.start()

def export(self, span_datas):
"""Put the trace to be exported into queue."""
"""Put the traces to be exported into queue."""
self.worker.enqueue(span_datas)

def flush(self):
Expand Down
6 changes: 5 additions & 1 deletion opencensus/trace/exporters/zipkin_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class ZipkinExporter(base.Exporter):
implement :meth:`.Transport.export`. Defaults to
:class:`.SyncTransport`. The other option is
:class:`.BackgroundThreadTransport`.

:type transport_config: :class:`dict`
:param transport_config: Transport configuration dictionary.
"""

def __init__(
Expand All @@ -75,6 +78,7 @@ def __init__(
endpoint=DEFAULT_ENDPOINT,
protocol=DEFAULT_PROTOCOL,
transport=sync.SyncTransport,
transport_config=None,
ipv4=None,
ipv6=None):
self.service_name = service_name
Expand All @@ -83,9 +87,9 @@ def __init__(
self.endpoint = endpoint
self.protocol = protocol
self.url = self.get_url
self.transport = transport(self)
self.ipv4 = ipv4
self.ipv6 = ipv6
self.transport = base.init_transport(self, transport, transport_config)

@property
def get_url(self):
Expand Down