|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +A sample app demonstrating Stackdriver Trace |
| 16 | +""" |
| 17 | + |
| 18 | +from flask import Flask |
| 19 | +from opencensus.trace import execution_context |
| 20 | +from opencensus.trace.propagation import google_cloud_format |
| 21 | +from opencensus.trace.samplers import AlwaysOnSampler |
| 22 | +from opencensus.ext.stackdriver.trace_exporter import StackdriverExporter |
| 23 | +from opencensus.ext.flask.flask_middleware import FlaskMiddleware |
| 24 | + |
| 25 | +import requests |
| 26 | +import argparse |
| 27 | +import time |
| 28 | +import random |
| 29 | + |
| 30 | +app = Flask(__name__) |
| 31 | + |
| 32 | +propagator = google_cloud_format.GoogleCloudFormatPropagator() |
| 33 | + |
| 34 | + |
| 35 | +def createMiddleWare(exporter): |
| 36 | + # Configure a flask middleware that listens for each request and applies automatic tracing. |
| 37 | + # This needs to be set up before the application starts. |
| 38 | + middleware = FlaskMiddleware( |
| 39 | + app, |
| 40 | + exporter=exporter, |
| 41 | + propagator=propagator, |
| 42 | + sampler=AlwaysOnSampler()) |
| 43 | + return middleware |
| 44 | + |
| 45 | + |
| 46 | +@app.route('/') |
| 47 | +def template_test(): |
| 48 | + # Sleep for a random time to imitate a random processing time |
| 49 | + time.sleep(random.uniform(0, 0.5)) |
| 50 | + # Keyword that gets passed in will be concatenated to the final output string. |
| 51 | + output_string = app.config['keyword'] |
| 52 | + # If there is no endpoint, return the output string. |
| 53 | + url = app.config['endpoint'] |
| 54 | + if url == "": |
| 55 | + return output_string, 200 |
| 56 | + # Endpoint is the next service to send string to. |
| 57 | + data = {'body': output_string} |
| 58 | + trace_context_header = propagator.to_header(execution_context.get_opencensus_tracer().span_context) |
| 59 | + response = requests.get( |
| 60 | + url, |
| 61 | + params=data, |
| 62 | + headers={ |
| 63 | + 'X-Cloud-Trace-Context' : trace_context_header} |
| 64 | + ) |
| 65 | + return response.text + app.config['keyword'] |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + parser = argparse.ArgumentParser() |
| 70 | + parser.add_argument("--keyword", default="", help="name of the service.") |
| 71 | + parser.add_argument("--endpoint", default="", help="endpoint to dispatch appended string, simply respond if not set") |
| 72 | + args = parser.parse_args() |
| 73 | + app.config['keyword'] = args.keyword |
| 74 | + app.config['endpoint'] = args.endpoint |
| 75 | + createMiddleWare(StackdriverExporter()) |
| 76 | + app.run(debug=True, host='0.0.0.0', port=8080) |
0 commit comments