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

feat: Add usage data metrics for sandbox #642

Merged
merged 28 commits into from
Nov 16, 2023

Conversation

Amplifiyer
Copy link
Contributor

Description of changes: Add usage data metrics for sandbox

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link

changeset-bot bot commented Nov 14, 2023

🦋 Changeset detected

Latest commit: 442ffc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@aws-amplify/backend-output-storage Patch
@aws-amplify/integration-tests Patch
@aws-amplify/backend-deployer Patch
create-amplify Patch
@aws-amplify/platform-core Patch
@aws-amplify/sandbox Patch
@aws-amplify/backend-cli Patch
@aws-amplify/backend-auth Patch
@aws-amplify/backend-data Patch
@aws-amplify/backend-function Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Amplifiyer Amplifiyer marked this pull request as ready for review November 14, 2023 23:57
packages/backend-deployer/src/cdk_deployer.ts Outdated Show resolved Hide resolved
packages/backend-output-storage/API.md Outdated Show resolved Hide resolved
packages/backend-deployer/API.md Outdated Show resolved Hide resolved
packages/backend-deployer/src/cdk_deployer.ts Fixed Show resolved Hide resolved
packages/platform-core/API.md Outdated Show resolved Hide resolved
const userName = userInfo().username;
const cwdPath = process.cwd();

return uuidV5(cwdPath + packageJsonName + userName, namespace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this mix some "machine identifier" like mac address ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the network interfaces could be named differently and some have just all 0s in it. Would this be a better idea https://www.npmjs.com/package/node-machine-id?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The network interface was an example. That library looks promising if it's maintained. So feel free to find best solution for this. We need some machine id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look well supported or a good alternative that exists without requiring a local state.

Copy link
Contributor

@edwardfoyle edwardfoyle Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't include cwd in this seed. You can run npx amplify from anywhere in your project. It should be the path of the root package.json or something like that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I'm wondering if we should only seed with __dirname? The packageJsonName and userName could both change without actually being a different installation. __dirname should be stable for a given installation in all normal scenarios I can think of

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__dirname + some "machine identifier". (if we punt machine id we should create gh issue)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userName changing is unlikely though it's usually is included in the path (for unix at least), however for cases where users are installing an "example" app in a /tmp or a root directory, we will get same installation ids across different users.

There is no reliable way to get machine identifier as per my quick research. We will have to punt that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use __dirname for now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term - #665

packages/platform-core/src/telemetry/get_usage_data_url.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/serializable_error.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/usage_data_emitter.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/version_manager.ts Outdated Show resolved Hide resolved
packages/backend-deployer/src/cdk_deployer.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/usage_data_emitter.ts Outdated Show resolved Hide resolved
) {}

emitSuccess = async (
metrics?: Record<string, number>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a stronger type here than just string record?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and #642 (comment) .
above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning behind is that "metrics collector" should expose api to collect metrics (i.e. abstract mechanism of how data is pushed to the backend) but it should be up to caller to know what exactly is being reported.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but we also have logic in here where we are looking for specific keys to perform certain transformations. Seems like we could get the type system to help us.

Regardless, I'm not going to block on it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. But I'd do that by casting these Records to something typed internally in data collector as we do this only because we want to keep old schema.

packages/platform-core/src/telemetry/usage_data_emitter.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/get_usage_data_url.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/get_usage_data_url.ts Outdated Show resolved Hide resolved
packages/platform-core/src/telemetry/usage_data_emitter.ts Outdated Show resolved Hide resolved
},
"devDependencies": {
"@types/debounce-promise": "^3.1.6",
"@types/parse-gitignore": "^1.0.0"
"@types/parse-gitignore": "^1.0.0",
"@types/uuid": "^9.0.7"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use crypto.randomUUID to avoid pulling in this dep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually needed in platform-core (moved there) and since it's core, we should use the browser compatible libraries and I'm not sure if node's crypto is.

Copy link
Member

@sobolk sobolk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment on lines +167 to +168
const aggregatorStderrStream = new stream.Writable();
aggregatorStderrStream._write = function (chunk, encoding, done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that aggregating output is existing thing.
Mind logging GH issue to make this part "streaming and parsing errors" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will log it, this would be a little harder since we are actually throwing and processing elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/platform-core/API.md Outdated Show resolved Hide resolved
packages/platform-core/src/cwd_package_json_loader.ts Outdated Show resolved Hide resolved
sobolk
sobolk previously approved these changes Nov 16, 2023
Copy link
Contributor

@edwardfoyle edwardfoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments but I don't think they are blockers

const userName = userInfo().username;
const modulePath = __dirname;

return uuidV5(modulePath + packageJsonName + userName, namespace);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My vote would still be for just modulePath + userName. While packageJsonName should be pretty stable, it is subject to change (we have changed our package.json names on occasion)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that case it's acceptable for installation id to change. I think it's worse to have two customers sharing the installation id vs one customer to have many. Nevertheless I'm not strong on that so changed it to remove package.json name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for machine id we could use os.hostname() for now ? https://nodejs.org/api/os.html#oshostname

// eslint-disable-next-line spellcheck/spell-checker
const AMPLIFY_CLI_UUID_NAMESPACE = 'e7368840-2eb6-4042-99b4-9d6c2a9370e6'; // A random v4 UUID
/**
* Generates a consistent installation Uuid from the cwd + packageJsonName + userName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: update comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

*
* Throws if no package.json is present
*/
export class CwdPackageJsonReader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should just be another method on PackageJsonReader. ie .readFromCwd or such?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll accommodate that in a follow up PR.

@Amplifiyer Amplifiyer merged commit 70685f3 into aws-amplify:main Nov 16, 2023
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants