当前位置:   article > 正文

aws xray ec2环境搭建和基础用法_aws x-ray

aws x-ray

参考资料

  • 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

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
  • 1
  • 2
  • 3

之后查看系统进程

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

输出日志如下

$ 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

此时xray daemon成功运行,注意:需要确保实例具有xray相关权限,如果修改实例凭证之后仍然没有权限,需要检查是否需要重启xray进程。

simple case

不同语言对于xray的支持是不同的,xray支持aws sdk,以及部分流行的web框架和三方库,这里以nodejs和python为例。安装和配置语言可以参考这篇blog

xray会跟踪数据,包括有关应用程序提供的传入 HTTP 请求的信息,以及应用程序使用 Amazon SDK 或 HTTP 客户机对下游服务进行的调用。

nodejs 发送 segments

初始化

npm init -y
npm install aws-xray-sdk
npm intall aws-sdk
npm install express
  • 1
  • 2
  • 3
  • 4

express quick start

// 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}`)
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

启动并访问

node app.js
curl 127.0.0.1:3000
  • 1
  • 2

查看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)
  • 1
  • 2
  • 3
  • 4

查看trace

在这里插入图片描述

查看定义的注释和元数据

在这里插入图片描述

python 发送 segments

初始化

pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install boto3
pip install requests
pip install aws-xray-sdk
pip install Flask
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
使用requests
# 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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

发送请求测试

python myreq.py
  • 1

查看自定义分段

在这里插入图片描述

查看s3调用

在这里插入图片描述

flask quick satrt
# 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>"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

启动和访问

flask --app myflask run
 * Serving Flask app 'hello'
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
  • 1
  • 2
  • 3

查看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)
  • 1
  • 2
  • 3
  • 4

查看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"
                }
            }
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号