Skip to content

Commit

Permalink
Console exporter (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Sep 20, 2019
1 parent ca10173 commit 60fe160
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ pip install -e ./ext/opentelemetry-ext-{integration}
from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.sdk.trace import Tracer
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

trace.set_preferred_tracer_implementation(lambda T: Tracer())
tracer = trace.tracer()
tracer.add_span_processor(
SimpleExportSpanProcessor(ConsoleSpanExporter())
)
with tracer.start_span('foo'):
print(Context)
with tracer.start_span('bar'):
print(Context)
with tracer.start_span('baz'):
print(Context)
print(Context)
print(Context)
```

See [opentelemetry-example-app](./opentelemetry-example-app/README.rst) for a complete example.
Expand Down
15 changes: 14 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,20 @@ def __init__(
self.start_time = None

def __repr__(self):
return '{}(name="{}")'.format(type(self).__name__, self.name)
return '{}(name="{}", context={})'.format(
type(self).__name__, self.name, self.context
)

def __str__(self):
return '{}(name="{}", context={}, kind={}, parent={}, start_time={}, end_time={})'.format(
type(self).__name__,
self.name,
self.context,
self.kind,
repr(self.parent),
util.ns_to_iso_str(self.start_time) if self.start_time else "None",
util.ns_to_iso_str(self.end_time) if self.end_time else "None",
)

def get_context(self):
return self.context
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ def on_end(self, span: Span) -> None:

def shutdown(self) -> None:
self.span_exporter.shutdown()


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 export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
for span in spans:
print(span)
return SpanExportResult.SUCCESS
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class InMemorySpanExporter(SpanExporter):
"""Implementation of :class:`.Exporter` that stores spans in memory.
"""Implementation of :class:`.SpanExporter` that stores spans in memory.
This class can be used for testing purposes. It stores the exported spans
in a list in memory that can be retrieved using the
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import time

try:
Expand All @@ -21,3 +22,9 @@

def time_ns():
return int(time.time() * 1e9)


def ns_to_iso_str(nanoseconds):
"""Get an ISO 8601 string from time_ns value."""
ts = datetime.datetime.fromtimestamp(nanoseconds / 1e9)
return ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

0 comments on commit 60fe160

Please sign in to comment.