Skip to content

Commit

Permalink
feat: added nodejs cpu profiler support (#19)
Browse files Browse the repository at this point in the history
* feat: added nodejs cpu profiler support

* fix: fixed error in package.json

* fix: fixing test

* fix: update jest config

* chore: update naming for tagWrapper for consistency

* test: added tests for cpu profiler

* chore: updated jest config

* fix: hotpatch period value

* test: added amazon linux to test suite

* chore: added idle notification on wall profiling

* Revert "test: added amazon linux to test suite"

This reverts commit c9c017f.

* refactor: make tagwrapper interface similar to python and go

* test: more tests on cpu & wall profiling

* docs: updated readme file

* chore: constants replaced with named values

* fix: added fix for nanoseconds

* feat: added emitter to be able to test profiles

* test: added cpu label tests

* add node 19 for testing

* fix freq

* flush last sample during cpu stop

* fix wall sample type config

* stop cpu timer sync

* upgrade to dd-pprof 2.0.0

* version bump

* "should allow to start cpu and wall profiling at the same time"

* fix tests

* make stopCpuProfiling async

* fix cpu flushing

* flush heap on stop

* add some heap configuration options

* maybe fix tests

* reformat heap.ts

* unified logic for profiling scheduling and cancelation

* fix sampletype conig again

* strip cwd from filenames

* feat: wip

* chore: remove unused module name mapper config

* updates min version

* post merge fixes

* auth fixes

* updates version

* test fixes

---------

Co-authored-by: Tolya Korniltsev <korniltsev.anatoly@gmail.com>
Co-authored-by: Roberto Pintos López <roberto.pintos.lopez@gmail.com>
Co-authored-by: Dmitry Filimonov <dmitry@pyroscope.io>
  • Loading branch information
4 people authored Apr 2, 2024
1 parent 9162a2f commit d63816f
Show file tree
Hide file tree
Showing 30 changed files with 10,674 additions and 6,313 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

.idea/
# Logs
logs
*.log
Expand Down
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Pyroscope supports two main operation modes:

Push mode means the package itself uploads profile data to a pyroscope server, when pull mode means you provide pyroscope server with an endponts to scrape profile data

NodeJS Pyroscope module supports collecting wall-time and heap. More details you may find [here](https://cloud.google.com/profiler/docs/concepts-profiling)
NodeJS Pyroscope module supports collecting cpu, wall-time and heap. More details you may find [here](https://cloud.google.com/profiler/docs/concepts-profiling)

## Push mode

Expand All @@ -46,7 +46,22 @@ Pyroscope.init({serverAddress: 'http://pyroscope:4040', appName: 'nodejs'});
Pyroscope.start();
```

Both params `appName` and `serverAddress` are mandatory. Once you `init` you may `startCpuProfiling()` and/or `startHeapProfiling()`. `start()` starts both memory and CPU profiling
Both params `appName` and `serverAddress` are mandatory. Once you `init` you may `startCpuProfiling()`, `startWallProfiling()` and/or `startHeapProfiling()`. `start()` starts both memory and CPU profiling

### Dynamic tags
You may assign certain labels to certain parts of your code by using wrapper function `tagWrapper(tags: Record<string, string | number | undefined>, fn: Function)`. Please note that this only
available for cpu profiling.

```typescript
...

app.get('/scooter', function scooterSearchHandler(req, res) {
Pyroscope.tagWrapper({'vehicle':'scooter'}, () =>
genericSearchHandler(0.1)(req, res)
);
});

```

## Pull Mode

Expand Down Expand Up @@ -111,7 +126,7 @@ then you also need to configure your pyroscope server by providing config file
log-level: debug
scrape-configs:
- job-name: testing # any name
enabled-profiles: [cpu, mem] # cpu and mem for wall and heap
enabled-profiles: [cpu, mem] # cpu and mem for cpu and heap
static-configs:
- application: rideshare
spy-name: nodespy # make pyroscope know it's node profiles
Expand All @@ -137,10 +152,10 @@ init(c : PyroscopeConfig)
Configuration options
```typescript
interface PyroscopeConfig {
serverAddress?: string; // Server address for push mode
serverAddress?: string; // Server address for push mode
sourceMapPath?: string[]; // Sourcemaps directories (optional)
appName?: string; // Application name
tags?: Record<string, any>; // Tags
appName?: string; // Application name
tags?: Record<string, any>; // Static tags
authToken?: string // Auth token for cloud version
}
```
Expand All @@ -156,6 +171,17 @@ Pyroscope.stopCpuProfiling()
// Or do it manually
Pyroscope.collectCpu(seconds?:number);
```

### Wall Profiling
```javascript
// Start collecting for 10s and push to server
Pyroscope.startWallProfiling()
Pyroscope.stopWallProfiling()

// Or do it manually
Pyroscope.collectWall(seconds?:number);
```

### Heap Profiling
```javascript
// Start heap profiling and upload to server
Expand Down
26 changes: 9 additions & 17 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
module.exports = {
"roots": [
"src",
"test"
],
moduleNameMapper: {
'^@pyroscope/nodejs$': '<rootDir>/src',
"./express.js": "<rootDir>/src/express.ts",
"./index.js": "<rootDir>/src/index.ts"
},
preset: "ts-jest",
testEnvironment: "node",
roots: ['src', 'test'],
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: [
"src/**/*.{ts,tsx}",
"test/**/*.{ts,js}",
"!**/node_modules/**",
"!**/vendor/**"
'src/**/*.{ts,tsx}',
'test/**/*.{ts,js}',
'!**/node_modules/**',
'!**/vendor/**',
],
transform: {
"^.+\\.tsx?$": "ts-jest",
'^.+\\.tsx?$': 'ts-jest',
},
globals: {
'ts-jest': {
tsconfig: './config/tsconfig.jest.json',
},
},
"extensionsToTreatAsEsm": [".ts"]
extensionsToTreatAsEsm: ['.ts'],
}
Loading

0 comments on commit d63816f

Please sign in to comment.