Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60fe160

Browse files
authored
Console exporter (open-telemetry#156)
1 parent ca10173 commit 60fe160

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ pip install -e ./ext/opentelemetry-ext-{integration}
4343
from opentelemetry import trace
4444
from opentelemetry.context import Context
4545
from opentelemetry.sdk.trace import Tracer
46+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
47+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
4648

4749
trace.set_preferred_tracer_implementation(lambda T: Tracer())
4850
tracer = trace.tracer()
51+
tracer.add_span_processor(
52+
SimpleExportSpanProcessor(ConsoleSpanExporter())
53+
)
4954
with tracer.start_span('foo'):
50-
print(Context)
5155
with tracer.start_span('bar'):
52-
print(Context)
5356
with tracer.start_span('baz'):
5457
print(Context)
55-
print(Context)
56-
print(Context)
5758
```
5859

5960
See [opentelemetry-example-app](./opentelemetry-example-app/README.rst) for a complete example.

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,20 @@ def __init__(
280280
self.start_time = None
281281

282282
def __repr__(self):
283-
return '{}(name="{}")'.format(type(self).__name__, self.name)
283+
return '{}(name="{}", context={})'.format(
284+
type(self).__name__, self.name, self.context
285+
)
286+
287+
def __str__(self):
288+
return '{}(name="{}", context={}, kind={}, parent={}, start_time={}, end_time={})'.format(
289+
type(self).__name__,
290+
self.name,
291+
self.context,
292+
self.kind,
293+
repr(self.parent),
294+
util.ns_to_iso_str(self.start_time) if self.start_time else "None",
295+
util.ns_to_iso_str(self.end_time) if self.end_time else "None",
296+
)
284297

285298
def get_context(self):
286299
return self.context

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@ def on_end(self, span: Span) -> None:
7676

7777
def shutdown(self) -> None:
7878
self.span_exporter.shutdown()
79+
80+
81+
class ConsoleSpanExporter(SpanExporter):
82+
"""Implementation of :class:`SpanExporter` that prints spans to the
83+
console.
84+
85+
This class can be used for diagnostic purposes. It prints the exported
86+
spans to the console STDOUT.
87+
"""
88+
89+
def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
90+
for span in spans:
91+
print(span)
92+
return SpanExportResult.SUCCESS

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/in_memory_span_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class InMemorySpanExporter(SpanExporter):
23-
"""Implementation of :class:`.Exporter` that stores spans in memory.
23+
"""Implementation of :class:`.SpanExporter` that stores spans in memory.
2424
2525
This class can be used for testing purposes. It stores the exported spans
2626
in a list in memory that can be retrieved using the

opentelemetry-sdk/src/opentelemetry/sdk/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import datetime
1516
import time
1617

1718
try:
@@ -21,3 +22,9 @@
2122

2223
def time_ns():
2324
return int(time.time() * 1e9)
25+
26+
27+
def ns_to_iso_str(nanoseconds):
28+
"""Get an ISO 8601 string from time_ns value."""
29+
ts = datetime.datetime.fromtimestamp(nanoseconds / 1e9)
30+
return ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

0 commit comments

Comments
 (0)