Skip to content

Commit 7671f96

Browse files
authored
Merge pull request #40 from lightstep/katia/python-3-compatible
All examples are python3 and python2 compatible
2 parents 72501f4 + 8b3a186 commit 7671f96

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ Please see the [example programs](examples/) for examples of how to use this lib
1818
* [Trivial Example](examples/trivial/main.py) shows how to use the library on a single host.
1919
* [Context in Headers](examples/http/context_in_headers.py) shows how to pass a `TraceContext` through `HTTP` headers.
2020

21+
You can run the examples by doing:
22+
```python
23+
tox
24+
source .tox/py27/bin/activate
25+
python examples/nontrivial/main.py
26+
```
27+
2128
Or if your python code is already instrumented for OpenTracing, you can simply switch to LightStep's implementation with:
2229

2330
```python

examples/http/context_in_headers.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@
1010
import socket
1111
import sys
1212
import threading
13-
import urllib2
1413

15-
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
14+
try:
15+
# For Python 3.0 and later
16+
from urllib.request import (
17+
Request,
18+
urlopen,
19+
)
20+
from http.server import BaseHTTPRequestHandler, HTTPServer
21+
except ImportError:
22+
# Fall back to Python 2
23+
from urllib2 import (
24+
Request,
25+
urlopen,
26+
)
27+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
28+
1629

1730
import opentracing
1831
import opentracing.ext.tags
@@ -29,7 +42,7 @@ def do_GET(self):
2942
self.send_response(200)
3043
self.send_header('Content-type', 'text/html')
3144
self.end_headers()
32-
self.wfile.write("Hello World!")
45+
self.wfile.write("Hello World!".encode("utf-8"))
3346

3447
server_span.log_event('prepared response', self.path)
3548

@@ -39,13 +52,19 @@ def before_sending_request(request):
3952
"""
4053
span = opentracing.tracer.start_span('Sending request')
4154
span.set_tag('server.http.url', request.get_full_url())
42-
host = request.get_host()
55+
try:
56+
# Python 2
57+
host = request.get_host()
58+
except:
59+
# Python 3
60+
host = request.host
61+
4362
if host:
4463
span.set_tag(opentracing.ext.tags.PEER_HOST_IPV4, host)
4564

4665
carrier_dict = {}
4766
span.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier_dict)
48-
for k, v in carrier_dict.iteritems():
67+
for k, v in carrier_dict.items():
4968
request.add_header(k, v)
5069
return span
5170

@@ -65,13 +84,13 @@ def before_answering_request(handler, tracer):
6584
operation_name=operation,
6685
child_of=extracted_context)
6786
else:
68-
print 'ERROR: Context missing, starting new trace'
87+
print('ERROR: Context missing, starting new trace')
6988
global _exit_code
7089
_exit_code = errno.ENOMSG
7190
span = tracer.start_span(operation_name=operation)
7291
headers = ', '.join({k + '=' + v for k, v in handler.headers.items()})
7392
span.log_event('extract_failed', headers)
74-
print 'Could not extract context from http headers: ' + headers
93+
print('Could not extract context from http headers: ' + headers)
7594

7695
host, port = handler.client_address
7796
if host:
@@ -131,24 +150,24 @@ def lightstep_tracer_from_args():
131150
# Run the server in a separate thread.
132151
server_thread = threading.Thread(target=server.serve_forever)
133152
server_thread.start()
134-
print 'Started httpserver on port ', port_number
153+
print('Started httpserver on port ', port_number)
135154

136155
# Prepare request in the client
137156
url = 'http://localhost:{}'.format(port_number)
138-
request = urllib2.Request(url)
157+
request = Request(url)
139158
with before_sending_request(request) as client_span:
140159
client_span.log_event('sending request', url)
141160

142161
# Send request to server
143-
response = urllib2.urlopen(request)
162+
response = urlopen(request)
144163

145164
response_body = response.read()
146165
client_span.log_event('server returned', {
147166
"code": response.code,
148167
"body": response_body,
149168
})
150169

151-
print 'Server returned ' + str(response.code) + ': ' + response_body
170+
print('Server returned ' + str(response.code) + ': ' + str(response_body))
152171

153172
sys.exit(_exit_code)
154173

examples/nontrivial/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def lightstep_tracer_from_args():
8484

8585

8686
if __name__ == '__main__':
87-
print 'Hello ',
87+
print('Hello '),
8888

8989
# Use LightStep's opentracing implementation
9090
with lightstep_tracer_from_args() as tracer:
@@ -98,6 +98,6 @@ def lightstep_tracer_from_args():
9898
t.start()
9999
for t in threads:
100100
t.join()
101-
print '\n'
101+
print('\n')
102102

103-
print ' World!'
103+
print(' World!')

examples/trivial/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ def lightstep_tracer_from_args():
7878

7979

8080
if __name__ == '__main__':
81-
print 'Hello ',
81+
print('Hello ')
8282

8383
# Use LightStep's opentracing implementation
8484
with lightstep_tracer_from_args() as tracer:
8585
opentracing.tracer = tracer
8686
add_spans()
8787

88-
print ' World!'
88+
print(' World!')

0 commit comments

Comments
 (0)