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

fix: apply transformers on end events #17

Merged
merged 2 commits into from
Apr 10, 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
15 changes: 10 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"plugin:ecmascript-compat/recommended",
"plugin:unicorn/all",
"plugin:jsdoc/recommended",
"plugin:node/recommended",
"plugin:n/recommended",
"plugin:prettier/recommended"
],
"ignorePatterns": [
Expand Down Expand Up @@ -44,9 +44,14 @@
],
"import/no-duplicates": "off",
"import/no-unresolved": "error",
"node/no-missing-import": "off",
"node/no-unsupported-features/es-syntax": "off",
"n/no-missing-import": "off",
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-unused-vars": ["error", {
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}],
"@typescript-eslint/consistent-type-imports": ["error", { "prefer": "type-imports" }],
"unicorn/filename-case": "off",
"unicorn/import-style": "off",
Expand All @@ -71,8 +76,8 @@
],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"node/no-unpublished-import": "off",
"node/no-extraneous-import": "off",
"n/no-unpublished-import": "off",
"n/no-extraneous-import": "off",
"no-only-tests/no-only-tests": "error"
}
}
Expand Down
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,34 @@
"test": "jest"
},
"devDependencies": {
"@commitlint/cli": "^17.6.1",
"@commitlint/config-conventional": "^17.6.1",
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@types/bunyan": "^1.8.8",
"@types/stream-json": "^1.7.3",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"@types/stream-json": "^1.7.7",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"bunyan": "^1.8.15",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-ecmascript-compat": "^3.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsdoc": "^43.1.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-ecmascript-compat": "^3.2.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsdoc": "^48.2.3",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unicorn": "^46.0.0",
"husky": "^8.0.3",
"jest": "^29.0.0",
"lint-staged": "^13.2.2",
"eslint-plugin-n": "^17.2.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^52.0.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"lint-staged": "^15.2.2",
"microbundle": "^0.15.1",
"prettier": "^2.8.8",
"semantic-release": "^21.0.2",
"prettier": "^3.2.5",
"semantic-release": "^23.0.8",
"tempy": "^1.0.0",
"ts-jest": "^29.1.0",
"typedoc": "^0.25.1",
"typescript": "5.1.x"
"typedoc": "^0.25.13",
"typescript": "5.2.x"
},
"dependencies": {
"@flatten-js/interval-tree": "^1.1.2",
Expand Down
20 changes: 20 additions & 0 deletions src/decorator/Bunyamin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ describe('Bunyamin', () => {
);
});

test('should process begin-end events', () => {
let counter = 0;
const transformer = jest.fn((fields: any) => {
return { ...fields, index: counter++ };
});

bunyamin.useTransform(transformer);
expect(() => bunyamin.trace.complete('something', willThrow)).toThrow('error');
expect(transformer).toHaveBeenCalledTimes(2);
expect(logger.trace).toHaveBeenCalledWith({ ph: 'B', index: 0 }, 'something');
expect(logger.trace).toHaveBeenCalledWith(
{ ph: 'E', success: false, err: 'error', index: 1 },
'something',
);
});

test('should compose multiple transformation functions', () => {
const context = { cat: 'test' };
const transformer1 = jest.fn((fields: any) => ({ ...fields, x: 3 }));
Expand Down Expand Up @@ -333,3 +349,7 @@ describe('Bunyamin', () => {
error = jest.fn();
}
});

function willThrow() {
throw 'error';
}
6 changes: 3 additions & 3 deletions src/decorator/Bunyamin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}
}

/** @deprecated */

Check warning on line 49 in src/decorator/Bunyamin.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
get threadGroups(): ThreadGroupConfig[] {
return [];
}
Expand Down Expand Up @@ -158,7 +158,7 @@
): T {
const end = (customContext: EndContext) => {
const endContext = {
...customContext,
...this.#transformContext(customContext),
ph: 'E',
tid: fields.tid,
} as ResolvedFields;
Expand Down Expand Up @@ -194,8 +194,8 @@
userContext === undefined
? arguments_
: isError(arguments_[0]) && arguments_.length === 1
? [arguments_[0].message]
: arguments_.slice(1);
? [arguments_[0].message]
: arguments_.slice(1);

return {
fields: this.#resolveFields(fields, phase),
Expand Down
4 changes: 3 additions & 1 deletion src/decorator/types/BunyaminConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ export type BunyaminConfig<Logger extends BunyanLikeLogger> = {
/**
* Optional transformation of log record fields provided by the user.
*/
transformFields?: <T extends BunyaminLogRecordFields>(context: T | undefined) => T | undefined;
transformFields?: (
fields: BunyaminLogRecordFields | undefined,
) => BunyaminLogRecordFields | undefined;
};
Loading