-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'benoit/validate-tags--fix-staging' (87ee856) into stagi…
…ng-51 pm_trace_id: 6351007 feature_branch_pipeline_id: 6351007 source: to-staging * commit '87ee85682347e9d7a8efc46f4b8d7388af82f518': ✅ use valid tag values in tests 👌 simplify implementation 🔥 [RUMF-1089] Cleanup legacy intake URLs (#1214) ✨ [RUMF-827] sanitize tags ♻️ [RUMF-827] add function to build tags at a higher level
- Loading branch information
Showing
8 changed files
with
108 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { display } from '../../tools/display' | ||
import { InitConfiguration } from './configuration' | ||
import { buildTag, buildTags, TAG_SIZE_LIMIT } from './tags' | ||
|
||
const LARGE_VALUE = Array(TAG_SIZE_LIMIT + 10).join('a') | ||
|
||
describe('buildTags', () => { | ||
it('build tags from init configuration', () => { | ||
expect( | ||
buildTags({ | ||
service: 'foo', | ||
env: 'bar', | ||
version: 'baz', | ||
} as InitConfiguration) | ||
).toEqual(['env:bar', 'service:foo', 'version:baz']) | ||
}) | ||
}) | ||
|
||
describe('buildTag', () => { | ||
let displaySpy: jasmine.Spy<typeof display.warn> | ||
beforeEach(() => { | ||
displaySpy = spyOn(display, 'warn') | ||
}) | ||
|
||
it('lowercases tags', () => { | ||
expect(buildTag('env', 'BaR')).toBe('env:bar') | ||
expectWarning() | ||
}) | ||
|
||
it('trims large tags', () => { | ||
const tag = buildTag('env', LARGE_VALUE) | ||
expect(tag.length).toBe(TAG_SIZE_LIMIT) | ||
expect(tag).toBe(`env:${LARGE_VALUE.slice(0, TAG_SIZE_LIMIT - 4)}`) | ||
expectWarning() | ||
}) | ||
|
||
it('replaces forbidden characters with slashes', () => { | ||
expect(buildTag('env', 'b#r')).toBe('env:b_r') | ||
expectWarning() | ||
}) | ||
|
||
function expectWarning() { | ||
expect(displaySpy).toHaveBeenCalledOnceWith("env value doesn't meet tag requirements and will be sanitized") | ||
} | ||
}) |
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,36 @@ | ||
import { display } from '../../tools/display' | ||
import { InitConfiguration } from './configuration' | ||
|
||
export const TAG_SIZE_LIMIT = 200 | ||
|
||
export function buildTags(configuration: InitConfiguration): string[] { | ||
const { env, service, version } = configuration | ||
const tags = [] | ||
|
||
if (env) { | ||
tags.push(buildTag('env', env)) | ||
} | ||
if (service) { | ||
tags.push(buildTag('service', service)) | ||
} | ||
if (version) { | ||
tags.push(buildTag('version', version)) | ||
} | ||
|
||
return tags | ||
} | ||
|
||
export function buildTag(key: string, rawValue: string) { | ||
// See https://docs.datadoghq.com/getting_started/tagging/#defining-tags for tags syntax. | ||
const valueSizeLimit = TAG_SIZE_LIMIT - key.length - 1 | ||
const sanitizedValue = rawValue | ||
.toLowerCase() | ||
.slice(0, valueSizeLimit) | ||
.replace(/[^a-z0-9_:./-]/g, '_') | ||
|
||
if (sanitizedValue !== rawValue) { | ||
display.warn(`${key} value doesn't meet tag requirements and will be sanitized`) | ||
} | ||
|
||
return `${key}:${sanitizedValue}` | ||
} |
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
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