generated from int128/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add metrics pattern filter * Fix * Fix * Add doc * Refactor * Pin dependency
- Loading branch information
Showing
11 changed files
with
143 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { minimatch } from 'minimatch' | ||
import { v1 } from '@datadog/datadog-api-client' | ||
|
||
type Inputs = { | ||
metricsPatterns: string[] | ||
datadogTags: string[] | ||
} | ||
|
||
export type MetricsFilter = <S extends v1.Series | v1.DistributionPointsSeries>(series: S[]) => S[] | ||
|
||
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) | ||
} | ||
} | ||
|
||
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('!')) { | ||
matched = matched && minimatch(metric, pattern) | ||
} else { | ||
matched = matched || minimatch(metric, pattern) | ||
} | ||
} | ||
return matched | ||
} | ||
|
||
export const injectTags = <S extends { tags?: string[] }>(series: S[], tags: string[]): S[] => { | ||
if (tags.length === 0) { | ||
return series | ||
} | ||
return series.map((s) => ({ ...s, tags: [...(s.tags ?? []), ...tags] })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createMatcher, injectTags } from '../src/filter.js' | ||
|
||
describe('injectTags', () => { | ||
it('should return series if tags is empty', () => { | ||
const series = [{ tags: [] }] | ||
expect(injectTags(series, [])).toEqual(series) | ||
}) | ||
it('should return series with tags', () => { | ||
const series = [{ tags: ['tag1:value1'] }] | ||
expect(injectTags(series, ['tag2:value2'])).toEqual([{ tags: ['tag1:value1', 'tag2:value2'] }]) | ||
}) | ||
}) | ||
|
||
describe('createMatcher', () => { | ||
it('should match anything when no pattern is given', () => { | ||
const matcher = createMatcher([]) | ||
expect(matcher('github.foo.bar')).toBe(true) | ||
expect(matcher('github.foo.baz')).toBe(true) | ||
}) | ||
it('should match it when a pattern is given', () => { | ||
const matcher = createMatcher(['*.bar']) | ||
expect(matcher('github.foo.bar')).toBe(true) | ||
expect(matcher('github.foo.baz')).toBe(false) | ||
}) | ||
it('should exclude it when a negative pattern is given', () => { | ||
const matcher = createMatcher(['*', '!*.github.*']) | ||
expect(matcher('foo.github.bar')).toBe(false) | ||
expect(matcher('foo.github.baz')).toBe(false) | ||
expect(matcher('example.bar')).toBe(true) | ||
expect(matcher('example.baz')).toBe(true) | ||
}) | ||
it('should take higher 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 matcher = createMatcher(['*.bar', '!foo.*', '*.baz']) | ||
expect(matcher('foo.github.bar')).toBe(false) | ||
expect(matcher('foo.github.baz')).toBe(true) | ||
expect(matcher('example.bar')).toBe(true) | ||
expect(matcher('example.baz')).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters