-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): resource usage report (#5243)
## Description closes #4644 As a part of the matrix automation process, we need to figure out what are the methods and properties that are used in each test. I came out with the following approach: - collect the inflight methods from the `_addOnLift` - collect the preflight methods and properties by adding a proxy to the Resource class - save the collected methods list to a file in the output dir (currently, it may change in the future) ### Implemented by adding a new platform (@hasanaburayyan FYI) Please let me know what you think :) TODO: - [x] Add tests - [ ] ~Change platform documentation (to reflect the change in the newInstance method)~ - [ ] Prefix "internal" prelight properties and methods with a "_" ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
Showing
22 changed files
with
453 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
{ | ||
"overrides": [] | ||
} |
This file contains 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 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 @@ | ||
lib/ |
This file contains 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,3 @@ | ||
{ | ||
"overrides": [] | ||
} |
This file contains 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,43 @@ | ||
{ | ||
"name": "@winglang/compatibility-spy", | ||
"version": "0.0.0", | ||
"author": { | ||
"name": "Wing Cloud", | ||
"email": "[email protected]", | ||
"organization": true | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/winglang/wing.git", | ||
"directory": "libs/awscdk" | ||
}, | ||
"description": "Wing Platform for spying the usage of cloud resources' methods and properties", | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org", | ||
"tag": "latest" | ||
}, | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"constructs": "~10.2.69", | ||
"@winglang/sdk": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"bump-pack": "workspace:^", | ||
"typescript": "^4.9.5", | ||
"tsup": "^6.7.0", | ||
"vitest": "^0.32.4" | ||
}, | ||
"scripts": { | ||
"compile": "tsc", | ||
"package": "bump-pack -b", | ||
"test": "vitest run --passWithNoTests --update" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
This file contains 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,98 @@ | ||
import { writeFileSync } from "fs"; | ||
import { resolve } from "path"; | ||
import { IPlatform } from "@winglang/sdk/lib/platform"; | ||
import { Construct } from "constructs"; | ||
import { App } from "@winglang/sdk/lib/core"; | ||
|
||
const PARENT_PROPERTIES: Set<string> = new Set([ | ||
"node", | ||
"onLiftMap", | ||
...Object.getOwnPropertyNames(Construct), | ||
...Object.getOwnPropertyNames(Construct.prototype), | ||
]); | ||
|
||
export class Platform implements IPlatform { | ||
public readonly target = "*"; | ||
/** | ||
* A summary of methods and property usage for each resource time | ||
* @internal | ||
*/ | ||
public _usageContext: Map<string, Set<string>> = new Map(); | ||
|
||
newInstance(fqn: string, scope: Construct, id: string, ...args: any) { | ||
//@ts-expect-error - accessing protected method | ||
const type = App.of(scope).typeForFqn(fqn); | ||
|
||
if (!type) { | ||
return undefined; | ||
} | ||
|
||
return new Proxy(new type(scope, id, ...args), { | ||
get: (target, prop: string | Symbol) => { | ||
if ( | ||
typeof prop === "string" && | ||
!prop.startsWith("_") && | ||
!PARENT_PROPERTIES.has(prop) | ||
) { | ||
this._addToUsageContext(target, prop); | ||
} | ||
//@ts-ignore | ||
return target[prop]; | ||
}, | ||
set: (target, prop, newValue) => { | ||
if (typeof prop === "string" && !prop.startsWith("_")) { | ||
this._addToUsageContext(target, prop); | ||
} | ||
//@ts-ignore | ||
target[prop] = newValue; | ||
return true; | ||
}, | ||
}); | ||
} | ||
|
||
preSynth(app: Construct) { | ||
for (const c of app.node.findAll()) { | ||
if ((c as any).onLiftMap?.size) { | ||
(c as any).onLiftMap.forEach((ops: Set<string>) => { | ||
ops.forEach((op: string) => this._addToUsageContext(c, op)); | ||
}); | ||
} | ||
} | ||
this._writeAppUsage(app as App); | ||
} | ||
|
||
/** | ||
* Adds an op to usage context | ||
* @param op | ||
* @internal | ||
*/ | ||
public _addToUsageContext(parent: any, op: string): void { | ||
const className = parent.constructor.name; | ||
|
||
if (["handle", "$inflight_init"].includes(op)) { | ||
return; | ||
} | ||
const usageContext = this._usageContext.get(className); | ||
if (!usageContext) { | ||
this._usageContext.set(className, new Set([op])); | ||
} else { | ||
usageContext.add(op); | ||
} | ||
} | ||
|
||
/** | ||
* Write the usage context into a file in the out dir | ||
* @internal | ||
*/ | ||
private _writeAppUsage(app: App): void { | ||
const context: Record<string, string[]> = {}; | ||
for (const key of this._usageContext.keys()) { | ||
context[key] = Array.from(this._usageContext.get(key) ?? []); | ||
} | ||
|
||
writeFileSync( | ||
resolve(app.outdir, "usage_context.json"), | ||
JSON.stringify(context, null, 2) | ||
); | ||
} | ||
} |
This file contains 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,56 @@ | ||
import { test, describe, expect, vi } from "vitest"; | ||
import { join } from "path"; | ||
import { | ||
PlatformManager, | ||
_loadCustomPlatform, | ||
} from "@winglang/sdk/lib/platform"; | ||
import { Platform } from "../src"; | ||
import { BUCKET_FQN } from "@winglang/sdk/lib/cloud"; | ||
import { App as SimApp } from "@winglang/sdk/lib/target-sim/app"; | ||
import { Bucket } from "@winglang/sdk/lib/target-sim/bucket"; | ||
|
||
import { Platform as SimPlatform } from "@winglang/sdk/lib/target-sim/platform"; | ||
|
||
describe("compatibility spy", async () => { | ||
let spyPlatform = new Platform(); | ||
|
||
vi.spyOn(spyPlatform, "newInstance"); | ||
|
||
const manager = new PlatformManager({ | ||
platformPaths: ["sim", join(__dirname, "../lib")], | ||
}); | ||
|
||
//@ts-expect-error- accessing private method | ||
vi.spyOn(manager, "loadPlatformPath").mockImplementation( | ||
//@ts-expect-error- accessing private method | ||
(platformPath: string) => { | ||
//@ts-expect-error- accessing private property | ||
manager.platformInstances.push( | ||
platformPath === "sim" ? new SimPlatform() : spyPlatform | ||
); | ||
} | ||
); | ||
|
||
const app = manager.createApp({ | ||
entrypointDir: __dirname, | ||
}) as SimApp; | ||
|
||
test("app overrides and hooks set correctly", () => { | ||
expect(app._newInstanceOverrides.length).toBe(1); | ||
//@ts-expect-error - _synthHooks is protected | ||
expect(app._synthHooks?.preSynthesize.length).toBe(1); | ||
}); | ||
|
||
const bucket = app.newAbstract(BUCKET_FQN, app, "bucket") as Bucket; | ||
|
||
bucket.addObject("a", "b"); | ||
// @ts-expect-error- accessing private property | ||
bucket.public; | ||
|
||
test("each new instance is wrapped in a proxy", () => { | ||
expect(spyPlatform.newInstance).toBeCalledTimes(1); | ||
expect(spyPlatform._usageContext.get("Bucket")).toEqual( | ||
new Set(["addObject", "initialObjects", "public"]) | ||
); | ||
}); | ||
}); |
This file contains 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,41 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "commonjs", | ||
"rootDir": "./src", | ||
"outDir": "./lib", | ||
"declarationMap": false, | ||
"inlineSourceMap": true, | ||
"inlineSources": true, | ||
"alwaysStrict": true, | ||
"declaration": true, | ||
"experimentalDecorators": true, | ||
"incremental": true, | ||
"lib": [ | ||
"es2020", | ||
"dom" | ||
], | ||
"esModuleInterop": true, | ||
"noEmitOnError": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"strictNullChecks": true, | ||
"strictPropertyInitialization": true, | ||
"stripInternal": false, | ||
"composite": false, | ||
"tsBuildInfoFile": "lib/tsconfig.tsbuildinfo" | ||
}, | ||
"include": [ | ||
"./src/**/*" | ||
], | ||
"exclude": [ | ||
"./node_modules" | ||
] | ||
} |
This file contains 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,7 @@ | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
entry: ["src/index.ts"], | ||
dts: true, | ||
clean: true, | ||
}); |
This file contains 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,16 @@ | ||
{ | ||
"$schema": "https://turborepo.org/schema.json", | ||
"extends": ["//"], | ||
"pipeline": { | ||
"compile": { | ||
"outputs": ["dist/**", "lib/**"] | ||
}, | ||
"test": { | ||
"dependsOn": ["compile"] | ||
}, | ||
"package": { | ||
"dependsOn": ["compile"], | ||
"outputs": ["../../dist/winglang-compatibility-spy-*.tgz"] | ||
} | ||
} | ||
} |
This file contains 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.