Skip to content
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

Console exporter #156

Merged
merged 4 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")