From 362647fd680f72fad9fb82428860b6597d174d43 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sat, 26 Nov 2022 14:28:50 +0900 Subject: [PATCH] Add `collect-step-metrics` input (#525) * Add `collect-step-metrics` input * Update README.md --- .github/workflows/e2e.yaml | 1 + README.md | 24 ++++++++++++++++-------- action.yaml | 11 +++++------ src/main.ts | 1 + src/run.ts | 14 ++++++++++++-- src/workflowRun/metrics.ts | 20 +++++++++++++------- tests/run.test.ts | 4 ++++ 7 files changed, 52 insertions(+), 23 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 125a4808..f61d6151 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -33,3 +33,4 @@ jobs: with: datadog-api-key: ${{ secrets.DATADOG_API_KEY }} collect-job-metrics: true + collect-step-metrics: true diff --git a/README.md b/README.md index e99bf9ea..ff79b4ab 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ See also the actual metrics in the [E2E test](https://github.com/int128/datadog- ### Job -This action sends the following metrics if enabled. +This action sends the following metrics if `collect-job-metrics` is enabled. - `github.actions.job.total` - `github.actions.job.conclusion.{CONCLUSION}_total` @@ -163,7 +163,7 @@ It has the following tags: ### Step -This action sends the following metrics if enabled. +This action sends the following metrics if `collect-step-metrics` is enabled. - `github.actions.step.total` - `github.actions.step.conclusion.{CONCLUSION}_total` @@ -193,7 +193,11 @@ It has the following tags: - e.g. `ubuntu-latest` -### Enable job and step metrics +### Enable job or step metrics + +Note that this action calls GitHub GraphQL API to get jobs and steps of a workflow run. +It may cause the rate exceeding error if too many workflows are run. +It may also increase the cost of custom metrics in Datadog. To send the metrics of jobs and steps: @@ -202,11 +206,9 @@ To send the metrics of jobs and steps: with: datadog-api-key: ${{ secrets.DATADOG_API_KEY }} collect-job-metrics: true + collect-step-metrics: true ``` -Note that this action calls GitHub GraphQL API to get jobs and steps of a workflow run. -It may cause the rate exceeding error if too many workflows are run. - To send the metrics of jobs and steps on the default branch only: ```yaml @@ -214,6 +216,7 @@ To send the metrics of jobs and steps on the default branch only: with: datadog-api-key: ${{ secrets.DATADOG_API_KEY }} collect-job-metrics: ${{ github.event.workflow_run.head_branch == github.event.repository.default_branch }} + collect-step-metrics: ${{ github.event.workflow_run.head_branch == github.event.repository.default_branch }} ``` @@ -341,9 +344,14 @@ Name | Default | Description `datadog-api-key` | - | Datadog API key. If not set, this action does not send metrics actually `datadog-site` | - | Datadog Server name such as `datadoghq.eu`, `ddog-gov.com`, `us3.datadoghq.com` `send-pull-request-labels` | `false` | Send pull request labels as Datadog tags -`collect-job-metrics` | `false` | Collect metrics of jobs and steps +`collect-job-metrics` | `false` | Collect job metrics +`collect-step-metrics` | `false` | Collect step metrics + +### Breaking changes + +`collect-step-metrics` is explicitly required to send the step metrics. -Note that `collect-job-metrics-for-only-default-branch` is no longer supported. +`collect-job-metrics-for-only-default-branch` is no longer supported. Use `collect-job-metrics` instead. diff --git a/action.yaml b/action.yaml index c2233378..7fc9dc52 100644 --- a/action.yaml +++ b/action.yaml @@ -19,7 +19,11 @@ inputs: required: false collect-job-metrics: - description: Collect metrics of jobs and steps + description: Collect job metrics + required: false + default: 'false' + collect-step-metrics: + description: Collect step metrics required: false default: 'false' send-pull-request-labels: @@ -27,11 +31,6 @@ inputs: required: false default: 'false' - collect-job-metrics-for-only-default-branch: - description: No longer supported - required: false - deprecationMessage: Use collect-job-metrics instead - runs: using: 'node16' main: 'dist/index.js' diff --git a/src/main.ts b/src/main.ts index 00be2a97..0ce57408 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ const main = async (): Promise => { datadogApiKey: core.getInput('datadog-api-key') || undefined, datadogSite: core.getInput('datadog-site') || undefined, collectJobMetrics: core.getBooleanInput('collect-job-metrics'), + collectStepMetrics: core.getBooleanInput('collect-step-metrics'), sendPullRequestLabels: core.getBooleanInput('send-pull-request-labels'), }) } diff --git a/src/run.ts b/src/run.ts index 42ab6f25..9b382384 100644 --- a/src/run.ts +++ b/src/run.ts @@ -17,6 +17,7 @@ type Inputs = { datadogApiKey?: string datadogSite?: string collectJobMetrics: boolean + collectStepMetrics: boolean sendPullRequestLabels: boolean } @@ -52,7 +53,7 @@ const handleWorkflowRun = async (e: WorkflowRunEvent, inputs: Inputs) => { if (e.action === 'completed') { let checkSuite - if (inputs.collectJobMetrics) { + if (inputs.collectJobMetrics || inputs.collectStepMetrics) { const octokit = github.getOctokit(inputs.githubToken) try { checkSuite = await queryCompletedCheckSuite(octokit, { @@ -66,7 +67,16 @@ const handleWorkflowRun = async (e: WorkflowRunEvent, inputs: Inputs) => { if (checkSuite) { core.info(`Found check suite with ${checkSuite.node.checkRuns.nodes.length} check run(s)`) } - return computeWorkflowRunJobStepMetrics(e, checkSuite) + + const metrics = computeWorkflowRunJobStepMetrics(e, checkSuite) + const series = [...metrics.workflowRunMetrics] + if (inputs.collectJobMetrics) { + series.push(...metrics.jobMetrics) + } + if (inputs.collectStepMetrics) { + series.push(...metrics.stepMetrics) + } + return series } } diff --git a/src/workflowRun/metrics.ts b/src/workflowRun/metrics.ts index 94867ad7..81482344 100644 --- a/src/workflowRun/metrics.ts +++ b/src/workflowRun/metrics.ts @@ -17,12 +17,18 @@ const computeCommonTags = (e: WorkflowRunCompletedEvent): string[] => [ `default_branch:${(e.workflow_run.head_branch === e.repository.default_branch).toString()}`, ] +export type WorkflowRunJobStepMetrics = { + workflowRunMetrics: v1.Series[] + jobMetrics: v1.Series[] + stepMetrics: v1.Series[] +} + export const computeWorkflowRunJobStepMetrics = ( e: WorkflowRunCompletedEvent, checkSuite?: CompletedCheckSuite -): v1.Series[] => { +): WorkflowRunJobStepMetrics => { if (checkSuite === undefined) { - return computeWorkflowRunMetrics(e) + return { workflowRunMetrics: computeWorkflowRunMetrics(e), jobMetrics: [], stepMetrics: [] } } let workflowDefinition @@ -35,11 +41,11 @@ export const computeWorkflowRunJobStepMetrics = ( core.info(`Found ${Object.keys(workflowDefinition.jobs).length} job(s) in the workflow file`) } - return [ - ...computeWorkflowRunMetrics(e, checkSuite), - ...computeJobMetrics(e, checkSuite, workflowDefinition), - ...computeStepMetrics(e, checkSuite, workflowDefinition), - ] + return { + workflowRunMetrics: computeWorkflowRunMetrics(e, checkSuite), + jobMetrics: computeJobMetrics(e, checkSuite, workflowDefinition), + stepMetrics: computeStepMetrics(e, checkSuite, workflowDefinition), + } } export const computeWorkflowRunMetrics = ( diff --git a/tests/run.test.ts b/tests/run.test.ts index 47d93f1a..9a918e81 100644 --- a/tests/run.test.ts +++ b/tests/run.test.ts @@ -46,6 +46,7 @@ test('workflow_run with collectJobMetrics', async () => { githubTokenForRateLimitMetrics: 'GITHUB_TOKEN', datadogApiKey: 'DATADOG_API_KEY', collectJobMetrics: true, + collectStepMetrics: true, sendPullRequestLabels: false, } ) @@ -69,6 +70,7 @@ test('workflow_run', async () => { githubTokenForRateLimitMetrics: 'GITHUB_TOKEN', datadogApiKey: 'DATADOG_API_KEY', collectJobMetrics: false, + collectStepMetrics: false, sendPullRequestLabels: false, } ) @@ -92,6 +94,7 @@ test('pull_request_opened', async () => { githubTokenForRateLimitMetrics: 'GITHUB_TOKEN', datadogApiKey: 'DATADOG_API_KEY', collectJobMetrics: false, + collectStepMetrics: false, sendPullRequestLabels: false, } ) @@ -116,6 +119,7 @@ test('pull_request_closed', async () => { githubTokenForRateLimitMetrics: 'GITHUB_TOKEN', datadogApiKey: 'DATADOG_API_KEY', collectJobMetrics: false, + collectStepMetrics: false, sendPullRequestLabels: true, } )