Google GCP跟踪:具有嵌套用户功能信息的跟踪

时间:2019-05-08 19:26:24

标签: google-app-engine google-cloud-platform stack-trace

当前,这是我在分析GCP跟踪时的图片。这是有用的,但我们需要对一些要改进的地方进行更深入的分析。

Standard Trace without nested user function info

我们想要做的是拥有这样的东西

  | vi/route/r   
     | function abc (75 ms)
       | redis-hget (1 ms)
       | datastore (69 ms)
          | function cde (30 ms)
              | ...

我们做了很多尝试,但没有找到有关GCP跟踪概念的更多文档,而只是详细的API调用。

这是我们的POC

条目级别

require('@google-cloud/trace-agent').start({
    projectId: 'my-project',
    keyFilename: './my-credentials.json',
    stackTraceLimit: 0,
});

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

const toSecondLevel = require('./controllers/home');
const initController = require('./controllers/init');

app.post('/poc/', bodyParser.json(), async (req, res) => {

    const trace = require('@google-cloud/trace-agent').get()

    const rootSpan = trace.getCurrentRootSpan();
    const { requestType } = req.body;

    rootSpan.addLabel('Root - First Level' , requestType);
    await toSecondLevel(rootSpan, req, res);

    rootSpan.endSpan(new Date());
    res.send('Hello World! ===> ' + requestType);
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

第二级

const toThirdLevel = require('../services/home')


const secondLevelFunction = async (rootSpan, req, res) => {

    await secondLevelFunctionPromise(rootSpan);
}


const secondLevelFunctionPromise = (rootSpan) => new Promise((resolve, reject) => {
    const span = rootSpan.createChildSpan({ name: 'secondLevelFunction' });
    setTimeout(async () => {
        console.log('secondLevelFunction', span.getTraceContext())
        span.addLabel('Should be nested to Root?', 'NOT nested');
        await toThirdLevel(rootSpan, new Date());

        span.endSpan(new Date())
        resolve();
    }, 2400);
})

module.exports = secondLevelFunction;

第三个级别

const thirdLevelFunction = (rootSpan, parametro1) => new Promise((resolve, reject) => {
    const span = rootSpan.createChildSpan({ name: 'thirdLevelFunction' });
    setTimeout(() => {
        span.addLabel('thirdLevelFunction', 'not nested as well')
        console.log(span.getTraceContext())

        span.endSpan(new Date());
        resolve();
    }, 1392);
})

module.exports = thirdLevelFunction;

问题/概念性问题:

1- POC中没有嵌套Span

2-所有跨度都有getTraceContext()。我可以用它来嵌套我的跨度吗?

3-是否有可能做我打算做的事情? (A-将我的Span和B-嵌套到第一张图片中显示的同一棵树中?

4-类RootSpan扩展了跨度。但是我对此没有任何不同的行为。

1 个答案:

答案 0 :(得分:0)

现在它正在工作。需要什么(typescript以更好地理解):

import * as traceAgent from '@google-cloud/trace-agent';
const trace: traceAgent.PluginTypes.Tracer = traceAgent.get();
const span: TraceSpan = this.trace.createChildSpan(options);
...
span.addLabel('property',yourContent);
spam.endSpan(getDate());

重要的是,注意到traceAgent.get()是一个工厂,即使几个线程要求新的跨度,该插件也设法使其在事件顺序上保持一致。

我强烈建议:

  1. 使用提供的接口,因为文档不完善。阅读 接口和实际代码可让您更好地理解
  2. 为您的跨度添加一个包装器,以便您可以创建一个更加紧密的间接连接 满足您的需求。现在,我可以使用标签打开跨度,使用 标签,添加一个对象,以便我可以基于创建标签 对象。

Now Working: Actual code and RPC calls

相关问题