Created
October 24, 2017 15:16
-
-
Save etano/a81b138e545021b23dba9afc0c3608b9 to your computer and use it in GitHub Desktop.
Demo Flask services with Jaeger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
Run
Start Jaeger
Start one service
Start another service in a different session
Go to http://localhost:8080 and refresh a few times.
See results at http://localhost:16686/ .