赞
踩
参考资料
https://docs.amazonaws.cn/en_us/xray/latest/devguide/xray-daemon.html
https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/
https://github.com/aws/aws-xray-sdk-node
https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/basic.html
之前写过一篇eks使用xray的demo
xray daemon监听 UDP 端口2000上的通信量,收集原始段数据,并将其转发到 Amazon X-Ray API。xray daemon需要aws凭证才能将跟踪数据上传到xray服务。如果在ec2上运行,则获取并使用实例凭证。application的凭证由sdk确定凭证链。
xray daemon输出有关其当前配置的信息以及它发送给 X-Ray 的段。
最简单的方式是在ec2上运行xray deamon
#!/bin/bash
curl https://s3.us-east-2.amazonaws.com/aws-xray-assets.us-east-2/xray-daemon/aws-xray-daemon-3.x.rpm -o /home/ec2-user/xray.rpm
yum install -y /home/ec2-user/xray.rpm
之后查看系统进程
xray.service - AWS X-Ray Daemon
Loaded: loaded (/etc/systemd/system/xray.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2022-11-16 08:57:05 UTC; 18h ago
Main PID: 12231 (xray)
Tasks: 8
Memory: 12.7M
CGroup: /system.slice/xray.service
└─12231 /usr/bin/xray -f /var/log/xray/xray.log
Nov 16 08:57:05 ip-172-31-18-4.cn-north-1.compute.internal systemd[1]: Started AWS X-Ray Daemon.
输出日志如下
$ cat /var/log/xray/xray.log
2022-11-16T08:57:05Z [Info] Initializing AWS X-Ray daemon 3.3.5
2022-11-16T08:57:05Z [Info] Using buffer memory limit of 38 MB
2022-11-16T08:57:05Z [Info] 608 segment buffers allocated
2022-11-16T08:57:05Z [Info] Using region: cn-north-1
2022-11-16T08:57:05Z [Info] HTTP Proxy server using X-Ray Endpoint : https://xray.cn-north-1.amazonaws.com.cn
2022-11-16T08:57:05Z [Info] Starting proxy http server on 127.0.0.1:2000
此时xray daemon成功运行,注意:需要确保实例具有xray相关权限,如果修改实例凭证之后仍然没有权限,需要检查是否需要重启xray进程。
不同语言对于xray的支持是不同的,xray支持aws sdk,以及部分流行的web框架和三方库,这里以nodejs和python为例。安装和配置语言可以参考这篇blog
xray会跟踪数据,包括有关应用程序提供的传入 HTTP 请求的信息,以及应用程序使用 Amazon SDK 或 HTTP 客户机对下游服务进行的调用。
初始化
npm init -y
npm install aws-xray-sdk
npm intall aws-sdk
npm install express
// app.js var AWSXRay = require('aws-xray-sdk'); // xray将aws sdk包装 var AWS = AWSXRay.captureAWS(require('aws-sdk')); // var AWS = require('aws-sdk'); AWS.config.update({ region: 'cn-north-1' }); // 指定xray守护进程监听地址 AWSXRay.setDaemonAddress('127.0.0.1:2000'); const express = require('express') const app = express() const port = 3000 app.use(AWSXRay.express.openSegment('MyApp')); app.get('/', (req, res) => { var document = AWSXRay.getSegment(); // 添加注释和元数据 document.addAnnotation("mykey", "my value"); document.addMetadata("my key", "my value", "my namespace"); res.send('Hello World!') var s3 = new AWS.S3(); var params = {}; s3.listBuckets(params, function (err, data) { if (err) console.log(err, err.stack); else console.log(data); }); }) app.use(AWSXRay.express.closeSegment()); app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
启动并访问
node app.js
curl 127.0.0.1:3000
查看xray日志,已经将分段成功发送
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.014 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.015 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.005 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.006 seconds)
查看trace
查看定义的注释和元数据
初始化
pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install boto3
pip install requests
pip install aws-xray-sdk
pip install Flask
# myreq.py import boto3 import requests from aws_xray_sdk.core import patch, xray_recorder ## xray将aws sdk包装 SUPPORTED_MODULES = ('requests','boto3') patch(SUPPORTED_MODULES) # 指定xray守护进程监听地址 xray_recorder.configure(service ="my app",daemon_address="127.0.0.1:2000" ) @xray_recorder.capture('mysubfunc') def func(): r = requests.get('https://www.example.com/') if __name__ == "__main__": a = xray_recorder.begin_segment('myseg') r1 = requests.get('https://www.example.com/') print(r1.status_code) # test subseg xray_recorder.begin_subsegment('mysubs1') r2 = requests.get('https://www.example.com/') print(r2.status_code) xray_recorder.end_subsegment() # test func func() xray_recorder.end_segment() print(a) # test aws sdk xray_recorder.begin_segment('myboto3') s3 = boto3.resource('s3') # print out bucket names for bucket in s3.buckets.all(): print(bucket.name) xray_recorder.end_segment()
发送请求测试
python myreq.py
查看自定义分段
查看s3调用
# myflask.py
from aws_xray_sdk.core import xray_recorder
# 导入xray中间件
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
from flask import Flask
app = Flask(__name__)
# 指定xray守护进程监听地址
xray_recorder.configure(service ="my app",daemon_address="127.0.0.1:2000" )
XRayMiddleware(app, xray_recorder)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
启动和访问
flask --app myflask run
* Serving Flask app 'hello'
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
查看xray日志
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.014 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.015 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.005 seconds)
2022-11-16Txx:xx:xxZ [Info] Successfully sent batch of 1 segments (0.006 seconds)
查看trace
查看rawdata
{ "Id": "1-6375b820-29abfda99cb913ae91e21581", "Duration": 0.001, "LimitExceeded": false, "Segments": [ { "Id": "3d22dba3d947cb49", "Document": { "id": "3d22dba3d947cb49", "name": "my_app_name", "start_time": 1668659232.6506412, "trace_id": "1-6375b820-29abfda99cb913ae91e21581", "end_time": 1668659232.6513555, "in_progress": false, "http": { "request": { "url": "http://127.0.0.1:5000/", "method": "GET", "user_agent": "curl/7.79.1", "client_ip": "127.0.0.1" }, "response": { "status": 200, "content_length": 20 } }, "aws": { "xray": { "sdk_version": "2.11.0", "sdk": "X-Ray for Python" } }, "service": { "runtime": "CPython", "runtime_version": "3.7.10" } } } ] }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。