【问题标题】:AWS XRay Tracing - Not Displaying Trace Map or SubSegmentsAWS XRay 跟踪 - 不显示跟踪图或子段
【发布时间】:2020-01-27 23:26:35
【问题描述】:

我正在尝试使用 AWSXRay 跟踪无服务器 express Lambda 函数。 我尝试了几种不同的方法,但似乎没有任何效果。

如果我省略了 aws-xray-sdk-express 中间件,我将在时间轴中看到我的所有片段,并看到我的 Lambda 函数在跟踪图中出现了两次。如果我包含 express 中间件,我将在 Trace Map 中看到中间件段(“Super Dooper Trace Function”),而在时间轴(不在 Trace Map 中)中只看到第一个子段(“MyFirstTrace”)。

我试图让所有Subsegments 出现在TimelinesNode Graphs

const AWSXray = require('aws-xray-sdk');
const xrayExpress = require('aws-xray-sdk-express');
const express = require('express');
const awsServerlessExpress = require('aws-serverless-express');


module.exports.handler = async (event, context) => {
    const app = express();
    app.use(xrayExpress.openSegment('Super Dooper Trace Function'));

    app.get('/test', async (req, res) => {
        const result = await doWork();
        res.send(result); 
    });

    app.use(xrayExpress.closeSegment());

    const server = awsServerlessExpress.createServer(app);
    return awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise;

}

const doWork = async () => {
    const res1 =  await traceMyFunction('MyFirstTrace', 3000)
    const res2 = await traceMyFunction('MySecondTrace', 3000);
    const res3 = await traceMyFunction('MyThirdTrace', 2000);
    return [res1, res2, res3];
}


const traceMyFunction = (name, delayMs) => {
    return trace(name, async () => {
        return delay(delayMs)
    });
}


function trace(name, promiseFunction) {
    return new Promise((resolve, reject) => {
        AWSXray.captureAsyncFunc(name, (subSegment) => {
            promiseFunction(subSegment)
                .then((result) => {
                    resolve(result);
                    subSegment.close();
                }).catch((e) => {
                    subSegment.close();
                    reject(e)
                });
        });     
    });
}

const delay = (ms) => {
    return new Promise((res, rej) => {
        setTimeout(() => res({ time: ms }), ms)
    });
};

这是结果XRay Trace

这是跟踪Raw Data

{
    "Duration": 8.278,
    "Id": "1-5e2f6cc3-a99e6c08f02ce0aec7ab7121",
    "Segments": [
        {
            "Document": {
                "id": "87ca46b60ded68e1",
                "name": "Super Dooper Trace Function",
                "start_time": 1580166338.92,
                "end_time": 1580166347.198,
                "http": {
                    "request": {
                        "url": "http://localhost/test",
                        "method": "GET",
                        "user_agent": "",
                        "client_ip": ""
                    },
                    "response": {
                        "status": 200
                    }
                },
                "aws": {
                    "xray": {
                        "sdk": "X-Ray for Node.js",
                        "sdk_version": "2.5.0",
                        "package": "aws-xray-sdk"
                    }
                },
                "service": {
                    "version": "unknown",
                    "runtime": "node",
                    "runtime_version": "v12.13.0",
                    "name": "unknown"
                },
                "trace_id": "1-5e2f6cc3-a99e6c08f02ce0aec7ab7121",
                "subsegments": [
                    {
                        "id": "98e9f32273700e6e",
                        "name": "MyFirstTrace",
                        "start_time": 1580166339.078,
                        "end_time": 1580166342.082
                    }
                ]
            },
            "Id": "87ca46b60ded68e1"
        }
    ]
}

【问题讨论】:

    标签: amazon-web-services express aws-lambda aws-xray


    【解决方案1】:

    Serverless Express 已知与当前的 X-Ray SDK 不兼容 - 由于 Lambda 会生成自己的 Segment,然后 Express 中间件也会尝试创建一个。我们计划在不久的将来解决这个问题。

    https://github.com/aws/aws-xray-sdk-node/issues/45

    完整的解释请看这个帖子:https://github.com/aws/aws-xray-sdk-node/issues/30

    【讨论】:

    • 感谢您的回复。但是问题是,如果我不使用 express 中间件,或者在 lambda 函数中根本不使用 express,即只在我的处理函数中调用doWork(),我确实会显示我的段的时间线,但是节点图,只显示两个节点,每个节点都是一个 Lambda 函数,我看不到我的任何子段(在节点图中)。
    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2017-06-28
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多