fromflaskimportFlask,requestimportrequests,osfromddtraceimporttracerapp=Flask(__name__)defshutdown_server():func=request.environ.get('werkzeug.server.shutdown')iffuncisNone:raiseRuntimeError('Not running with the Werkzeug Server')func()@app.route('/a',methods=['GET'])defindex():requests.get('http://127.0.0.1:54322/b')return'OK',200@app.route('/stop',methods=['GET'])defstop():shutdown_server()return'Server shutting down...\n'# Start service A: HTTP service starts on port 54321if__name__=='__main__':app.run(host="0.0.0.0",port=54321,debug=True)
service_b.py
fromflaskimportFlask,requestimportos,time,requestsfromddtraceimporttracerapp=Flask(__name__)defshutdown_server():func=request.environ.get('werkzeug.server.shutdown')iffuncisNone:raiseRuntimeError('Not running with the Werkzeug Server')func()@app.route('/b',methods=['GET'])defindex():time.sleep(1)return'OK',200@app.route('/stop',methods=['GET'])defstop():shutdown_server()return'Server shutting down...\n'# Start service B: HTTP service starts on port 54322if__name__=='__main__':app.run(host="0.0.0.0",port=54322,debug=True)
Here, we take the commonly used Python Web Server Flask application as an example. In the example, SERVICE_A provides an HTTP service and calls the SERVICE_B HTTP service.
The common environment variables supported are as follows. For a complete list of Python environment variables, refer to the DataDog Official Documentation.
DD_ENV: Sets the environment variable for the service.
DD_VERSION: The version number of the APP.
DD_SERVICE: Used to set the application's service name. When integrating with web frameworks such as Pylons, Flask, or Django, this value is passed. For Tracing without web integration, it is recommended to set the service name in the code.
DD_SERVICE_MAPPING: Defines service name mappings for renaming services in Tracing.
DD_TAGS: Adds default Tags to each Span in the format key:val,key:val.
DD_AGENT_HOST: The hostname where Datakit is listening, default is localhost.
DD_AGENT_PORT: The port number where Datakit is listening, default is 9529.
DD_TRACE_SAMPLE_RATE: Sets the sampling rate from 0.0 (0%) to 1.0 (100%).