Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Aug 14, 2024
1 parent 65f4f36 commit 132d545
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,18 @@ You can set the following inputs:
| `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` |
| `datadog-tags` | - | Additional tags in the form of `key:value` in a multiline string |
| `metrics-filter` | - | Filter the metrics by given patterns in a multiline string |
| `send-pull-request-labels` | `false` | Send pull request labels as Datadog tags |
| `collect-job-metrics` | `false` | Collect job metrics |
| `collect-step-metrics` | `false` | Collect step metrics |
| `prefer-distribution-workflow-run-metrics` | `false` | If true, send the distribution metrics instead of gauge metrics |
| `prefer-distribution-job-metrics` | `false` | If true, send the distribution metrics instead of gauge metrics |
| `prefer-distribution-step-metrics` | `false` | If true, send the distribution metrics instead of gauge metrics |

### Filter metrics

If `metrics-filter` is set, this action sends only metrics.

### Proxy

To connect to Datadog API via a HTTPS proxy, set `https_proxy` environment variable.
Expand Down
3 changes: 3 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
datadog-tags:
description: Additional tags in the form of `key:value` in a multiline string
required: false
metrics-filter:
description: Filter the metrics by given patterns in a multiline string
required: false

collect-job-metrics:
description: Collect job metrics
Expand Down
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class RealMetricsClient implements MetricsClient {
export const createMatcher =
(patterns: string[]) =>
(metric: string): boolean => {
if (patterns.length === 0) {
return true
}
let matched = false
for (const pattern of patterns) {
if (pattern.startsWith('!')) {
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,
datadogTags: core.getMultilineInput('datadog-tags'),
metricsFilter: core.getMultilineInput('metrics-filter'),
collectJobMetrics: core.getBooleanInput('collect-job-metrics'),
collectStepMetrics: core.getBooleanInput('collect-step-metrics'),
preferDistributionWorkflowRunMetrics: core.getBooleanInput('prefer-distribution-workflow-run-metrics'),
Expand Down
1 change: 1 addition & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Inputs = {
datadogApiKey?: string
datadogSite?: string
datadogTags: string[]
metricsFilter: string[]
collectJobMetrics: boolean
collectStepMetrics: boolean
preferDistributionWorkflowRunMetrics: boolean
Expand Down
29 changes: 25 additions & 4 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@ describe('injectTags', () => {
})
})

describe('createMetricsFilter', () => {
it('should match', () => {
const filter = createMatcher(['*.bar', '!foo.*'])
expect(filter('foo.bar')).toBeTruthy()
describe('createMatcher', () => {
it('should match anything when no pattern is given', () => {
const filter = createMatcher([])
expect(filter('foo.bar')).toBe(true)
expect(filter('foo.baz')).toBe(true)
})
it('should match it when a pattern is given', () => {
const filter = createMatcher(['*.bar'])
expect(filter('foo.bar')).toBe(true)
expect(filter('foo.baz')).toBe(false)
})
it('should exclude it when a negative pattern is given', () => {
const filter = createMatcher(['**', '!foo.*'])
expect(filter('foo.bar')).toBe(false)
expect(filter('foo.baz')).toBe(false)
expect(filter('example.bar')).toBe(true)
expect(filter('example.baz')).toBe(true)
})
it('should take precedence to the later pattern', () => {
// https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow#example-including-and-excluding-branches
const filter = createMatcher(['*.bar', '!foo.*', '*.baz'])
expect(filter('foo.bar')).toBe(false)
expect(filter('foo.baz')).toBe(true)
expect(filter('example.bar')).toBe(true)
expect(filter('example.baz')).toBe(true)
})
})
4 changes: 4 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test('workflow_run with collectJobMetrics', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
datadogTags: [],
metricsFilter: [],
collectJobMetrics: true,
collectStepMetrics: true,
preferDistributionWorkflowRunMetrics: false,
Expand Down Expand Up @@ -75,6 +76,7 @@ test('workflow_run', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
datadogTags: [],
metricsFilter: [],
collectJobMetrics: false,
collectStepMetrics: false,
preferDistributionWorkflowRunMetrics: false,
Expand Down Expand Up @@ -103,6 +105,7 @@ test('pull_request_opened', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
datadogTags: [],
metricsFilter: [],
collectJobMetrics: false,
collectStepMetrics: false,
preferDistributionWorkflowRunMetrics: false,
Expand Down Expand Up @@ -132,6 +135,7 @@ test('pull_request_closed', async () => {
githubTokenForRateLimitMetrics: 'GITHUB_TOKEN',
datadogApiKey: 'DATADOG_API_KEY',
datadogTags: [],
metricsFilter: [],
collectJobMetrics: false,
collectStepMetrics: false,
preferDistributionWorkflowRunMetrics: false,
Expand Down

0 comments on commit 132d545

Please sign in to comment.