Skip to content
This repository was archived by the owner on Jul 21, 2021. It is now read-only.

Commit 96ebe40

Browse files
catvecyurishkuro
authored andcommitted
Add example usage (opentracing#33)
* added an example of basictracer usage to readme * set example language in readme * moved ex from readme into example dir * made more complete example, documented ex dir in readme * added more detailed header comment to ex * added example output to ex header comment * fixed orphaned word in ex header comment * made span logging ex output more useful * logging ex: fixed to use span id instead of trace id * made example log parent span id * updated log output in ex header comment
1 parent 4648833 commit 96ebe40

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
# Basictracer Python
44

5-
A python version of the "BasicTracer" reference implementation for OpenTracing.
5+
A python version of the "BasicTracer" reference implementation for OpenTracing.
6+
7+
The `examples/` directory contains a sample of how the BasicTracer
8+
implementation could be used to display spans in the console.
69

710
## Development
811

examples/span_logging.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
""" Example usage of the BasicTracer implementation which log spans to the console.
2+
3+
Ensure the following pip packages are installed:
4+
5+
- opentracing
6+
- basictracer
7+
8+
Run with the command:
9+
10+
python3 examples/span_logging.py
11+
12+
Example output:
13+
14+
[DEBUG ] span_logging.loop.14644595994193200868[1.00 S parent=14464006555861026741 i=0]: message=Sleeping for 1 second
15+
[DEBUG ] span_logging.loop.1520449992154412943[1.01 S parent=14464006555861026741 i=1]: message=Sleeping for 1 second
16+
[DEBUG ] span_logging.loop.15868927260288211418[1.00 S parent=14464006555861026741 i=2]: message=Sleeping for 1 second
17+
[DEBUG ] span_logging.loop.13311248096002911557[1.00 S parent=14464006555861026741 i=3]: message=Sleeping for 1 second
18+
[DEBUG ] span_logging.loop.13408887418100893456[1.00 S parent=14464006555861026741 i=4]: message=Sleeping for 1 second
19+
[DEBUG ] span_logging.loop.2166932256275619626[1.00 S parent=14464006555861026741 i=5]: message=Sleeping for 1 second
20+
[DEBUG ] span_logging.loop.17208766771418783859[1.00 S parent=14464006555861026741 i=6]: message=Sleeping for 1 second
21+
[DEBUG ] span_logging.loop.175072314141445432[1.00 S parent=14464006555861026741 i=7]: message=Sleeping for 1 second
22+
[DEBUG ] span_logging.loop.6741939691448627555[1.00 S parent=14464006555861026741 i=8]: message=Sleeping for 1 second
23+
[DEBUG ] span_logging.loop.8035570631348486191[1.00 S parent=14464006555861026741 i=9]: message=Sleeping for 1 second
24+
[DEBUG ] span_logging.main.14464006555861026741[10.03 S]: finished
25+
"""
26+
27+
import logging
28+
import sys
29+
import time
30+
31+
from opentracing import Tracer
32+
from basictracer import BasicTracer
33+
from basictracer.recorder import SpanRecorder
34+
35+
class LogSpanRecorder(SpanRecorder):
36+
""" Records spans by printing them to a log
37+
Fields:
38+
- logger (Logger): Logger used to display spans
39+
"""
40+
41+
def __init__(self, logger: logging.Logger):
42+
self.logger = logger
43+
44+
def record_span(self, span):
45+
bracket_items = [] # Information to put in log tag brackets
46+
47+
# Time
48+
duration_str = "{0:.2f} S".format(span.duration)
49+
50+
if span.duration < 0:
51+
duration_str = "{0:.2e} S".format(span.duration)
52+
53+
bracket_items.append(duration_str)
54+
55+
# Parent ID
56+
if span.parent_id is not None:
57+
bracket_items.append("parent={}".format(span.parent_id))
58+
59+
60+
# Tags
61+
tags_strs = ["{}={}".format(tag, span.tags[tag]) for tag in span.tags]
62+
bracket_items.extend(tags_strs)
63+
64+
# Create logger for span
65+
bracket_str = " ".join(bracket_items)
66+
67+
span_logger = self.logger.getChild("{}.{}[{}]"
68+
.format(span.operation_name, span.context.span_id,
69+
bracket_str))
70+
71+
# Print span logs
72+
if len(span.logs) > 0:
73+
for log in span.logs:
74+
log_str = " ".join(["{}={}".format(log_key, log.key_values[log_key]) for log_key in log.key_values])
75+
76+
span_logger.debug(log_str)
77+
else:
78+
# If no span logs exist simply print span finished
79+
span_logger.debug("finished")
80+
81+
def main():
82+
# Setup BasicTracer to log to console
83+
logger = logging.getLogger('span_logging')
84+
85+
logger.setLevel(logging.DEBUG)
86+
87+
hndlr = logging.StreamHandler(sys.stdout)
88+
hndlr.setFormatter(logging.Formatter("[%(levelname)-8s] %(name)s: %(message)s"))
89+
90+
logger.addHandler(hndlr)
91+
92+
recorder = LogSpanRecorder(logger)
93+
94+
tracer = BasicTracer(recorder=recorder)
95+
tracer.register_required_propagators()
96+
97+
# Use tracer to create spans
98+
span = tracer.start_span(operation_name='main')
99+
100+
for i in range(10):
101+
child_span = tracer.start_span(operation_name='loop', child_of=span)
102+
child_span.set_tag('i', i)
103+
104+
child_span.log_kv({'message': "Sleeping for 1 second"})
105+
106+
time.sleep(1)
107+
108+
child_span.finish()
109+
110+
span.finish()
111+
112+
if __name__ == '__main__':
113+
main()

0 commit comments

Comments
 (0)