Skip to content

Commit

Permalink
Add a post step to report telemetry (#492)
Browse files Browse the repository at this point in the history
Adds a post step to both our Actions (with shared code) to report some
telemetry.
  • Loading branch information
edoardopirovano authored Aug 16, 2024
2 parents fc0e8ba + aaa7798 commit ef0f3cc
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 78 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ RUN yarn --frozen-lockfile

COPY tsconfig.base.json tsconfig.json ./

COPY scripts/post-step.sh /app

COPY src src

RUN yarn build
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ outputs: {}
runs:
using: "docker"
image: "docker://ghcr.io/alwaysmeticulous/report-diffs-action:main"
post-entrypoint: "/app/post-step.sh"
env:
API_TOKEN: ${{ inputs.api-token }}
GITHUB_TOKEN: ${{ inputs.github-token }}
Expand Down
1 change: 1 addition & 0 deletions cloud-compute/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ outputs: {}
runs:
using: node20
main: "../out/cloud-compute.entrypoint.js"
post: "../out/post-step.entrypoint.js"
branding:
color: purple
icon: camera
70 changes: 35 additions & 35 deletions out/cloud-compute.entrypoint.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions out/post-step.entrypoint.js

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
"license": "Unliscensed",
"private": true,
"main": "dist/main.entrypoint.mjs",
"post-step": "out/post-step.entrypoint.js",
"cloud-compute": "out/cloud-compute.entrypoint.js",
"targets": {
"main": {
"source": "src/main.entrypoint.ts",
"distDir": "dist",
"outputFormat": "esmodule"
},
"post": {
"source": "src/post-step.entrypoint.ts",
"includeNodeModules": true,
"distDir": "out",
"sourceMap": false,
"outputFormat": "commonjs"
},
"cloud-compute": {
"source": "src/cloud-compute.entrypoint.ts",
"includeNodeModules": true,
Expand All @@ -38,12 +46,12 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@alwaysmeticulous/client": "^2.145.0",
"@alwaysmeticulous/common": "^2.145.0",
"@alwaysmeticulous/remote-replay-launcher": "^2.145.0",
"@alwaysmeticulous/client": "^2.147.1",
"@alwaysmeticulous/common": "^2.146.1",
"@alwaysmeticulous/remote-replay-launcher": "^2.147.1",
"//": "Upgrading `replay-orchestrator-launcher`? Consider bumping the environment version `LOGICAL_ENVIRONMENT_VERSION` in `constants.ts` if the new version includes visible changes.",
"@alwaysmeticulous/replay-orchestrator-launcher": "^2.145.0",
"@alwaysmeticulous/sentry": "^2.145.0",
"@alwaysmeticulous/replay-orchestrator-launcher": "^2.147.1",
"@alwaysmeticulous/sentry": "^2.146.1",
"@sentry/node": "^7.107.0",
"lodash.debounce": "^4.0.8",
"loglevel": "^1.8.1",
Expand All @@ -52,7 +60,7 @@
},
"devDependencies": {
"@alwaysmeticulous/api": "^2.144.0",
"@alwaysmeticulous/sdk-bundles-api": "^2.145.0",
"@alwaysmeticulous/sdk-bundles-api": "^2.146.1",
"@parcel/packager-ts": "^2.12.0",
"@parcel/transformer-typescript-types": "^2.12.0",
"@types/jest": "^27.0.3",
Expand Down
3 changes: 3 additions & 0 deletions scripts/post-step.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash

node /app/out/post-step.entrypoint.js
65 changes: 65 additions & 0 deletions src/common/run-post-step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getInput } from "@actions/core";
import { context } from "@actions/github";
import { createClient, emitTelemetry } from "@alwaysmeticulous/client";
import { getInputFromEnv } from "./get-input-from-env";
import { getOctokitOrFail } from "./octokit";

export const runPostStep = async (): Promise<void> => {
const apiToken = getActionInput("api-token");
const githubToken = getActionInput("github-token");
const octokit = getOctokitOrFail(githubToken);
const workflow = await octokit.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
});

const values: Record<string, number> = {
"report_diffs_action.was_cancelled":
workflow.data.status === "cancelled" ? 1 : 0,
};

// workflow.data.run_started_at should always be set since the workflow is running, but we need to make the compiler happy
const workflowStartTime = new Date();
if (workflow.data.run_started_at) {
workflowStartTime.setTime(new Date(workflow.data.run_started_at).getTime());
const timeSinceStart =
new Date().getTime() - new Date(workflow.data.run_started_at).getTime();
values["report_diffs_action.job_duration_seconds"] = timeSinceStart / 1000;
}

if (context.payload.pull_request?.number) {
const prComments = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
per_page: 1000,
});
values["report_diffs_action.saw_comment"] = prComments.data.some(
(c) =>
c.body?.includes(
"<!--- alwaysmeticulous/report-diffs-action/status-comment"
) && new Date(c.updated_at).getTime() >= workflowStartTime.getTime()
)
? 1
: 0;
}

const client = createClient({ apiToken });
await emitTelemetry({ client, values });
};

/**
* This method checks the input from the action in two different ways because we don't know if we are the post step
* for the main action or the cloud-compute one so we need to support both.
*/
const getActionInput = (name: string) => {
return (
getInput(name) ||
getInputFromEnv({
name: name,
required: true,
type: "string",
})
);
};
8 changes: 8 additions & 0 deletions src/post-step.entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { warning } from "@actions/core";
import { runPostStep } from "./common/run-post-step";

runPostStep().catch((error) => {
// We're just emitting telemetry in this post step, so log a warning but don't fail
const message = error instanceof Error ? error.message : `${error}`;
warning(message);
});
74 changes: 37 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,33 @@
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/api/-/api-2.144.0.tgz#265053f34ced76818f1933f62afef32a2cfe87c2"
integrity sha512-0bSHe2Gctf5KyEVi/IcOd543voIkzzhKhb/AnOVxjY/ONhmIwpcErt0f/4ETdwboNAYwVjqSHRfxGGKf+JsO6A==

"@alwaysmeticulous/client@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/client/-/client-2.145.0.tgz#0fd27d4df8b659c669c4183aefd82695f5dbacb2"
integrity sha512-7LwcBJiv55XOuSIupSWCT7GgB0oXrsR3T5/H77z940kx+UhzU4dCtJDfdMYNHX2NBq0qZHpBJp0860Qk8EicbA==
"@alwaysmeticulous/client@^2.147.1":
version "2.147.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/client/-/client-2.147.1.tgz#5d53d39119c1a1d7cde021ece64bdd9d35958937"
integrity sha512-LEAiQw+d+rJp0VGba4qrmNqTOc3ff1QFWdsK+bxsOsB88AI6rcNXHlGH7ANDwEhEJpRmGR31DGBcRLaBjbyOKw==
dependencies:
"@alwaysmeticulous/api" "^2.144.0"
"@alwaysmeticulous/common" "^2.145.0"
"@alwaysmeticulous/common" "^2.146.1"
axios "^1.2.6"
axios-retry "^4.1.0"
loglevel "^1.8.0"

"@alwaysmeticulous/common@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/common/-/common-2.145.0.tgz#68de9dafd7de6ba3046bf9c0e78d1bc52f2a056f"
integrity sha512-a9M+Howvaq0FmYDQpnDqHj95KB/FGeK6oXtuXUlt5GLfejR69x1h7Sc+dRawxfGd1cVhPBkict577U8FaY6Jsg==
"@alwaysmeticulous/common@^2.146.1":
version "2.146.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/common/-/common-2.146.1.tgz#d8e2acd8cb71e2a72e78e70bf9d12f2b7c93dc34"
integrity sha512-3V84vSscDu4jRg8eER3JOedKTZHeftfdWKXSOUQ7uxXxkvjAW/+qLsNMn7iNRl8KVCD7s3cuEsgXQDsd7vyuuQ==
dependencies:
loglevel "^1.8.0"
luxon "^3.2.1"

"@alwaysmeticulous/downloading-helpers@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/downloading-helpers/-/downloading-helpers-2.145.0.tgz#8d5683825696c1cf9467c5fa1b92a17240516f78"
integrity sha512-UBiYoSPQDkcYel/9s7APQjC5PLLyRD3CCMb7IQWdPZTFaX6vg/Yy5FTczdQJzTNRts6rNKCLDNiwn4unZcEJtA==
"@alwaysmeticulous/downloading-helpers@^2.147.1":
version "2.147.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/downloading-helpers/-/downloading-helpers-2.147.1.tgz#29e54fdfc6cd46936750d9bd95599537e75df19f"
integrity sha512-Xzmcy71zHDLh1gUAyDFnKz1bzuxoGk71ZD5+B9I/wAJFgmHSn+6/uoD4y2WqGEYHhbwr5C8kCKBKor6OuIevwQ==
dependencies:
"@alwaysmeticulous/api" "^2.144.0"
"@alwaysmeticulous/client" "^2.145.0"
"@alwaysmeticulous/common" "^2.145.0"
"@alwaysmeticulous/client" "^2.147.1"
"@alwaysmeticulous/common" "^2.146.1"
axios "^1.2.6"
axios-retry "^4.1.0"
extract-zip "^2.0.1"
Expand All @@ -72,38 +72,38 @@
p-limit "^3.1.0"
proper-lockfile "^4.1.2"

"@alwaysmeticulous/remote-replay-launcher@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/remote-replay-launcher/-/remote-replay-launcher-2.145.0.tgz#39e06a951d6dd8624835b2696e09480fc34bbcb4"
integrity sha512-OaA/NfzALDXl/yycu6GtH018sOCFtkge422mCHN+niwa8bP6vDqbLSgjCoguJoOlFEDnK2Dck6Z9jFpYLZhg1Q==
"@alwaysmeticulous/remote-replay-launcher@^2.147.1":
version "2.147.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/remote-replay-launcher/-/remote-replay-launcher-2.147.1.tgz#e5e4eef75eef7f52992731c139fcfc0adb62f12e"
integrity sha512-s6DI4iC//Ps/PjAae4bf1v00j1PpRKOL33hzhs4mmZFnvlWM7dWbN7Vt61rycEcjqnaUNydvEPuDqy0aGDpcQQ==
dependencies:
"@alwaysmeticulous/client" "^2.145.0"
"@alwaysmeticulous/common" "^2.145.0"
"@alwaysmeticulous/client" "^2.147.1"
"@alwaysmeticulous/common" "^2.146.1"
"@alwaysmeticulous/tunnels-client" "^2.143.0"
loglevel "^1.8.0"

"@alwaysmeticulous/replay-orchestrator-launcher@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/replay-orchestrator-launcher/-/replay-orchestrator-launcher-2.145.0.tgz#db3441005fe922cd2f34d47cf39914c503887d86"
integrity sha512-bEOQuZBJwtyIwU1hfwPMfrH8iP1QzfPGUpwLMzhTVMCts3AmGb3OYerJmEJuOk1f7BH2sPDDLqynkNdN+jL8LA==
"@alwaysmeticulous/replay-orchestrator-launcher@^2.147.1":
version "2.147.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/replay-orchestrator-launcher/-/replay-orchestrator-launcher-2.147.1.tgz#e699a04fc32c55524e568f28fea78a53560177ab"
integrity sha512-Rku7npLb+c02HkHeh3gzEb4H3/+FTNBXvE7M8WbX33tiSKeJj06PYER67T0F1xxRhzbLqf4HGF8w5FLa89Ei5A==
dependencies:
"@alwaysmeticulous/common" "^2.145.0"
"@alwaysmeticulous/downloading-helpers" "^2.145.0"
"@alwaysmeticulous/sdk-bundles-api" "^2.145.0"
"@alwaysmeticulous/common" "^2.146.1"
"@alwaysmeticulous/downloading-helpers" "^2.147.1"
"@alwaysmeticulous/sdk-bundles-api" "^2.146.1"
loglevel "^1.8.0"
puppeteer "21.9.0"

"@alwaysmeticulous/sdk-bundles-api@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/sdk-bundles-api/-/sdk-bundles-api-2.145.0.tgz#10b253ac2a3b8c0920601eb41fe8fec7d3fc3868"
integrity sha512-G12xevDlZ9IM2UXHg/3K+nYX1O11qIaSFrHr3QH7Wo6AJmk+2GJnyxixVhtfFPBc4kr9xpi6FiaYPzWYQ7p9RQ==
"@alwaysmeticulous/sdk-bundles-api@^2.146.1":
version "2.146.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/sdk-bundles-api/-/sdk-bundles-api-2.146.1.tgz#ed9d26225334f67ba5d942fa00d51ef9ff1517a4"
integrity sha512-5+8JToMEe1ne/knfXzzUtxBtPBWsVN2pgp50Q9N3UZcYgyyMOu0uropijL09xFNSNZnKs8so8AKfFVJH4ZYQ4w==

"@alwaysmeticulous/sentry@^2.145.0":
version "2.145.0"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/sentry/-/sentry-2.145.0.tgz#2007ba9f9aa6aaed3391a4ad5220d0c05f4ae3b9"
integrity sha512-0d7mbSfKEdBJbbwUIanzF5kjYynBRe06Hxk+CS5tnLs3OBxAkstxWc0ydzbe7rCICUGMorMj1g7rjIqGwi6Gmg==
"@alwaysmeticulous/sentry@^2.146.1":
version "2.146.1"
resolved "https://registry.yarnpkg.com/@alwaysmeticulous/sentry/-/sentry-2.146.1.tgz#b54cd013693c576f4bf1aa2039c37987b2b15cdb"
integrity sha512-5i1B7tqmqXTFMOzT0/FdVsBevFl7zClQB5v0zcOnWhu94b+X6+LUrkn35jrfh52Pa2bwvYrNNHgJQlubFCl2mw==
dependencies:
"@alwaysmeticulous/common" "^2.145.0"
"@alwaysmeticulous/common" "^2.146.1"
"@sentry/node" "^7.107.0"
"@sentry/tracing" "^7.107.0"
luxon "^3.2.1"
Expand Down

0 comments on commit ef0f3cc

Please sign in to comment.