diff --git a/opencensus/trace/exporters/base.py b/opencensus/trace/exporters/base.py index 8011e86fc..584482a18 100644 --- a/opencensus/trace/exporters/base.py +++ b/opencensus/trace/exporters/base.py @@ -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) diff --git a/opencensus/trace/exporters/file_exporter.py b/opencensus/trace/exporters/file_exporter.py index f250d1e15..2d81775f4 100644 --- a/opencensus/trace/exporters/file_exporter.py +++ b/opencensus/trace/exporters/file_exporter.py @@ -28,6 +28,10 @@ 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 @@ -35,18 +39,15 @@ class FileExporter(base.Exporter): :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) self.file_mode = file_mode + self.transport = base.init_transport(self, transport, transport_config) def emit(self, span_datas): """ diff --git a/opencensus/trace/exporters/jaeger_exporter.py b/opencensus/trace/exporters/jaeger_exporter.py index 32024e532..ddd030e43 100644 --- a/opencensus/trace/exporters/jaeger_exporter.py +++ b/opencensus/trace/exporters/jaeger_exporter.py @@ -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__( @@ -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 @@ -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): diff --git a/opencensus/trace/exporters/logging_exporter.py b/opencensus/trace/exporters/logging_exporter.py index 83002d217..71bad03d0 100644 --- a/opencensus/trace/exporters/logging_exporter.py +++ b/opencensus/trace/exporters/logging_exporter.py @@ -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 @@ -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: @@ -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): """ diff --git a/opencensus/trace/exporters/print_exporter.py b/opencensus/trace/exporters/print_exporter.py index 72a30bcf9..abdf5288e 100644 --- a/opencensus/trace/exporters/print_exporter.py +++ b/opencensus/trace/exporters/print_exporter.py @@ -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): """ diff --git a/opencensus/trace/exporters/stackdriver_exporter.py b/opencensus/trace/exporters/stackdriver_exporter.py index 95ae9f7ab..ffafd709c 100644 --- a/opencensus/trace/exporters/stackdriver_exporter.py +++ b/opencensus/trace/exporters/stackdriver_exporter.py @@ -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): """ diff --git a/opencensus/trace/exporters/transports/background_thread.py b/opencensus/trace/exporters/transports/background_thread.py index 0768ee737..1b02be4d9 100644 --- a/opencensus/trace/exporters/transports/background_thread.py +++ b/opencensus/trace/exporters/transports/background_thread.py @@ -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): diff --git a/opencensus/trace/exporters/zipkin_exporter.py b/opencensus/trace/exporters/zipkin_exporter.py index cd1d21352..23e967b8b 100644 --- a/opencensus/trace/exporters/zipkin_exporter.py +++ b/opencensus/trace/exporters/zipkin_exporter.py @@ -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__( @@ -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 @@ -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):