Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nodejs examples #3555

Merged
merged 11 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Choose a language folder to select an example for your language of choice.

# How Pyroscope works

Pyroscope identifies performance issues in your application by continuously profiling the code.

If you've never used a profiler before, then welcome!
Expand Down
18 changes: 18 additions & 0 deletions examples/language-sdk-instrumentation/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { check } from 'k6';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this referenced anywhere or just a helper to verify things? I see we have one in the nodejs folder as well

Copy link
Contributor Author

@bryanhuhta bryanhuhta Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accidentally committed two of these scripts. I removed one in 316666d. I used this to quickly apply load to a locally running example.

The Python load generator is nice for the Docker Compose examples because it runs its load continuously, but it requires more configuration. This k6 script is nice for testing an example that is running locally without having to spin up the Python load generator. Instead, a quick

k6 run load.js

command will start sending load to a local instance.

I've wanted to move all our examples over to using k6 as a load generator but haven't found time to experiment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense and yeah, replacing them with k6 eventually would be nice.

import http from 'k6/http';

export const options = {
duration: '5m',
vus: 3,
};

const URL = __ENV.TARGET_URL || 'http://localhost:5000';

export default function() {
for (const endpoint of ['car', 'scooter', 'bike']) {
const res = http.get(`${URL}/${endpoint}`);
check(res, {
'status is 200': (r) => r.status === 200,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/build
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "v0.3.11",
"@pyroscope/nodejs": "0.4.0",
"express": "^4.19.2",
"morgan": "^1.10.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@v0.3.11":
version "0.3.11"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
"@pyroscope/nodejs@0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/build
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ FROM node:latest
WORKDIR /app

COPY package.json yarn.lock .
RUN yarn

COPY tsconfig.json .
RUN yarn
COPY *.ts .
RUN yarn build
ENV DEBUG=pyroscope
CMD ["yarn", "run", "run"]

ENV DEBUG=pyroscope
CMD ["yarn", "start"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ const Pyroscope = require('@pyroscope/nodejs');
const SourceMapper = Pyroscope.default.SourceMapper;

const port = process.env['PORT'] || 5000;

const region = process.env['REGION'] || 'default';
const appName = process.env['APP_NAME'] || 'express-ts-inline';
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';

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

app.get('/', (req, res) => {
app.get('/', (_, res) => {
res.send('Available routes are: /bike, /car, /scooter');
});

const genericSearchHandler = (p: number) => (req: any, res: any) => {
const genericSearchHandler = (p: number) => (_: any, res: any) => {
const time = +new Date() + p * 1000;
let i = 0;
while (+new Date() < time) {
Expand All @@ -30,11 +31,13 @@ app.get('/bike', function bikeSearchHandler(req, res) {
genericSearchHandler(0.2)(req, res)
);
});

app.get('/car', function carSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
genericSearchHandler(1)(req, res)
);
});

app.get('/scooter', function scooterSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
genericSearchHandler(0.5)(req, res)
Expand All @@ -44,14 +47,14 @@ app.get('/scooter', function scooterSearchHandler(req, res) {
SourceMapper.create(['.'])
.then((sourceMapper) => {
Pyroscope.init({
appName: 'nodejs',
serverAddress: 'http://pyroscope:4040',
appName: appName,
serverAddress: pyroscopeUrl,
sourceMapper: sourceMapper,
tags: { region },
});
Pyroscope.start();
})
.catch((e) => {
.catch((e: any) => {
console.error(e);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node build/index.js"
"start": "node build/index.js",
"start:local": "yarn build && PYROSCOPE_URL=http://localhost:4040 yarn start",
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
"down": "docker compose down"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "v0.3.11",
"@pyroscope/nodejs": "0.4.0",
"axios": "^0.28.0",
"express": "^4.19.2",
"morgan": "^1.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@v0.3.11":
version "0.3.11"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
"@pyroscope/nodejs@0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/build
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ FROM node:latest
WORKDIR /app

COPY package.json yarn.lock .
RUN yarn

COPY tsconfig.json .
RUN yarn
COPY *.ts .
RUN yarn build
ENV DEBUG=pyroscope
CMD ["yarn", "run", "run"]

ENV DEBUG=pyroscope
CMD ["yarn", "start"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import Pyroscope from '@pyroscope/nodejs';
const SourceMapper = Pyroscope.SourceMapper;

const port = process.env['PORT'] || 5000;

const region = process.env['REGION'] || 'default';
const appName = process.env['APP_NAME'] || 'express-ts';
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';

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

app.get('/', (req, res) => {
app.get('/', (_, res) => {
res.send('Available routes are: /bike, /car, /scooter');
});

const genericSearchHandler = (p: number) => (req: any, res: any) => {
const genericSearchHandler = (p: number) => (_: any, res: any) => {
const time = +new Date() + p * 1000;
let i = 0;
while (+new Date() < time) {
Expand All @@ -30,11 +31,13 @@ app.get('/bike', function bikeSearchHandler(req, res) {
genericSearchHandler(0.2)(req, res)
);
});

app.get('/car', function carSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
genericSearchHandler(1)(req, res)
);
});

app.get('/scooter', function scooterSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
genericSearchHandler(0.5)(req, res)
Expand All @@ -44,8 +47,8 @@ app.get('/scooter', function scooterSearchHandler(req, res) {
SourceMapper.create(['.'])
.then((sourceMapper) => {
Pyroscope.init({
appName: 'nodejs',
serverAddress: 'http://pyroscope:4040',
appName: appName,
serverAddress: pyroscopeUrl,
sourceMapper: sourceMapper,
tags: { region },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node build/index.js"
"start": "node build/index.js",
"start:local": "yarn build && PYROSCOPE_URL=http://localhost:4040 yarn start",
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
"down": "docker compose down"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "v0.3.11",
"@pyroscope/nodejs": "0.4.0",
"axios": "^0.28.0",
"express": "^4.19.2",
"morgan": "^1.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@v0.3.11":
version "0.3.11"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
"@pyroscope/nodejs@0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/build
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ COPY index.js .

ENV DEBUG=pyroscope
ENV PYROSCOPE_WALL_COLLECT_CPU_TIME=true
CMD ["node", "index.js"]
CMD ["node", "index.js"]
14 changes: 8 additions & 6 deletions examples/language-sdk-instrumentation/nodejs/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
const Pyroscope = require('@pyroscope/nodejs');

const port = process.env['PORT'] || 5000;

const region = process.env['REGION'] || 'default';
const appName = process.env['APP_NAME'] || 'express';
const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';

const express = require('express');
const morgan = require('morgan');

const app = express();
app.use(morgan('dev'));
app.get('/', (req, res) => {
app.get('/', (_, res) => {
res.send('Available routes are: /bike, /car, /scooter');
});

const genericSearchHandler = (p) => (req, res) => {
const genericSearchHandler = (p) => (_, res) => {
const time = +new Date() + p * 1000;
let i = 0;
while (+new Date() < time) {
Expand All @@ -24,23 +25,24 @@ const genericSearchHandler = (p) => (req, res) => {
};

Pyroscope.init({
appName: 'nodejs',
serverAddress: process.env['PYROSCOPE_SERVER'] || 'http://pyroscope:4040',
appName: appName,
serverAddress: pyroscopeUrl,
tags: { region },
});

Pyroscope.start();

app.get('/bike', function bikeSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
genericSearchHandler(0.5)(req, res)
);
});

app.get('/car', function carSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
genericSearchHandler(1)(req, res)
);
});

app.get('/scooter', function scooterSearchHandler(req, res) {
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
genericSearchHandler(0.25)(req, res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start:local": "PYROSCOPE_URL=http://localhost:4040 yarn start",
"up": "yarn down && docker compose up --build --force-recreate --no-deps",
"down": "docker compose down"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "v0.3.11",
"@pyroscope/nodejs": "v0.4.0",
"express": "^4.19.2",
"morgan": "^1.10.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@v0.3.11":
version "0.3.11"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.11.tgz#d3ffed8423b628701d06cdc6ef97fd5943d91939"
integrity sha512-xqxUDrzgdfTic4QU3FyvPvO3iAF63zEEI+gXgBBNA6MrJVOJxaEDJkeOGnH0AT7yG/vLJVmSeo4+VyIKrCmztw==
"@pyroscope/nodejs@v0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.4.0.tgz#913bfdeb764462fb46d8f6defabd682631c21542"
integrity sha512-To75AyiEr7eS74fLXZUDlSHAWuOfBh/euL3JvYK376WoubtoaafPLzSgK/7XtWxYk+7ogjYQYpnuFk9/w+cgXA==
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:latest

WORKDIR /app

COPY package.json yarn.lock .
RUN yarn install

COPY index.js .

CMD ["node", "index.js"]
Loading
Loading