Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit b106545

Browse files
authored
Batch exporting spans once a while (#63)
1 parent 26374d4 commit b106545

20 files changed

Lines changed: 841 additions & 32 deletions

trace/opencensus/trace/exporters/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ class Exporter(object):
2020
2121
Subclasses of :class:`Exporter` must override :meth:`export`.
2222
"""
23+
def emit(self, trace):
24+
"""Emit the trace."""
25+
raise NotImplementedError
2326

2427
def export(self, trace):
25-
"""Export the trace."""
28+
"""Export the trace. Send trace to transport, and transport will call
29+
exporter.emit() to actually send the trace to the specified tracing
30+
backend.
31+
"""
2632
raise NotImplementedError

trace/opencensus/trace/exporters/file_exporter.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818

1919
from opencensus.trace.exporters import base
20+
from opencensus.trace.exporters.transports import sync
2021

2122
DEFAULT_FILENAME = 'opencensus-traces.json'
2223

@@ -25,16 +26,28 @@ class FileExporter(base.Exporter):
2526
"""
2627
:type file_name: str
2728
:param file_name: The name of the output file.
29+
30+
:type transport: :class:`type`
31+
:param transport: Class for creating new transport objects. It should
32+
extend from the base :class:`.Transport` type and
33+
implement :meth`.Transport.export`. Defaults to
34+
:class:`.SyncTransport`. The other option is
35+
:class:`.BackgroundThreadTransport`.
2836
"""
2937

30-
def __init__(self, file_name=DEFAULT_FILENAME):
38+
def __init__(self, file_name=DEFAULT_FILENAME,
39+
transport=sync.SyncTransport):
3140
self.file_name = file_name
41+
self.transport = transport(self)
3242

33-
def export(self, trace):
43+
def emit(self, trace):
3444
"""
3545
:type trace: dict
3646
:param trace: Trace collected.
3747
"""
3848
with open(self.file_name, 'w+') as file:
3949
trace_str = json.dumps(trace)
4050
file.write(trace_str)
51+
52+
def export(self, trace):
53+
self.transport.export(trace)

trace/opencensus/trace/exporters/logging_exporter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818

1919
from opencensus.trace.exporters import base
20+
from opencensus.trace.exporters.transports import sync
2021

2122

2223
class LoggingExporter(base.Exporter):
@@ -26,6 +27,13 @@ class LoggingExporter(base.Exporter):
2627
:type handler: :class:`logging.handler`
2728
:param handler: the handler to attach to the global handler
2829
30+
:type transport: :class:`type`
31+
:param transport: Class for creating new transport objects. It should
32+
extend from the base :class:`.Transport` type and
33+
implement :meth`.Transport.export`. Defaults to
34+
:class:`.SyncTransport`. The other option is
35+
:class:`.BackgroundThreadTransport`.
36+
2937
Example:
3038
3139
.. code-block:: python
@@ -44,7 +52,7 @@ class LoggingExporter(base.Exporter):
4452
will be exported to logging when finished.
4553
"""
4654

47-
def __init__(self, handler=None):
55+
def __init__(self, handler=None, transport=sync.SyncTransport):
4856
self.logger = logging.getLogger()
4957

5058
if handler is None:
@@ -53,10 +61,14 @@ def __init__(self, handler=None):
5361
self.handler = handler
5462
self.logger.addHandler(handler)
5563
self.logger.setLevel(logging.INFO)
64+
self.transport = transport(self)
5665

57-
def export(self, trace):
66+
def emit(self, trace):
5867
"""
5968
:type traces: dict
6069
:param traces: Trace collected.
6170
"""
6271
self.logger.info(trace)
72+
73+
def export(self, trace):
74+
self.transport.export(trace)

trace/opencensus/trace/exporters/print_exporter.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@
1515
"""Export the trace spans by printing them out."""
1616

1717
from opencensus.trace.exporters import base
18+
from opencensus.trace.exporters.transports import sync
1819

1920

2021
class PrintExporter(base.Exporter):
21-
def export(self, trace):
22+
"""Export the spans by printing them.
23+
24+
:type transport: :class:`type`
25+
:param transport: Class for creating new transport objects. It should
26+
extend from the base :class:`.Transport` type and
27+
implement :meth`.Transport.export`. Defaults to
28+
:class:`.SyncTransport`. The other option is
29+
:class:`.BackgroundThreadTransport`.
30+
"""
31+
def __init__(self, transport=sync.SyncTransport):
32+
self.transport = transport(self)
33+
34+
def emit(self, trace):
2235
"""export the traces by printing it out.
2336
2437
:type trace: dict
@@ -29,3 +42,6 @@ def export(self, trace):
2942
"""
3043
print(trace)
3144
return trace
45+
46+
def export(self, trace):
47+
self.transport.export(trace)

trace/opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616

1717
from opencensus.trace.exporters import base
18+
from opencensus.trace.exporters.transports import sync
1819

1920
from google.cloud.trace.client import Client
2021

@@ -73,23 +74,41 @@ def is_gae_environment():
7374
class StackdriverExporter(base.Exporter):
7475
"""A exporter that send traces and trace spans to Google Cloud Stackdriver
7576
Trace.
77+
78+
:type client: :class: `~google.cloud.trace.client.Client`
79+
:param client: Stackdriver Trace client.
80+
81+
:type project_id: str
82+
:param project_id: project_id to create the Trace client.
83+
84+
:type transport: :class:`type`
85+
:param transport: Class for creating new transport objects. It should
86+
extend from the base :class:`.Transport` type and
87+
implement :meth`.Transport.export`. Defaults to
88+
:class:`.SyncTransport`. The other option is
89+
:class:`.BackgroundThreadTransport`.
7690
"""
77-
def __init__(self, client=None, project_id=None):
91+
def __init__(self, client=None, project_id=None,
92+
transport=sync.SyncTransport):
7893
# The client will handler the case when project_id is None
7994
if client is None:
8095
client = Client(project=project_id)
8196

8297
self.client = client
8398
self.project_id = client.project
99+
self.transport = transport(self)
84100

85-
def export(self, trace):
101+
def emit(self, trace):
86102
"""
87103
:type trace: dict
88104
:param trace: Trace collected.
89105
"""
90106
stackdriver_traces = self.translate_to_stackdriver(trace)
91107
self.client.patch_traces(stackdriver_traces)
92108

109+
def export(self, trace):
110+
self.transport.export(trace)
111+
93112
def translate_to_stackdriver(self, trace):
94113
"""
95114
:type trace: dict
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2017, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

0 commit comments

Comments
 (0)