xray 采样规则测试


本文follow @zhaojiew 的测试进行,原文连接如下:
https://github.com/zhojiew/aws-learning-notebook/blob/main/xray/xray%E9%80%9A%E8%BF%87%E8%AE%BE%E7%BD%AE%E9%87%87%E6%A0%B7%E8%A7%84%E5%88%99%E5%AF%B9%E8%AF%B7%E6%B1%82%E8%BF%9B%E8%A1%8C%E8%BF%87%E6%BB%A4.md

初始化环境 安装npm

npm init -y
npm install aws-xray-sdk
npm install aws-sdk
npm install express

起一个express server测试

// app.js
var AWSXRay = require('aws-xray-sdk');
// xray将aws sdk包装
var AWS = AWSXRay.captureAWS(require('aws-sdk'));
// var AWS = require('aws-sdk');

// AWSXRay.config([AWSXRay.plugins.EC2Plugin, AWSXRay.plugins.ElasticBeanstalkPlugin]);
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('TestPathApp'));

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.get('/testpath', (req, res) => {
    res.send('test path!')
})

app.use(AWSXRay.express.closeSegment());

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

开始测试

为了让xray获取正确权限(没绑定instance profile所以直接命令启动)

/usr/bin/xray
for i in `seq 1 10000`; do curl localhost:3000/ ; curl localhost:3000/testpath ; sl
eep 0.2; done

配置采样规则

https://docs.aws.amazon.com/zh_cn/xray/latest/devguide/xray-console-sampling.html
https://docs.aws.amazon.com/zh_cn/xray/latest/devguide/xray-sdk-nodejs.html

设置采样限制

reservoir,每秒钟请求的采样数量
fixed rate,超出reservoir之后,对额外请求的百分比
举例,存储器容量为50,百分比为10%,如果总体请求为100,则每秒钟的采样数量为50+(100-50)*10%=55

比如 关掉默认的采样规则(都设置0,0)设置200 的优先级 采样规则匹配指定路径,这个设置0,0 然后设置400的采样规则代替默认,由于先匹配200 规则,就能够过滤掉不想要的路径的追踪


文章作者: Felix Li
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Felix Li !
  目录