在上一篇文章中,我们看到了仪器在途中使用Otel发射指标和痕迹的Python应用程序。虽然这很好,但它仍然可能对 instrumentation-newbie 。
为了克服这一障碍并允许快速启动,某些语言具有自动启动支持。一个人只需要安装一些库才能开始。
我们的示例Web-App
import datetime
import flask
######################
## initialization
######################
app = flask.Flask(__name__)
start = datetime.datetime.now()
######################
## routes
######################
@app.route('/', methods=['GET'])
def root():
return flask.jsonify({'message': 'flask app root/'})
@app.route('/healthz', methods=['GET'])
def healthz():
now = datetime.datetime.now()
return flask.jsonify({'message': f'up and running since {(now - start)}'})
######################
if __name__ == '__main__':
######################
app.run(debug=True, host='0.0.0.0', port=5000)
安装必要的库
$ pip install flask
$ pip install opentelemetry-distro opentelemetry-instrumentation-flask
注意:一些角案也使我的安装opentelemetry-api
也是如此,但根据官方文档不需要。
初始化(根据需要安装额外的库)
$ opentelemetry-bootstrap -a install
运行代码
$ opentelemetry-instrument --traces_exporter console --metrics_exporter console flask run
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://192.168.1.7:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 121-673-590
127.0.0.1 - - [17/Oct/2022 07:21:05] "GET /healthz HTTP/1.1" 200 -
{
"name": "/healthz",
"context": {
"trace_id": "0xd0850752865577d2d8cd11aaef169574",
"span_id": "0x29c8ad5fd974de41",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "2022-10-17T01:52:45.522806Z",
"end_time": "2022-10-17T01:52:45.523615Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.server_name": "127.0.0.1",
"http.scheme": "http",
"net.host.port": 5000,
"http.host": "localhost:5000",
"http.target": "/healthz",
"net.peer.ip": "127.0.0.1",
"http.user_agent": "curl/7.79.1",
"net.peer.port": 55838,
"http.flavor": "1.1",
"http.route": "/healthz",
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"attributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.13.0",
"telemetry.auto.version": "0.34b0",
"service.name": "unknown_service"
},
"schema_url": ""
}
}
{"resource_metrics": [{"resource": {"attributes": {"telemetry.sdk.language": "python", "telemetry.sdk.name": "opentelemetry", "telemetry.sdk.version": "1.13.0", "telemetry.auto.version": "0.34b0", "service.name": "unknown_service"}, "schema_url": ""}, "scope_metrics": [], "schema_url": ""}]}
从本文开始,诸如Django,Fastapi,Blask等最受欢迎的框架具有HTTP上下文传播的仪器库
代码大小的含义
自动启动确实增加了一些额外的库。这是我案件的结果
$ du -sh manual/venv/ auto/venv/
29M manual/venv/
30M auto/venv/
始终参考最新的正式文档
Official documentation