Skip to content

Commit

Permalink
Add collect-step-metrics input (#525)
Browse files Browse the repository at this point in the history
* Add `collect-step-metrics` input

* Update README.md
  • Loading branch information
int128 authored Nov 26, 2022
1 parent d36ebd0 commit 362647f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ jobs:
with:
datadog-api-key: ${{ secrets.DATADOG_API_KEY }}
collect-job-metrics: true
collect-step-metrics: true
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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:

Expand All @@ -202,18 +206,17 @@ 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
- uses: int128/datadog-actions-metrics@v1
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 }}
```


Expand Down Expand Up @@ -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.


Expand Down
11 changes: 5 additions & 6 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ 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:
description: Send pull request labels as Datadog tags
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'
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const main = async (): Promise<void> => {
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'),
})
}
Expand Down
14 changes: 12 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Inputs = {
datadogApiKey?: string
datadogSite?: string
collectJobMetrics: boolean
collectStepMetrics: boolean
sendPullRequestLabels: boolean
}

Expand Down Expand Up @@ -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, {
Expand All @@ -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
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/workflowRun/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = (
Expand Down
4 changes: 4 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test('workflow_run with collectJobMetrics', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: true,
collectStepMetrics: true,
sendPullRequestLabels: false,
}
)
Expand All @@ -69,6 +70,7 @@ test('workflow_run', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
collectStepMetrics: false,
sendPullRequestLabels: false,
}
)
Expand All @@ -92,6 +94,7 @@ test('pull_request_opened', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
collectStepMetrics: false,
sendPullRequestLabels: false,
}
)
Expand All @@ -116,6 +119,7 @@ test('pull_request_closed', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
collectStepMetrics: false,
sendPullRequestLabels: true,
}
)
Expand Down

0 comments on commit 362647f

Please sign in to comment.