Skip to content

Commit

Permalink
add exclude tags option
Browse files Browse the repository at this point in the history
  • Loading branch information
gradam committed Jan 7, 2025
1 parent 2dc841c commit 6fb347a
Show file tree
Hide file tree
Showing 7 changed files with 1,561 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ You can set the following inputs:
| `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-patterns` | - | Filter the metrics by patterns in a multiline string |
| `tags-to-exclude` | - | Exclude specified tags by names 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 |
Expand Down Expand Up @@ -457,6 +458,23 @@ steps:

If both include and exclude patterns are given, the later pattern has higher precedence.

### Exclude Tags

The `tags-to-exclude` input allows you to specify tags that should be excluded from being sent to Datadog. You can provide this as a multiline string where each line contains a tag to be excluded.

To exclude specific tags, include the `tags-to-exclude` option like this:

```yaml
steps:
- uses: int128/datadog-actions-metrics@v1
with:
tags-to-exclude: |
job_id
runs_on
```

Each tag listed in `tags-to-exclude` will not be sent to Datadog, helping you streamline the metrics data you wish to analyze.

### Proxy

To connect to Datadog API via a HTTPS proxy, set `https_proxy` environment variable.
Expand Down
1 change: 1 addition & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Inputs = {
datadogSite?: string
datadogTags: string[]
metricsPatterns: string[]
tagsToExclude: string[]
}

export type MetricsClient = {
Expand Down
13 changes: 12 additions & 1 deletion src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v1 } from '@datadog/datadog-api-client'
type Inputs = {
metricsPatterns: string[]
datadogTags: string[]
tagsToExclude: string[]
}

export type MetricsFilter = <S extends v1.Series | v1.DistributionPointsSeries>(series: S[]) => S[]
Expand All @@ -12,7 +13,7 @@ export const createMetricsFilter = (inputs: Inputs): MetricsFilter => {
const matcher = createMatcher(inputs.metricsPatterns)
return (series) => {
series = series.filter((s) => matcher(s.metric))
return injectTags(series, inputs.datadogTags)
return excludeTags(injectTags(series, inputs.datadogTags), inputs.tagsToExclude)
}
}

Expand All @@ -39,3 +40,13 @@ export const injectTags = <S extends { tags?: string[] }>(series: S[], tags: str
}
return series.map((s) => ({ ...s, tags: [...(s.tags ?? []), ...tags] }))
}

export const excludeTags = <S extends { tags?: string[] }>(series: S[], tagsToExclude: string[]): S[] => {
if (tagsToExclude.length === 0) {
return series
}
return series.map((s) => ({
...s,
tags: (s.tags || []).filter((tag) => !tagsToExclude.some((exclude) => tag.startsWith(`${exclude}:`))),
}))
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const main = async (): Promise<void> => {
preferDistributionJobMetrics: core.getBooleanInput('prefer-distribution-job-metrics'),
preferDistributionStepMetrics: core.getBooleanInput('prefer-distribution-step-metrics'),
sendPullRequestLabels: core.getBooleanInput('send-pull-request-labels'),
tagsToExclude: core.getMultilineInput('tags-to-exclude'),
})
}

Expand Down
1 change: 1 addition & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Inputs = {
preferDistributionJobMetrics: boolean
preferDistributionStepMetrics: boolean
sendPullRequestLabels: boolean
tagsToExclude: string[]
}

export const run = async (context: GitHubContext, inputs: Inputs): Promise<void> => {
Expand Down
Loading

0 comments on commit 6fb347a

Please sign in to comment.