Skip to content
欢迎扫码关注公众号

TKE Node.js 服务接入应用性能监测 SkyWalking

接入步骤

  1. 获取接入点和 Token

    通过腾讯云的应用性能观测页面的接入应用按钮,可以查看 SkyWalking 的接入点和 Token。

  2. 使用 npm 安装 skywalking-backend-js

    bash
    npm i skywalking-backend-js
  3. 数据上报

    在入口文件引入 SDK, 通过 start 方法执行数据上报。

    下面的示例使用环境变量的方式获取具体的配置参数,在 TKE 中配置环境变量的方式可参考之前 SpringBoot 服务接入 APM 的博客

    javascript
    const {default: agent} = require('skywalking-backend-js');
    agent.start({
        // 服务名
        serviceName: `${process.env.SW_AGENT_NAME}|${process.env.SW_AGENT_NAMESPACE}|${process.env.SW_AGENT_CLUSTER}`,
        // 服务实例名
        serviceInstance: `${process.env.SW_AGENT_NAME}|${currentPodIp}`,
        // APM 上报路径 (来自第一步)
        collectorAddress: process.env.SW_AGENT_COLLECTOR_BACKEND_SERVICES,
        // Token (来自第一步)
        authorization: process.env.SW_AGENT_AUTHENTICATION,
    });

日志添加 traceId

为了和 Spring Boot 服务的日志统一,这里使用了 log4js 来自定义输出的日志内容格式。

  1. 安装 log4js

    shell
    npm i log4js
  2. 配置日志格式

    其中使用 skywalking-backend-js 包中的 ContextManager 获取当前的 traceId

    这里是配置的示例:

    javascript
    const { default: agent, ContextManager: swContextManager } = require('skywalking-backend-js');
    const log4js = require("log4js");
    
    log4js.configure({
        appenders: {
            sw: {
                type: 'stdout',
                layout: {
                    type: 'pattern',
                    pattern: '%d{yyyy-MM-dd hh:mm:ss,SSS} %p %z (%f{1}:%l)- TID:%x{tid} %m%n',
                    tokens: {
                        tid: function (logEvent) {
                            return swContextManager.current.traceId();
                        },
                    },
                }
            }
        },
        categories: { default: { appenders: ["sw"], level: "info", enableCallStack: true } },
    });
    • %f%l 是文件名和行号,需要在 categories 中启用 enableCallStack
    • %x{tid} 就是当前的 traceIdtid 需要在 tokens 中配置其获取的方法。

    更多的日志 layout 配置可以参考官方文档

  3. 输出日志

    javascript
    const logger = require("log4js").getLogger();
    logger.info("Hello,World!");

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.