-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-290417: [Product Metrics/Observability] Implement Collector class #352
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6db3071
CLOUDP-290417: [Product Metrics/Observability] Implement Collector class
yelizhenden-mdb e6c1b06
Fix collector class singleton pattern
yelizhenden-mdb daaaa87
Prettier fix
yelizhenden-mdb fa6362c
Fix: Include exception reason
yelizhenden-mdb 0fca929
little fix
yelizhenden-mdb 4b48f99
fix: collect violations and adoptions in helper functions
yelizhenden-mdb 695af05
fix: add jsdoc
yelizhenden-mdb 3ab740a
fix: all collect helpers in collectionUtils file
yelizhenden-mdb d83044c
little fix: schemaPath
yelizhenden-mdb f1a52c0
prettier fix
yelizhenden-mdb 8c4b9a6
fix: import exception_extension
yelizhenden-mdb c00e9d7
fix: update .gitignore
yelizhenden-mdb 56e520f
fix: update .gitignore
yelizhenden-mdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ | |
*.vscode | ||
|
||
*.out | ||
**/*ipa-collector-results-combined.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { beforeEach, describe, expect, it } from '@jest/globals'; | ||
import collector, { EntryType } from '../../metrics/collector'; | ||
import * as fs from 'node:fs'; | ||
|
||
jest.mock('node:fs'); | ||
|
||
describe('Collector Class', () => { | ||
const expectedOutput = { | ||
violations: [ | ||
{ componentId: 'example.component', ruleName: 'rule-1' }, | ||
{ componentId: 'example.component', ruleName: 'rule-2' }, | ||
], | ||
adoptions: [{ componentId: 'example.component', ruleName: 'rule-3' }], | ||
exceptions: [{ componentId: 'example.component', ruleName: 'rule-4', exceptionReason: 'exception-reason' }], | ||
}; | ||
|
||
beforeEach(() => { | ||
collector.entries = { | ||
[EntryType.VIOLATION]: [], | ||
[EntryType.ADOPTION]: [], | ||
[EntryType.EXCEPTION]: [], | ||
}; | ||
|
||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should collect violations, adoptions, and exceptions correctly', () => { | ||
collector.add(EntryType.VIOLATION, ['example', 'component'], 'rule-1'); | ||
collector.add(EntryType.VIOLATION, ['example', 'component'], 'rule-2'); | ||
collector.add(EntryType.ADOPTION, ['example', 'component'], 'rule-3'); | ||
collector.add(EntryType.EXCEPTION, ['example', 'component'], 'rule-4', 'exception-reason'); | ||
|
||
expect(collector.entries).toEqual(expectedOutput); | ||
|
||
collector.flushToFile(); | ||
const writtenData = JSON.stringify(expectedOutput, null, 2); | ||
expect(fs.writeFileSync).toHaveBeenCalledWith('ipa-collector-results-combined.log', writtenData); | ||
}); | ||
|
||
it('should not add invalid entries', () => { | ||
collector.add(null, 'rule-1', EntryType.VIOLATION); | ||
collector.add(['example', 'component'], null, EntryType.ADOPTION); | ||
collector.add(['example', 'component'], 'rule-4', null); | ||
|
||
expect(collector.entries).toEqual({ | ||
violations: [], | ||
adoptions: [], | ||
exceptions: [], | ||
}); | ||
|
||
expect(fs.writeFileSync).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as fs from 'node:fs'; | ||
|
||
export const EntryType = Object.freeze({ | ||
EXCEPTION: 'exceptions', | ||
VIOLATION: 'violations', | ||
ADOPTION: 'adoptions', | ||
}); | ||
|
||
class Collector { | ||
static instance = null; | ||
|
||
static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new Collector(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
constructor() { | ||
if (Collector.instance) { | ||
throw new Error('Use Collector.getInstance()'); | ||
} | ||
|
||
this.entries = { | ||
[EntryType.VIOLATION]: [], | ||
[EntryType.ADOPTION]: [], | ||
[EntryType.EXCEPTION]: [], | ||
}; | ||
|
||
this.fileName = 'ipa-collector-results-combined.log'; | ||
|
||
process.on('exit', () => this.flushToFile()); | ||
process.on('SIGINT', () => { | ||
this.flushToFile(); | ||
process.exit(); | ||
}); | ||
} | ||
|
||
add(type, componentId, ruleName, exceptionReason = null) { | ||
if (componentId && ruleName && type) { | ||
if (!Object.values(EntryType).includes(type)) { | ||
throw new Error(`Invalid entry type: ${type}`); | ||
} | ||
|
||
componentId = componentId.join('.'); | ||
const entry = { componentId, ruleName }; | ||
|
||
if (type === EntryType.EXCEPTION && exceptionReason) { | ||
entry.exceptionReason = exceptionReason; | ||
} | ||
|
||
this.entries[type].push(entry); | ||
} | ||
} | ||
|
||
flushToFile() { | ||
try { | ||
const data = JSON.stringify(this.entries, null, 2); | ||
fs.writeFileSync(this.fileName, data); | ||
} catch (error) { | ||
console.error('Error writing exceptions to file:', error); | ||
} | ||
} | ||
} | ||
|
||
const collector = Collector.getInstance(); | ||
export default collector; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.