-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.js
More file actions
126 lines (111 loc) · 3.65 KB
/
trace.js
File metadata and controls
126 lines (111 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const opentelemetry = require("@opentelemetry/api");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const {
BatchSpanProcessor,
ConsoleSpanExporter,
} = require("@opentelemetry/sdk-trace-base");
const {
OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-grpc");
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const {
ExpressInstrumentation,
} = require("@opentelemetry/instrumentation-express");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");
const { resourceFromAttributes } = require("@opentelemetry/resources");
const grpc = require("@grpc/grpc-js");
const os = require("os");
const hostname = os.hostname();
// 注意:intranet 端点只能在阿里云张家口地域 VPC 内访问
// 如果在本地开发,需要改为公网端点(去掉 -intranet)
// 公网端点(本地开发用)
const url = "k8s-log.cn-zhangjiakou.log.aliyuncs.com:10010";
// 内网端点(部署在阿里云张家口地域 VPC 内时用)
// const url = 'k8s-log-.cn-zhangjiakou-intranet.log.aliyuncs.com:10010';
const logStdout = url === "stdout";
const meta = new grpc.Metadata();
meta.add("x-sls-otel-project", "k8s-log-");
meta.add("x-sls-otel-instance-id", "devonew");
meta.add("x-sls-otel-ak-id", "LTAI5tJNP7HYTiDC");
meta.add("x-sls-otel-ak-secret", "tgSKLMS5aoRczguGeJ");
const resource = resourceFromAttributes({
"service.name": "Bohrium-web",
"service.version": "v0.1.0",
"host.name": hostname,
});
function createSpanProcessor() {
if (!logStdout) {
const exporter = new OTLPTraceExporter({
url: url,
credentials: grpc.credentials.createSsl(),
metadata: meta,
});
return new BatchSpanProcessor(exporter);
}
return new BatchSpanProcessor(new ConsoleSpanExporter());
}
const debugProcessor = new BatchSpanProcessor(new ConsoleSpanExporter());
const provider = new NodeTracerProvider({
resource,
spanProcessors: [createSpanProcessor(), debugProcessor],
});
provider.register();
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
new HttpInstrumentation({
ignoreIncomingPaths: [/\.(png|html)$/],
}),
new ExpressInstrumentation({
ignoreLayers: [/middleware.*/],
}),
],
});
const tracer = opentelemetry.trace.getTracer("front-end");
const express = require("express");
const app = express();
app.get("/hello", function (req, res) {
res.send("success");
});
app.get("/app", function (req, res) {
res.send("qqqqq");
});
app.get("/err", function (req, res) {
res.send("qqqqq");
});
const server = app.listen(8079, function () {
const addr = server.address();
if (addr) {
console.log(
"App now running in %s mode on port %d",
app.get("env"),
addr.port,
);
}
});
server.on("error", function (err) {
if (err.code === "EADDRINUSE") {
console.error(
"Port 8079 is already in use. Kill the existing process first: lsof -ti:8079 | xargs kill -9",
);
} else {
console.error("Server error:", err);
}
process.exit(1);
});
function gracefulShutdown() {
console.log("\nFlushing remaining spans before exit...");
provider
.forceFlush()
.then(() => provider.shutdown())
.then(() => {
console.log("All spans flushed. Bye!");
process.exit(0);
})
.catch((err) => {
console.error("Error flushing spans:", err);
process.exit(1);
});
}
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);