Skip to content

Commit 8bd07ea

Browse files
authored
Update nodejs examples (#3555)
1 parent 4b38e9f commit 8bd07ea

28 files changed

+549
-47
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Choose a language folder to select an example for your language of choice.
44

55
# How Pyroscope works
6+
67
Pyroscope identifies performance issues in your application by continuously profiling the code.
78

89
If you've never used a profiler before, then welcome!
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { check } from 'k6';
2+
import http from 'k6/http';
3+
4+
export const options = {
5+
duration: '5m',
6+
vus: 3,
7+
};
8+
9+
const URL = __ENV.TARGET_URL || 'http://localhost:5000';
10+
11+
export default function() {
12+
for (const endpoint of ['car', 'scooter', 'bike']) {
13+
const res = http.get(`${URL}/${endpoint}`);
14+
check(res, {
15+
'status is 200': (r) => r.status === 200,
16+
});
17+
}
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/build

examples/language-sdk-instrumentation/nodejs/express-pull/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "",
1111
"license": "Apache-2.0",
1212
"dependencies": {
13-
"@pyroscope/nodejs": "v0.3.11",
13+
"@pyroscope/nodejs": "0.4.0",
1414
"express": "^4.19.2",
1515
"morgan": "^1.10.0"
1616
},

examples/language-sdk-instrumentation/nodejs/express-pull/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
6767
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==
6868

69-
"@pyroscope/nodejs@v0.3.11":
70-
version "0.3.11"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
72-
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
69+
"@pyroscope/nodejs@0.4.0":
70+
version "0.4.0"
71+
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
72+
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/build

examples/language-sdk-instrumentation/nodejs/express-ts-inline/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ FROM node:latest
33
WORKDIR /app
44

55
COPY package.json yarn.lock .
6+
RUN yarn
7+
68
COPY tsconfig.json .
7-
RUN yarn
89
COPY *.ts .
910
RUN yarn build
10-
ENV DEBUG=pyroscope
11-
CMD ["yarn", "run", "run"]
1211

12+
ENV DEBUG=pyroscope
13+
CMD ["yarn", "start"]

examples/language-sdk-instrumentation/nodejs/express-ts-inline/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ const Pyroscope = require('@pyroscope/nodejs');
66
const SourceMapper = Pyroscope.default.SourceMapper;
77

88
const port = process.env['PORT'] || 5000;
9-
109
const region = process.env['REGION'] || 'default';
10+
const appName = process.env['APP_NAME'] || 'express-ts-inline';
11+
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';
1112

1213
const app = express();
1314
app.use(morgan('dev'));
1415

15-
app.get('/', (req, res) => {
16+
app.get('/', (_, res) => {
1617
res.send('Available routes are: /bike, /car, /scooter');
1718
});
1819

19-
const genericSearchHandler = (p: number) => (req: any, res: any) => {
20+
const genericSearchHandler = (p: number) => (_: any, res: any) => {
2021
const time = +new Date() + p * 1000;
2122
let i = 0;
2223
while (+new Date() < time) {
@@ -30,11 +31,13 @@ app.get('/bike', function bikeSearchHandler(req, res) {
3031
genericSearchHandler(0.2)(req, res)
3132
);
3233
});
34+
3335
app.get('/car', function carSearchHandler(req, res) {
3436
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
3537
genericSearchHandler(1)(req, res)
3638
);
3739
});
40+
3841
app.get('/scooter', function scooterSearchHandler(req, res) {
3942
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
4043
genericSearchHandler(0.5)(req, res)
@@ -44,14 +47,14 @@ app.get('/scooter', function scooterSearchHandler(req, res) {
4447
SourceMapper.create(['.'])
4548
.then((sourceMapper) => {
4649
Pyroscope.init({
47-
appName: 'nodejs',
48-
serverAddress: 'http://pyroscope:4040',
50+
appName: appName,
51+
serverAddress: pyroscopeUrl,
4952
sourceMapper: sourceMapper,
5053
tags: { region },
5154
});
5255
Pyroscope.start();
5356
})
54-
.catch((e) => {
57+
.catch((e: any) => {
5558
console.error(e);
5659
});
5760

examples/language-sdk-instrumentation/nodejs/express-ts-inline/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"scripts": {
66
"build": "tsc",
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"run": "node build/index.js"
8+
"start": "node build/index.js",
9+
"start:local": "yarn build && PYROSCOPE_URL=http://localhost:4040 yarn start",
10+
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
11+
"down": "docker compose down"
912
},
1013
"author": "",
1114
"license": "Apache-2.0",
1215
"dependencies": {
13-
"@pyroscope/nodejs": "v0.3.11",
16+
"@pyroscope/nodejs": "0.4.0",
1417
"axios": "^0.28.0",
1518
"express": "^4.19.2",
1619
"morgan": "^1.10.0",

examples/language-sdk-instrumentation/nodejs/express-ts-inline/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
6767
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==
6868

69-
"@pyroscope/nodejs@v0.3.11":
70-
version "0.3.11"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
72-
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
69+
"@pyroscope/nodejs@0.4.0":
70+
version "0.4.0"
71+
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
72+
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/build

examples/language-sdk-instrumentation/nodejs/express-ts/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ FROM node:latest
33
WORKDIR /app
44

55
COPY package.json yarn.lock .
6+
RUN yarn
7+
68
COPY tsconfig.json .
7-
RUN yarn
89
COPY *.ts .
910
RUN yarn build
10-
ENV DEBUG=pyroscope
11-
CMD ["yarn", "run", "run"]
1211

12+
ENV DEBUG=pyroscope
13+
CMD ["yarn", "start"]

examples/language-sdk-instrumentation/nodejs/express-ts/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import Pyroscope from '@pyroscope/nodejs';
66
const SourceMapper = Pyroscope.SourceMapper;
77

88
const port = process.env['PORT'] || 5000;
9-
109
const region = process.env['REGION'] || 'default';
10+
const appName = process.env['APP_NAME'] || 'express-ts';
11+
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';
1112

1213
const app = express();
1314
app.use(morgan('dev'));
1415

15-
app.get('/', (req, res) => {
16+
app.get('/', (_, res) => {
1617
res.send('Available routes are: /bike, /car, /scooter');
1718
});
1819

19-
const genericSearchHandler = (p: number) => (req: any, res: any) => {
20+
const genericSearchHandler = (p: number) => (_: any, res: any) => {
2021
const time = +new Date() + p * 1000;
2122
let i = 0;
2223
while (+new Date() < time) {
@@ -30,11 +31,13 @@ app.get('/bike', function bikeSearchHandler(req, res) {
3031
genericSearchHandler(0.2)(req, res)
3132
);
3233
});
34+
3335
app.get('/car', function carSearchHandler(req, res) {
3436
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
3537
genericSearchHandler(1)(req, res)
3638
);
3739
});
40+
3841
app.get('/scooter', function scooterSearchHandler(req, res) {
3942
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
4043
genericSearchHandler(0.5)(req, res)
@@ -44,8 +47,8 @@ app.get('/scooter', function scooterSearchHandler(req, res) {
4447
SourceMapper.create(['.'])
4548
.then((sourceMapper) => {
4649
Pyroscope.init({
47-
appName: 'nodejs',
48-
serverAddress: 'http://pyroscope:4040',
50+
appName: appName,
51+
serverAddress: pyroscopeUrl,
4952
sourceMapper: sourceMapper,
5053
tags: { region },
5154
});

examples/language-sdk-instrumentation/nodejs/express-ts/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"scripts": {
66
"build": "tsc",
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"run": "node build/index.js"
8+
"start": "node build/index.js",
9+
"start:local": "yarn build && PYROSCOPE_URL=http://localhost:4040 yarn start",
10+
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
11+
"down": "docker compose down"
912
},
1013
"author": "",
1114
"license": "Apache-2.0",
1215
"dependencies": {
13-
"@pyroscope/nodejs": "v0.3.11",
16+
"@pyroscope/nodejs": "0.4.0",
1417
"axios": "^0.28.0",
1518
"express": "^4.19.2",
1619
"morgan": "^1.10.0",

examples/language-sdk-instrumentation/nodejs/express-ts/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
6767
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==
6868

69-
"@pyroscope/nodejs@v0.3.11":
70-
version "0.3.11"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
72-
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
69+
"@pyroscope/nodejs@0.4.0":
70+
version "0.4.0"
71+
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
72+
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/build

examples/language-sdk-instrumentation/nodejs/express/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ COPY index.js .
88

99
ENV DEBUG=pyroscope
1010
ENV PYROSCOPE_WALL_COLLECT_CPU_TIME=true
11-
CMD ["node", "index.js"]
11+
CMD ["node", "index.js"]

examples/language-sdk-instrumentation/nodejs/express/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
const Pyroscope = require('@pyroscope/nodejs');
33

44
const port = process.env['PORT'] || 5000;
5-
65
const region = process.env['REGION'] || 'default';
6+
const appName = process.env['APP_NAME'] || 'express';
7+
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';
78

89
const express = require('express');
910
const morgan = require('morgan');
1011

1112
const app = express();
1213
app.use(morgan('dev'));
13-
app.get('/', (req, res) => {
14+
app.get('/', (_, res) => {
1415
res.send('Available routes are: /bike, /car, /scooter');
1516
});
1617

17-
const genericSearchHandler = (p) => (req, res) => {
18+
const genericSearchHandler = (p) => (_, res) => {
1819
const time = +new Date() + p * 1000;
1920
let i = 0;
2021
while (+new Date() < time) {
@@ -24,23 +25,24 @@ const genericSearchHandler = (p) => (req, res) => {
2425
};
2526

2627
Pyroscope.init({
27-
appName: 'nodejs',
28-
serverAddress: process.env['PYROSCOPE_SERVER'] || 'http://pyroscope:4040',
28+
appName: appName,
29+
serverAddress: pyroscopeUrl,
2930
tags: { region },
3031
});
31-
3232
Pyroscope.start();
3333

3434
app.get('/bike', function bikeSearchHandler(req, res) {
3535
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
3636
genericSearchHandler(0.5)(req, res)
3737
);
3838
});
39+
3940
app.get('/car', function carSearchHandler(req, res) {
4041
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
4142
genericSearchHandler(1)(req, res)
4243
);
4344
});
45+
4446
app.get('/scooter', function scooterSearchHandler(req, res) {
4547
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
4648
genericSearchHandler(0.25)(req, res)

examples/language-sdk-instrumentation/nodejs/express/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "node index.js",
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"start:local": "PYROSCOPE_URL=http://localhost:4040 yarn start",
10+
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
11+
"down": "docker compose down"
912
},
1013
"author": "",
1114
"license": "Apache-2.0",
1215
"dependencies": {
13-
"@pyroscope/nodejs": "v0.3.11",
16+
"@pyroscope/nodejs": "v0.4.0",
1417
"express": "^4.19.2",
1518
"morgan": "^1.10.0"
1619
},

examples/language-sdk-instrumentation/nodejs/express/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
6767
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==
6868

69-
"@pyroscope/nodejs@v0.3.11":
70-
version "0.3.11"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
72-
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
69+
"@pyroscope/nodejs@v0.4.0":
70+
version "0.4.0"
71+
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
72+
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/build
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM node:latest
2+
3+
WORKDIR /app
4+
5+
COPY package.json yarn.lock .
6+
RUN yarn install
7+
8+
COPY index.js .
9+
10+
CMD ["node", "index.js"]

0 commit comments

Comments
 (0)