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

Add logic to send analytics data #5545

Merged
merged 32 commits into from
Aug 20, 2024

Conversation

ChristopherDedominici
Copy link
Contributor

@ChristopherDedominici ChristopherDedominici commented Jul 23, 2024

How it works in short:

so far there are 2 types of analytics that can be sent (like in the old hardhat).

  1. the response to the telemetry consent
  2. information about the executed tasks

The first one is a very specific type of analytics, so it cannot be extended or modified.
The second one is more general so other types of analytics' payloads will have the same structure.

The creation of the payload is done in the main process, then a detached subprocess is spawn to perform the http request.

In the tests I created a fake subprocess file that tests that the correct payload is received by the subprocess.
I also did a bit of a "hacky" thing to run the tests in the CI:
the analytics have a check that avoids the execution in CI environments. The check is performed on several env variables. So before the test I overwrite them, and then I reset them after the test.

TODO

  • There are debug logging functions that are commented because the debug log PR is not merged yet
  • I need to create an analytics test environment to check that the information are correctly sent

Copy link

vercel bot commented Jul 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 15, 2024 0:42am

Copy link

changeset-bot bot commented Jul 23, 2024

⚠️ No Changeset found

Latest commit: 082b407

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link
Member

@schaable schaable left a comment

Choose a reason for hiding this comment

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

Looks good, I left a couple comments

*
* @returns A promise that resolves to the path of the telemetry directory.
*/
export async function getTelemetryDir(packageName?: string): Promise<string> {
Copy link
Member

Choose a reason for hiding this comment

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

Can we make sure that this dir is not deleted by the clean --global task? /cc @schaable

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so, as the global flag deletes the cache path and telemetry uses the data path.

import { postJsonRequest } from "@ignored/hardhat-vnext-utils/request";

// These keys are expected to be public
const ANALYTICS_URL = "https://www.google-analytics.com/mp/collect";
Copy link
Member

Choose a reason for hiding this comment

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

Are these the test or the production values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Production values

Copy link
Contributor Author

@ChristopherDedominici ChristopherDedominici Jul 29, 2024

Choose a reason for hiding this comment

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

I'll updated the code with test variables. Before merging the code, I'll replace them with production values.

DO NOT REMOVE THIS COMMENT, it works as a reminder

}

export function getUserType(): string {
return isCi() ? "CI" : "Developer";
Copy link
Member

Choose a reason for hiding this comment

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

We don't send analytics hits from CIs anymore, so we should remove the userType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"result.json",
);

(() => {
Copy link
Member

Choose a reason for hiding this comment

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

This lambda is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

async function createSubprocessToSendAnalytics(
payload: TelemetryConsentPayload | Payload,
): Promise<void> {
const fileExt =
Copy link
Member

Choose a reason for hiding this comment

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

Let's document this here with a comment because we'll forget it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done here, with other smaller fixes

await writeJsonFile(process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH, payload);
}

await main();
Copy link
Member

Choose a reason for hiding this comment

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

We have top-level-await 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.

Done here, with other smaller fixes

@@ -0,0 +1,20 @@
// This file will be copied to the "analytics" folder so it will be executed as a subprocess.
Copy link
Member

Choose a reason for hiding this comment

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

Not used anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done here, with other smaller fixes

@alcuadrado
Copy link
Member

There are just a few small comments regarding the PR, but we need to actually validate that this is working as intended in analytics, and in particular, we should validate that the custom dimensions work. Note that we are using a new analytics property for testing, so no setup has been done there.

@schaable schaable added the v-next A Hardhat v3 development task label Aug 9, 2024
@kanej
Copy link
Member

kanej commented Aug 16, 2024

Both Chris and I have separately done a manual test that events are coming through with the expected custom dimensions. This PR is pointing at a test Google Analytics environment.

@ChristopherDedominici ChristopherDedominici added this pull request to the merge queue Aug 20, 2024
Merged via the queue into v-next with commit 2d4f8db Aug 20, 2024
22 checks passed
@ChristopherDedominici ChristopherDedominici deleted the feature/add-logic-to-send-telemetry-data branch August 20, 2024 12:16
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Port or reimplement and test the analytics integration
4 participants