Skip to content

Commit

Permalink
Don't send label tag by default (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Feb 26, 2022
1 parent 66d6929 commit b0c3458
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ It has the following tags:
- `label` = label(s) of a pull request
- `merged` = `true` or `false`

You can aggregate a metric by a label in Datadog.
You can set `send-pull-request-labels` to use `label` tag in Datadog.
If a pull request has multiple labels, this action sends the metrics for each label.
This action does not send labels by default from the cost perspective of the custom metrics.


### Push
Expand Down Expand Up @@ -266,6 +267,7 @@ Name | Default | Description
`github-token-rate-limit-metrics` | `github.token` | GitHub token for rate limit metrics
`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

Note that `collect-job-metrics-for-only-default-branch` is no longer supported.
Expand Down
7 changes: 7 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ inputs:
description: GitHub token for rate limit metrics
default: ${{ github.token }}
required: true

datadog-api-key:
description: Datadog API key (dry-run if not set)
required: false
datadog-site:
description: Datadog Site name if different than datadoghq.com.
required: false

collect-job-metrics:
description: Collect metrics of jobs and steps
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
Expand Down
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'),
sendPullRequestLabels: core.getBooleanInput('send-pull-request-labels'),
})
}

Expand Down
19 changes: 15 additions & 4 deletions src/pullRequest/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ export const computePullRequestOpenedMetrics = (e: PullRequestOpenedEvent): Seri
]
}

export const computePullRequestClosedMetrics = (e: PullRequestClosedEvent, pr?: ClosedPullRequest): Series[] => {
type ClosedMetricsOptions = {
sendPullRequestLabels: boolean
}

export const computePullRequestClosedMetrics = (
e: PullRequestClosedEvent,
pr: ClosedPullRequest | undefined,
options: ClosedMetricsOptions
): Series[] => {
const tags = computeCommonTags(e)
tags.push(`merged:${String(e.pull_request.merged)}`)
const t = unixTime(e.pull_request.closed_at)
Expand Down Expand Up @@ -128,9 +136,12 @@ export const computePullRequestClosedMetrics = (e: PullRequestClosedEvent, pr?:
)
}

// Datadog treats a tag as combination of values.
// For example, if we send a metric with tags `label:foo` and `label:bar`, Datadog will show `label:foo,bar`.
return expandSeriesByLabels(series, e.pull_request.labels)
if (options.sendPullRequestLabels) {
// Datadog treats a tag as combination of values.
// For example, if we send a metric with tags `label:foo` and `label:bar`, Datadog will show `label:foo,bar`.
return expandSeriesByLabels(series, e.pull_request.labels)
}
return series
}

const unixTime = (s: string): number => Date.parse(s) / 1000
3 changes: 2 additions & 1 deletion 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
sendPullRequestLabels: boolean
}

export const run = async (context: GitHubContext, inputs: Inputs): Promise<void> => {
Expand Down Expand Up @@ -85,7 +86,7 @@ const handlePullRequest = async (e: PullRequestEvent, context: GitHubContext, in
} catch (error) {
core.warning(`Could not get the pull request: ${String(error)}`)
}
return computePullRequestClosedMetrics(e, closedPullRequest)
return computePullRequestClosedMetrics(e, closedPullRequest, inputs)
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/pullRequest/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ test('computePullRequestOpenedMetrics', () => {
})

test('computePullRequestClosedMetrics', () => {
const series = computePullRequestClosedMetrics(examplePullRequestClosedEvent)
const series = computePullRequestClosedMetrics(examplePullRequestClosedEvent, undefined, {
sendPullRequestLabels: true,
})
expect(series).toMatchSnapshot()
})

test('computePullRequestClosedMetricsWithQuery', () => {
const series = computePullRequestClosedMetrics(examplePullRequestClosedEvent, exampleClosedPullRequest)
const series = computePullRequestClosedMetrics(examplePullRequestClosedEvent, exampleClosedPullRequest, {
sendPullRequestLabels: true,
})
expect(series).toMatchSnapshot()
})
4 changes: 4 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test('workflow_run with collectJobMetrics', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: true,
sendPullRequestLabels: false,
}
)
expect(getOctokit).toBeCalledWith('GITHUB_TOKEN')
Expand All @@ -78,6 +79,7 @@ test('workflow_run', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
sendPullRequestLabels: false,
}
)
expect(getOctokit).toBeCalledWith('GITHUB_TOKEN')
Expand All @@ -103,6 +105,7 @@ test('pull_request_opened', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
sendPullRequestLabels: false,
}
)
expect(getOctokit).toBeCalledWith('GITHUB_TOKEN')
Expand All @@ -126,6 +129,7 @@ test('pull_request_closed', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
collectJobMetrics: false,
sendPullRequestLabels: true,
}
)
expect(getOctokit).toBeCalledWith('GITHUB_TOKEN')
Expand Down

0 comments on commit b0c3458

Please sign in to comment.