Skip to content
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
run: corepack enable
- name: Install modules
run: yarn
- name: Check version.ts is in sync with package.json
run: yarn check:version
- name: Lint code base
run: yarn lint
test:
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
"scripts": {
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable",
"build": "yarn build:cjs && yarn build:esm",
"build": "node tools/generate-version.js && yarn build:cjs && yarn build:esm",
"build:cjs": "node tools/cleanup cjs && tsc -p config/tsconfig.cjs.json",
"build:esm": "node tools/cleanup esm && tsc -p config/tsconfig.esm.json && echo '{\"type\":\"module\"}' >./dist/esm/package.json",
"clean": "node tools/cleanup",
"package": "yarn build && yarn pack",
"format:fix": "prettier --write .",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "yarn lint --fix",
"build:test": "node tools/cleanup dist-test && tsc -p config/tsconfig.test.json && echo '{\"type\":\"module\"}' >./dist-test/package.json",
"check:version": "node tools/generate-version.js && node tools/check-version.js",
"build:test": "node tools/generate-version.js && node tools/cleanup dist-test && tsc -p config/tsconfig.test.json && echo '{\"type\":\"module\"}' >./dist-test/package.json",
"test": "yarn build:test && node --test dist-test/test/*.test.js",
"test:cov": "yarn build:test && node --test --experimental-test-coverage dist-test/test/*.test.js",
"addscope": "node tools/packagejson name @pyroscope"
"test:cov": "yarn build:test && node --test --experimental-test-coverage dist-test/test/*.test.js"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 6 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
".": {
"release-type": "node",
"package-name": "@pyroscope/nodejs",
"extra-files": [
{
"type": "generic",
"path": "src/version.ts"
}
],
"bump-minor-pre-major": true,
"changelog-sections": [
{"type": "feat", "section": "Features"},
Expand Down
21 changes: 16 additions & 5 deletions src/profilers/pyroscope-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { SourceMapper } from '../sourcemapper.js';
import { ContinuousProfiler } from './continuous-profiler.js';
import { HeapProfiler, HeapProfilerStartArgs } from './heap-profiler.js';
import { WallProfiler, WallProfilerStartArgs } from './wall-profiler.js';
import process from 'node:process';
import { VERSION } from '../version.js';

const MICROS_PER_SECOND = 1e6;
const MS_PER_SECOND = 1e3;
Expand Down Expand Up @@ -43,11 +45,20 @@ export class PyroscopeProfiler {

private buildApplicationName(config: PyroscopeConfig): string {
const appName: string = config.appName ?? DEFAULT_APP_NAME;
const tagsStringified: string = Object.entries(config.tags ?? {})
.map(
([tagKey, tagValue]: [string, number | string]) =>
`${tagKey}=${tagValue}`
)
const tags = config.tags ?? {};
const stringify = ([tagKey, tagValue]: [string, number | string]) => {
return `${tagKey}=${tagValue}`;
};

const tagsStringified = Object.entries({
'otel.scope.name': 'com.grafana.pyroscope/nodejs',
'otel.scope.version': VERSION,
'process.runtime.name': 'nodejs',
'process.runtime.version': process.versions.node,
})
.filter((e: [string, string]) => !(e[0] in tags))
.map(stringify)
.concat(Object.entries(tags).map(stringify))
.join(',');

return `${appName}{${tagsStringified}}`;
Expand Down
2 changes: 2 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Generated by tools/generate-version.js — do not edit.
export const VERSION = '0.4.13'; // x-release-please-version
32 changes: 28 additions & 4 deletions test/profiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import { describe, it, type TestContext } from 'node:test';
import { strict as assert } from 'node:assert';
import process from 'node:process';

import Pyroscope from '../src/index.js';
import { VERSION } from '../src/version.js';
import express from 'express';
import busboy from 'busboy';
import { Profile } from 'pprof-format';
import zlib from 'zlib';

function assertAppNameIncludes(name: unknown, ...parts: string[]): void {
const value = String(name);
for (const part of parts) {
assert.ok(
value.includes(part),
`expected app name to include ${JSON.stringify(part)}, got ${JSON.stringify(value)}`
);
}
}

const defaultSemconvTags = [
'otel.scope.name=com.grafana.pyroscope/nodejs',
`otel.scope.version=${VERSION}`,
'process.runtime.name=nodejs',
`process.runtime.version=${process.versions.node}`,
];

// createBackend creates an Express server with an /ingest endpoint and returns a promise
// that resolves to the HTTP port the server is listening on
const createBackend = (
Expand Down Expand Up @@ -87,7 +106,7 @@ describe('common behaviour of profilers', () => {
const req = await firstRequest;
await Pyroscope.stopWallProfiling();
assert.strictEqual(req.query.spyName, 'nodespy');
assert.strictEqual(req.query.name, 'nodejs{}');
assertAppNameIncludes(req.query.name, 'nodejs{', ...defaultSemconvTags);
});

it('should call a server on startHeapProfiling and clear gracefully', async (t) => {
Expand Down Expand Up @@ -138,7 +157,12 @@ describe('common behaviour of profilers', () => {
const req = await firstRequest;
await Pyroscope.stopHeapProfiling();
assert.strictEqual(req.query['spyName'], 'nodespy');
assert.strictEqual(req.query['name'], 'nodejs{env=test env}');
assertAppNameIncludes(
req.query['name'],
'nodejs{',
...defaultSemconvTags,
'env=test env'
);
});

it('should allow to call start profiling twice', async (t) => {
Expand Down Expand Up @@ -245,7 +269,7 @@ describe('common behaviour of profilers', () => {
await Pyroscope.stopWallProfiling();

assert.strictEqual(req.query['spyName'], 'nodespy');
assert.strictEqual(req.query['name'], 'nodejs{}');
assertAppNameIncludes(req.query['name'], 'nodejs{', ...defaultSemconvTags);

// ensure we contain everything expected
const emptyLabels = JSON.stringify({});
Expand Down Expand Up @@ -305,7 +329,7 @@ describe('common behaviour of profilers', () => {
await Pyroscope.stopWallProfiling();

assert.strictEqual(req.query['spyName'], 'nodespy');
assert.strictEqual(req.query['name'], 'nodejs{}');
assertAppNameIncludes(req.query['name'], 'nodejs{', ...defaultSemconvTags);
// expect sample, wall and cpu types
assert.deepStrictEqual(sampleType, [
'samples=count',
Expand Down
21 changes: 21 additions & 0 deletions tools/check-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable */
const { execSync } = require('child_process');
/* eslint-enable */

try {
execSync('git ls-files --error-unmatch src/version.ts', { stdio: 'ignore' });
} catch {
console.error(
'src/version.ts is not tracked by git. Run node tools/generate-version.js and commit the file.'
);
process.exit(1);
}

try {
execSync('git diff --exit-code -- src/version.ts', { stdio: 'inherit' });
} catch {
console.error(
'src/version.ts is out of sync with package.json. Run: node tools/generate-version.js'
);
process.exit(1);
}
12 changes: 12 additions & 0 deletions tools/generate-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable */
const fs = require('fs');
const path = require('path');
const { version } = require('../package.json');

const outPath = path.join(__dirname, '../src/version.ts');
const contents = `// Generated by tools/generate-version.js — do not edit.
export const VERSION = '${version}'; // x-release-please-version
`;

fs.writeFileSync(outPath, contents);
console.log(`Generated ${outPath} with VERSION=${version}`);
25 changes: 0 additions & 25 deletions tools/packagejson.js

This file was deleted.

Loading