Skip to content

Commit

Permalink
WIP: Use dynamic labels for nodejs
Browse files Browse the repository at this point in the history
This updates the nodejs examples and docs

TODO:
- [ ] Actually use released version of package
  • Loading branch information
simonswine committed May 24, 2024
1 parent 2063655 commit 9db192f
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 16 deletions.
40 changes: 39 additions & 1 deletion docs/sources/configure-client/language-sdks/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,39 @@ Pyroscope.init({
Pyroscope.start()
```

[comment]: <> (TODO This needs its own page like https://grafana.com/docs/pyroscope/latest/configure-client/grafana-agent/go_pull/)
Note: If you'd prefer to use Pull mode you can do so using the [Grafana Agent]({{< relref "../grafana-agent" >}}).

### Configuration options

| Init parameter | ENVIRONMENT VARIABLE | Type | DESCRIPTION |
|-------------------------------|-----------------------------------------|----------------|-----------------------------------------------------------------------------------|
| `appName: | PYROSCOPE_APPLICATION_NAME | String | Sets the `service_name` label |
| `serverAddress:` | PYROSCOPE_SERVER_ADDRESS | String | URL of the Pyroscope Server |
| `basicAuthUser:` | n/a | String | Username for basic auth / Grafana Cloud stack user ID (Default `""`) |
| `basicAuthPassword:` | n/a | String | Password for basic auth / Grafana Cloud API key (Default `""`) |
| `flushIntervalMs:` | PYROSCOPE_FLUSH_INTERVAL_MS | Number | Interval when profiles are sent to the server (Default `60000`) |
| `heapSamplingIntervalBytes` | PYROSCOPE_HEAP_SAMPLING_INTERVAL_BYTES | Number | Average number of bytes between samples. (Default `524288`) |
| `heapStackDepth:` | PYROSCOPE_HEAP_STACK_DEPTH | Number | Maximum stack depth for heap samples (Default `64`) |
| `wallSamplingDurationMs:` | PYROSCOPE_WALL_SAMPLING_DURATION_MS | Number | Duration of a single wall profile (Default `60000`) |
| `wallSamplingIntervalMicros:` | PYROSCOPE_WALL_SAMPLING_INTERVAL_MICROS | Number | Interval of how often wall samples are collected (Default `10000 |
| `wallCollectCpuTime:` | PYROSCOPE_WALL_COLLECT_CPU_TIME | Boolean | Enable CPU time collection for wall profiles (Default `false`) |
| `tags:` | n/a | [LabelSet] | Static labels applying to all profiles collected (Default `{}`) |
| `sourceMapper:` | n/a | [SourceMapper] | Provide source file mapping information (Default `undefined`) |

[LabelSet]:https://github.com/DataDog/pprof-nodejs/blob/v5.3.0/ts/src/v8-types.ts#L59-L61
[SourceMapper]:https://github.com/DataDog/pprof-nodejs/blob/v5.3.0/ts/src/sourcemapper/sourcemapper.ts#L152


### Add profiling labels to Node.js applications

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.
#### Static labels

It is possible to add static labels to the profiling data. These labels can be used to filter the data in the UI and apply for all profiles collected. Common static labels include:

* `hostname`
* `region`
* `team`

```javascript
Pyroscope.init({
Expand All @@ -65,6 +93,16 @@ Pyroscope.init({
Pyroscope.start()
```

#### Dynamic labels for wall/cpu profiles

In wall/cpu profiles labels can also be attached dynamically and help to sepearte different code paths:

```javascript
Pyroscope.wrapWithWallLabels({ vehicle: 'bike' }, () =>
slowCode()
);
```

## Send data to Pyroscope OSS or Grafana Cloud

```javascript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ RUN yarn install
COPY index.js .

ENV DEBUG=pyroscope
ENV PYROSCOPE_WALL_COLLECT_CPU_TIME=true
CMD ["node", "index.js"]
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ Pyroscope.init({ tags: { region } });
app.use(expressMiddleware());

app.get('/bike', function bikeSearchHandler(req, res) {
return genericSearchHandler(0.5)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'bike' }, () =>
genericSearchHandler(0.5)(req, res)
);
});
app.get('/car', function carSearchHandler(req, res) {
return genericSearchHandler(1)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'car' }, () =>
genericSearchHandler(1)(req, res)
);
});
app.get('/scooter', function scooterSearchHandler(req, res) {
return genericSearchHandler(0.25)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'scooter' }, () =>
genericSearchHandler(0.25)(req, res)
);
});

app.listen(port, () => {
Expand Down
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.9",
"@pyroscope/nodejs": "https://github.com/simonswine/pyroscope-nodejs.git#475331c",
"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,9 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@v0.3.9":
"@pyroscope/nodejs@https://github.com/simonswine/pyroscope-nodejs.git#20240523_dev", "@pyroscope/nodejs@https://github.com/simonswine/pyroscope-nodejs.git#475331c":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.9.tgz#4ca4499a8715a22b5d8847239fc91d1df2a90e8a"
integrity sha512-To0b003umbwd4RM4MYXFe1HnEraC+Zz9VDlHT2olqPL+9XATjNOx9HYK7ErC92gvGXqPxwhZwWC3bDqNwkSVhw==
resolved "https://github.com/simonswine/pyroscope-nodejs.git#475331c81d605a00190602c9dfe2092d34ce7b60"
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "0.3.9",
"@pyroscope/nodejs": "https://github.com/simonswine/pyroscope-nodejs.git#475331c",
"axios": "^0.28.0",
"express": "^4.19.2",
"morgan": "^1.10.0",
Expand Down
11 changes: 11 additions & 0 deletions examples/language-sdk-instrumentation/nodejs/express-ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@
regenerator-runtime "^0.13.11"
source-map "^0.7.3"

"@pyroscope/nodejs@https://github.com/simonswine/pyroscope-nodejs.git#475331c":
version "0.3.9"
resolved "https://github.com/simonswine/pyroscope-nodejs.git#475331c81d605a00190602c9dfe2092d34ce7b60"
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
debug "^4.3.3"
form-data "^4.0.0"
regenerator-runtime "^0.13.11"
source-map "^0.7.3"

"@types/body-parser@*":
version "1.19.5"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.5.tgz#04ce9a3b677dc8bd681a17da1ab9835dc9d3ede4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ RUN yarn install
COPY index.js .

ENV DEBUG=pyroscope
ENV PYROSCOPE_WALL_COLLECT_CPU_TIME=true
CMD ["node", "index.js"]
12 changes: 9 additions & 3 deletions examples/language-sdk-instrumentation/nodejs/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ Pyroscope.init({
Pyroscope.start();

app.get('/bike', function bikeSearchHandler(req, res) {
return genericSearchHandler(0.5)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'bike' }, () =>
genericSearchHandler(0.5)(req, res)
);
});
app.get('/car', function carSearchHandler(req, res) {
return genericSearchHandler(1)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'car' }, () =>
genericSearchHandler(1)(req, res)
);
});
app.get('/scooter', function scooterSearchHandler(req, res) {
return genericSearchHandler(0.25)(req, res);
Pyroscope.wrapWithWallLabels({ vehicle: 'scooter' }, () =>
genericSearchHandler(0.25)(req, res)
);
});

app.listen(port, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pyroscope/nodejs": "0.3.9",
"@pyroscope/nodejs": "https://github.com/simonswine/pyroscope-nodejs.git#475331c",
"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,9 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@pyroscope/nodejs@0.3.9":
"@pyroscope/nodejs@https://github.com/simonswine/pyroscope-nodejs.git#475331c":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@pyroscope/nodejs/-/nodejs-0.3.9.tgz#4ca4499a8715a22b5d8847239fc91d1df2a90e8a"
integrity sha512-To0b003umbwd4RM4MYXFe1HnEraC+Zz9VDlHT2olqPL+9XATjNOx9HYK7ErC92gvGXqPxwhZwWC3bDqNwkSVhw==
resolved "https://github.com/simonswine/pyroscope-nodejs.git#475331c81d605a00190602c9dfe2092d34ce7b60"
dependencies:
"@datadog/pprof" "^5.3.0"
axios "^0.28.0"
Expand Down

0 comments on commit 9db192f

Please sign in to comment.