-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-294461: [Product Metrics/Observability] Implement Metric Collection workflow #357
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 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b3d0b3d
CLOUDP-294461: [Product Metrics/Observability] Implement Metric Colle…
yelizhenden-mdb dc56c6a
prettier fix
yelizhenden-mdb f52313c
fix: config in a separate file
yelizhenden-mdb caab2ae
fix:unused imports
yelizhenden-mdb d6aed8f
fix:file path
yelizhenden-mdb 8f33f03
fix: explicitly flush to file
yelizhenden-mdb 5386c00
fix
yelizhenden-mdb 0924673
fix: owner_team
yelizhenden-mdb 7ed962f
fix: create outputs dir
yelizhenden-mdb 72dcbb2
prettier fix
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 |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ | |
|
|
||
| *.out | ||
| **/*ipa-collector-results-combined.log | ||
| **/*metric-collection-results.json | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const rootDir = path.resolve(dirname, '../../../../'); | ||
|
|
||
| const config = { | ||
| defaultOasFilePath: path.join(rootDir, 'openapi', 'v2.json'), | ||
| defaultCollectorResultsFilePath: path.join(dirname, 'ipa-collector-results-combined.log'), | ||
| defaultRulesetFilePath: path.join(dirname, '..', 'ipa-spectral.yaml'), | ||
| defaultMetricCollectionResultsFilePath: path.join(dirname, 'metric-collection-results.json'), | ||
| }; | ||
|
|
||
| export default config; | ||
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,51 @@ | ||
| import spectral from '@stoplight/spectral-core'; | ||
| import * as fs from 'node:fs'; | ||
| import { | ||
| loadOpenAPIFile, | ||
| extractTeamOwnership, | ||
| loadRuleset, | ||
| loadCollectorResults, | ||
| getSeverityPerRule, | ||
| merge, | ||
| } from './utils.js'; | ||
| import config from './config.js'; | ||
| const { Spectral } = spectral; | ||
|
|
||
| async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) { | ||
| try { | ||
| console.log(`Loading OpenAPI file: ${oasFilePath}`); | ||
| const oasContent = loadOpenAPIFile(oasFilePath); | ||
|
|
||
| console.log('Extracting team ownership data...'); | ||
| const ownershipData = extractTeamOwnership(oasContent); | ||
|
|
||
| console.log('Initializing Spectral...'); | ||
| const spectral = new Spectral(); | ||
| const ruleset = await loadRuleset(config.defaultRulesetFilePath, spectral); | ||
|
|
||
| console.log('Getting rule severities...'); | ||
| const ruleSeverityMap = getSeverityPerRule(ruleset); | ||
|
|
||
| console.log('Running Spectral analysis...'); | ||
| const spectralResults = await spectral.run(oasContent); | ||
|
|
||
| console.log('Loading collector results...'); | ||
| const collectorResults = loadCollectorResults(config.defaultCollectorResultsFilePath); | ||
|
|
||
| console.log('Merging results...'); | ||
| const mergedResults = merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap); | ||
|
|
||
| console.log('Metric collection job complete.'); | ||
| return mergedResults; | ||
| } catch (error) { | ||
| console.error('Error during metric collection:', error.message); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| const args = process.argv.slice(2); | ||
| const customOasFile = args[0]; | ||
|
|
||
| runMetricCollectionJob(customOasFile) | ||
| .then((results) => fs.writeFileSync(config.defaultMetricCollectionResultsFilePath, JSON.stringify(results))) | ||
| .catch((error) => console.error(error.message)); |
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,108 @@ | ||
| import fs from 'node:fs'; | ||
| import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; | ||
| import { EntryType } from './collector.js'; | ||
|
|
||
| export function loadOpenAPIFile(filePath) { | ||
| try { | ||
| const content = fs.readFileSync(filePath, 'utf8'); | ||
| return JSON.parse(content); | ||
| } catch (error) { | ||
| throw new Error(`Failed to load OpenAPI file: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| export function getSeverityPerRule(ruleset) { | ||
| const rules = ruleset.rules || {}; | ||
| const map = {}; | ||
| for (const [name, ruleObject] of Object.entries(rules)) { | ||
| map[name] = ruleObject.definition.severity; | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| export async function loadRuleset(rulesetPath, spectral) { | ||
| try { | ||
| const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch }); | ||
| await spectral.setRuleset(ruleset); | ||
| return ruleset; | ||
| } catch (error) { | ||
| throw new Error(`Failed to load ruleset: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| export function extractTeamOwnership(oasContent) { | ||
| const ownerTeams = {}; | ||
| const paths = oasContent.paths || {}; | ||
|
|
||
| for (const [path, pathItem] of Object.entries(paths)) { | ||
| for (const [, operation] of Object.entries(pathItem)) { | ||
| const ownerTeam = operation['x-xgen-owner-team']; | ||
|
|
||
| if (ownerTeam) { | ||
| if (!ownerTeams[path]) { | ||
| ownerTeams[path] = ownerTeam; | ||
| } else if (ownerTeams[path] !== ownerTeam) { | ||
| console.warn(`Conflict on path ${path}: ${ownerTeams[path]} vs ${ownerTeam}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ownerTeams; | ||
| } | ||
|
|
||
| export function loadCollectorResults(collectorResultsFilePath) { | ||
| try { | ||
| const content = fs.readFileSync(collectorResultsFilePath, 'utf8'); | ||
| const contentParsed = JSON.parse(content); | ||
| return { | ||
| [EntryType.VIOLATION]: contentParsed[EntryType.VIOLATION], | ||
| [EntryType.ADOPTION]: contentParsed[EntryType.ADOPTION], | ||
| [EntryType.EXCEPTION]: contentParsed[EntryType.EXCEPTION], | ||
| }; | ||
| } catch (error) { | ||
| throw new Error(`Failed to load Collector Results file: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| function getIPAFromIPARule(ipaRule) { | ||
| const pattern = /IPA-\d{3}/; | ||
| const match = ipaRule.match(pattern); | ||
| if (match) { | ||
| return match[0]; | ||
| } | ||
| } | ||
|
|
||
| export function merge(spectralResults, ownershipData, collectorResults, ruleSeverityMap) { | ||
| const results = []; | ||
|
|
||
| function addEntry(entryType, adoptionStatus) { | ||
| for (const entry of collectorResults[entryType]) { | ||
| const existing = results.find( | ||
| (result) => result.component_id === entry.componentId && result.ipa_rule === entry.ruleName | ||
| ); | ||
|
|
||
| if (existing) { | ||
| console.warn('Duplicate entries found', existing); | ||
| continue; | ||
| } | ||
|
|
||
| results.push({ | ||
| component_id: entry.componentId, | ||
| ipa_rule: entry.ruleName, | ||
| ipa: getIPAFromIPARule(entry.ruleName), | ||
| severity_level: ruleSeverityMap[entry.ruleName], | ||
| adoption_status: adoptionStatus, | ||
| exception_reason: entryType === EntryType.EXCEPTION ? entry.exceptionReason : null, | ||
| owner_team: entry.ownerTeam || null, | ||
| timestamp: new Date().toISOString(), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| addEntry(EntryType.VIOLATION, 'violated'); | ||
| addEntry(EntryType.ADOPTION, 'adopted'); | ||
| addEntry(EntryType.EXCEPTION, 'exempted'); | ||
|
|
||
| return results; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example how we can set different configurations? For the GH action later on we'd like to point to the generated FOAS for example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script currently supports providing a custom OAS file only, which must be in JSON format.
For example, you can run the script using:
node metricCollection.js "<path_to_oas_file>"to specify a custom OAS file.