"use strict";const axios = require("axios").default;const express = require("express");const redis = require('./utils/redis');const dbHelper = require("./utils/db");const app = express();app.get("/remoteInvoke", async (req, res) => {const result = await axios.get("http://cloud.tencent.com");return res.status(200).send(result.data);});app.get("/redis", async(req, res) => {let queryRes = await redis.getKey("foo")res.json({ code: 200, result: queryRes})})app.get("/mysql", async(req, res) => {let select = `select * from table_demo`;await dbHelper.query(select);res.json({ code: 200, result: "mysql op ended"})})app.use(express.json());app.listen(8080, () => {console.log("Listening on http://localhost:8080");});
npm install --save @opentelemetry/apinpm install --save @opentelemetry/auto-instrumentations-node
export OTEL_TRACES_EXPORTER="otlp"export OTEL_RESOURCE_ATTRIBUTES='token=<token>,hostName=<hostName>'export OTEL_EXPORTER_OTLP_PROTOCOL='grpc'export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="<endpoint>"export OTEL_SERVICE_NAME="<serviceName>"export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"node main.js
<serviceName>
:应用名,多个使用相同 serviceName 接入的应用进程,在 APM 中会表现为相同应用下的多个实例。应用名最长63个字符,只能包含小写字母、数字及分隔符“ - ”,且必须以小写字母开头,数字或小写字母结尾。<token>
:前置步骤中拿到业务系统 Token。<hostName>
:该实例的主机名,是应用实例的唯一标识,通常情况下可以设置为应用实例的 IP 地址。<endpoint>
:前置步骤中拿到的接入点。myService
,业务系统 Token 为 myToken
,主机名为 192.168.0.10
,接入点以 http://pl-demo.ap-guangzhou.apm.tencentcs.com:4317
为例,完整的启动命令为:export OTEL_TRACES_EXPORTER="otlp"export OTEL_RESOURCE_ATTRIBUTES='token=myToken
,hostName=192.168.0.10
'export OTEL_EXPORTER_OTLP_PROTOCOL='grpc'export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://pl-demo.ap-guangzhou.apm.tencentcs.com:4317"export OTEL_SERVICE_NAME="myService
"export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"node main.js
https://localhost:8080/
。在有正常流量的情况下,应用性能监控 > 应用监控 > 应用列表 中将展示接入的应用,应用性能监控 > 应用监控 > 应用详情 > 实例监控中将展示接入的应用实例。由于可观测数据的处理存在一定延时,如果接入后在控制台没有查询到应用或实例,请等待30秒左右。const opentelemetry = require("@opentelemetry/api")app.get("/attr", async(req, res) => {const tracer = opentelemetry.trace.getTracer('my-service-tracer');tracer.startActiveSpan('new internal span', span => {span.addEvent("Acquiring lock", {'log.severity':'error','log.message':'data node found',})span.addEvent("Got lock, doing work...", {'log.severity':'11111','log.message':'2222222','log.message1':'3333333',})span.addEvent("Unlocking")span.end();});res.json({ code: 200, msg: "success" });})
本页内容是否解决了您的问题?