Skip to content

Instantly share code, notes, and snippets.

@etano
Created October 24, 2017 15:16
Show Gist options
  • Select an option

  • Save etano/a81b138e545021b23dba9afc0c3608b9 to your computer and use it in GitHub Desktop.

Select an option

Save etano/a81b138e545021b23dba9afc0c3608b9 to your computer and use it in GitHub Desktop.
Demo Flask services with Jaeger
from flask import Flask
from flask_opentracing import FlaskTracer
from jaeger_client import Config
import opentracing
import time
import urllib2
app = Flask(__name__)
# one-time tracer initialization code
config = Config(
config={ # usually read from some yaml config
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name='service0'
)
ot_tracer = config.initialize_tracer()
# This tracer traces all requests
tracer = FlaskTracer(ot_tracer, True, app, ["url_rule"])
@app.route("/")
def create_child_span():
parent_span = tracer.get_span()
child_span = ot_tracer.start_span("inside service0 span", child_of=parent_span)
request = urllib2.Request("http://localhost:8081/")
inject_as_headers(ot_tracer, child_span, request)
try:
response = urllib2.urlopen(request)
except urllib2.URLError as ue:
response = ue
child_span.finish()
time.sleep(1)
return 'OK', 200
def inject_as_headers(tracer, span, request):
text_carrier = {}
tracer.inject(span.context, opentracing.Format.TEXT_MAP, text_carrier)
for k, v in text_carrier.iteritems():
request.add_header(k,v)
if __name__ == '__main__':
app.run(host='localhost', port=8080)
from flask import Flask
from flask_opentracing import FlaskTracer
from jaeger_client import Config
import opentracing
import time
app = Flask(__name__)
# one-time tracer initialization code
config = Config(
config={ # usually read from some yaml config
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name='service1'
)
ot_tracer = config.initialize_tracer()
# This tracer traces all requests
tracer = FlaskTracer(ot_tracer, True, app, ["url_rule"])
@app.route("/")
def create_child_span():
parent_span = tracer.get_span()
child_span = ot_tracer.start_span("inside service1 span", child_of=parent_span)
time.sleep(1)
child_span.finish()
time.sleep(1)
return 'OK', 200
if __name__ == '__main__':
app.run(host='localhost', port=8081)
@etano
Copy link
Author

etano commented Oct 24, 2017

Install

sudo pip install -r requirements.txt

Run

Start Jaeger

docker run -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp \
    -p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one:latest

Start one service

python service0.py

Start another service in a different session

python service1.py

Go to http://localhost:8080 and refresh a few times.

See results at http://localhost:16686/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment