Skip to content

Commit 24a46e4

Browse files
Use dynamic labels for nodejs examples and docs (#3309)
* Use dynamic labels for nodejs This updates the nodejs examples and docs to align with the v0.3.11 release. It also adds nodejs to the updater script Co-authored-by: Kim Nylander <[email protected]> * Consistent spelling/casing of Wall/CPU --------- Co-authored-by: Kim Nylander <[email protected]>
1 parent 5d5abe9 commit 24a46e4

File tree

16 files changed

+144
-42
lines changed

16 files changed

+144
-42
lines changed

docs/sources/configure-client/language-sdks/nodejs.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,44 @@ Pyroscope.init({
4747
Pyroscope.start()
4848
```
4949

50-
Note: If you'd prefer to use Pull mode you can do so using the [Grafana Agent]({{< relref "../grafana-agent" >}}).
50+
[comment]: <> (TODO This needs its own page like https://grafana.com/docs/pyroscope/latest/configure-client/grafana-agent/go_pull/)
51+
{{< admonition type="note" >}}
52+
If you'd prefer, you can use Pull mode using [Grafana Alloy](https://grafana.com/docs/alloy/latest/) (recommended) or [Grafana Agent]({{< relref "../grafana-agent" >}}) (legacy).
53+
{{< /admonition >}}
54+
55+
56+
### Configuration options
57+
58+
| Init parameter | ENVIRONMENT VARIABLE | Type | DESCRIPTION |
59+
|-------------------------------|-------------------------------------------|----------------|-----------------------------------------------------------------------------------|
60+
| `appName: | `PYROSCOPE_APPLICATION_NAME` | String | Sets the `service_name` label |
61+
| `serverAddress:` | `PYROSCOPE_SERVER_ADDRESS` | String | URL of the Pyroscope Server |
62+
| `basicAuthUser:` | n/a | String | Username for basic auth / Grafana Cloud stack user ID (Default `""`) |
63+
| `basicAuthPassword:` | n/a | String | Password for basic auth / Grafana Cloud API key (Default `""`) |
64+
| `flushIntervalMs:` | `PYROSCOPE_FLUSH_INTERVAL_MS` | Number | Interval when profiles are sent to the server (Default `60000`) |
65+
| `heapSamplingIntervalBytes` | `PYROSCOPE_HEAP_SAMPLING_INTERVAL_BYTES` | Number | Average number of bytes between samples. (Default `524288`) |
66+
| `heapStackDepth:` | `PYROSCOPE_HEAP_STACK_DEPTH` | Number | Maximum stack depth for heap samples (Default `64`) |
67+
| `wallSamplingDurationMs:` | `PYROSCOPE_WALL_SAMPLING_DURATION_MS` | Number | Duration of a single wall profile (Default `60000`) |
68+
| `wallSamplingIntervalMicros:` | `PYROSCOPE_WALL_SAMPLING_INTERVAL_MICROS` | Number | Interval of how often wall samples are collected (Default `10000` |
69+
| `wallCollectCpuTime:` | `PYROSCOPE_WALL_COLLECT_CPU_TIME` | Boolean | Enable CPU time collection for wall profiles (Default `false`) |
70+
| `tags:` | n/a | [LabelSet] | Static labels applying to all profiles collected (Default `{}`) |
71+
| `sourceMapper:` | n/a | [SourceMapper] | Provide source file mapping information (Default `undefined`) |
72+
73+
[LabelSet]:https://github.com/DataDog/pprof-nodejs/blob/v5.3.0/ts/src/v8-types.ts#L59-L61
74+
[SourceMapper]:https://github.com/DataDog/pprof-nodejs/blob/v5.3.0/ts/src/sourcemapper/sourcemapper.ts#L152
75+
5176

5277
### Add profiling labels to Node.js applications
5378

54-
It is possible to add tags (labels) to the profiling data. These tags can be used to filter the data in the UI. Dynamic tagging isn't supported yet.
79+
#### Static labels
80+
81+
You can add static labels to the profiling data.
82+
These labels can be used to filter the data in the UI and apply for all profiles collected.
83+
Common static labels include:
84+
85+
* `hostname`
86+
* `region`
87+
* `team`
5588

5689
```javascript
5790
Pyroscope.init({
@@ -65,6 +98,16 @@ Pyroscope.init({
6598
Pyroscope.start()
6699
```
67100

101+
#### Dynamic labels for Wall/CPU profiles
102+
103+
In Wall/CPU profiles, labels can also be attached dynamically and help to separate different code paths:
104+
105+
```javascript
106+
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
107+
slowCode()
108+
);
109+
```
110+
68111
## Send data to Pyroscope OSS or Grafana Cloud
69112

70113
```javascript

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ RUN yarn install
77
COPY index.js .
88

99
ENV DEBUG=pyroscope
10+
ENV PYROSCOPE_WALL_COLLECT_CPU_TIME=true
1011
CMD ["node", "index.js"]

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ Pyroscope.init({ tags: { region } });
3131
app.use(expressMiddleware());
3232

3333
app.get('/bike', function bikeSearchHandler(req, res) {
34-
return genericSearchHandler(0.5)(req, res);
34+
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
35+
genericSearchHandler(0.5)(req, res)
36+
);
3537
});
3638
app.get('/car', function carSearchHandler(req, res) {
37-
return genericSearchHandler(1)(req, res);
39+
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
40+
genericSearchHandler(1)(req, res)
41+
);
3842
});
3943
app.get('/scooter', function scooterSearchHandler(req, res) {
40-
return genericSearchHandler(0.25)(req, res);
44+
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
45+
genericSearchHandler(0.25)(req, res)
46+
);
4147
});
4248

4349
app.listen(port, () => {

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.9",
13+
"@pyroscope/nodejs": "v0.3.11",
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/[email protected].9":
70-
version "0.3.9"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.9.tgz#4ca4499a8715a22b5d8847239fc91d1df2a90e8a"
72-
integrity sha512-To0b003umbwd4RM4MYXFe1HnEraC+Zz9VDlHT2olqPL+9XATjNOx9HYK7ErC92gvGXqPxwhZwWC3bDqNwkSVhw==
69+
"@pyroscope/[email protected].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==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ const genericSearchHandler = (p: number) => (req: any, res: any) => {
2626
};
2727

2828
app.get('/bike', function bikeSearchHandler(req, res) {
29-
return genericSearchHandler(0.2)(req, res);
29+
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
30+
genericSearchHandler(0.2)(req, res)
31+
);
3032
});
3133
app.get('/car', function carSearchHandler(req, res) {
32-
return genericSearchHandler(1)(req, res);
34+
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
35+
genericSearchHandler(1)(req, res)
36+
);
3337
});
3438
app.get('/scooter', function scooterSearchHandler(req, res) {
35-
return genericSearchHandler(0.5)(req, res);
39+
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
40+
genericSearchHandler(0.5)(req, res)
41+
);
3642
});
3743

38-
SourceMapper.create(["."])
44+
SourceMapper.create(['.'])
3945
.then((sourceMapper) => {
4046
Pyroscope.init({
4147
appName: 'nodejs',
@@ -46,8 +52,8 @@ SourceMapper.create(["."])
4652
Pyroscope.start();
4753
})
4854
.catch((e) => {
49-
console.error(e)
50-
})
55+
console.error(e);
56+
});
5157

5258
app.listen(port, () => {
5359
console.log(

examples/language-sdk-instrumentation/nodejs/express-ts-inline/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": "0.3.10",
13+
"@pyroscope/nodejs": "v0.3.11",
1414
"axios": "^0.26.1",
1515
"express": "^4.17.3",
1616
"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@0.3.10":
70-
version "0.3.10"
71-
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.10.tgz#84bf5ad2258c614c307b46ed0dd98277e32bd582"
72-
integrity sha512-/GHbVp7i48HW08tnDiqp54wQquGrDokiiYQnUtWMcOtiDaSnmwgHlhuZIOS1/P8MatHqIhIChBGn1Bpi53xGlw==
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==
7373
dependencies:
7474
"@datadog/pprof" "^5.3.0"
7575
axios "^0.28.0"

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ const genericSearchHandler = (p: number) => (req: any, res: any) => {
2626
};
2727

2828
app.get('/bike', function bikeSearchHandler(req, res) {
29-
return genericSearchHandler(0.2)(req, res);
29+
Pyroscope.wrapWithLabels({ vehicle: 'bike' }, () =>
30+
genericSearchHandler(0.2)(req, res)
31+
);
3032
});
3133
app.get('/car', function carSearchHandler(req, res) {
32-
return genericSearchHandler(1)(req, res);
34+
Pyroscope.wrapWithLabels({ vehicle: 'car' }, () =>
35+
genericSearchHandler(1)(req, res)
36+
);
3337
});
3438
app.get('/scooter', function scooterSearchHandler(req, res) {
35-
return genericSearchHandler(0.5)(req, res);
39+
Pyroscope.wrapWithLabels({ vehicle: 'scooter' }, () =>
40+
genericSearchHandler(0.5)(req, res)
41+
);
3642
});
3743

38-
SourceMapper.create(["."])
44+
SourceMapper.create(['.'])
3945
.then((sourceMapper) => {
4046
Pyroscope.init({
4147
appName: 'nodejs',
@@ -46,8 +52,8 @@ SourceMapper.create(["."])
4652
Pyroscope.start();
4753
})
4854
.catch((e) => {
49-
console.error(e)
50-
})
55+
console.error(e);
56+
});
5157

5258
app.listen(port, () => {
5359
console.log(

examples/language-sdk-instrumentation/nodejs/express-ts/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": "0.3.9",
13+
"@pyroscope/nodejs": "v0.3.11",
1414
"axios": "^0.28.0",
1515
"express": "^4.19.2",
1616
"morgan": "^1.10.0",

0 commit comments

Comments
 (0)