-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (47 loc) · 1.64 KB
/
app.js
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
const { appsignal } = require("./appsignal");
const http = require("http");
const https = require("https");
const tracer = appsignal.tracer();
const hostname = "127.0.0.1";
const port = 3000;
tracer.withSpan(tracer.createSpan(), span => {
span.setName("GET /");
const server = http.createServer((req, res) => {
tracer.withSpan(span.child(), (child) => {
child
.setName("GET /users")
.setCategory("process_request.http")
https
.get("https://jsonplaceholder.typicode.com/users", (res) => {
let data = [];
const headerDate =
res.headers && res.headers.date
? res.headers.date
: "no response date";
console.log("Status Code:", res.statusCode);
console.log("Date in Response header:", headerDate);
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
console.log("Response ended: ");
const users = JSON.parse(Buffer.concat(data).toString());
for (user of users) {
console.log(`Got user with id: ${user.id}, name: ${user.name}`);
}
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});
child.close(); // don't forget to close the span if you're done with it!
});
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World");
span.close(); // don't forget to close the span if you're done with it!
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
});