Skip to content

Commit

Permalink
Merge branch 'master' into add-express-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Feb 14, 2020
2 parents fe66b6f + 274b05f commit 39b5f27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/opentelemetry-core/src/internal/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
* limitations under the License.
*/

const VALID_KEY_REGEX = /^[a-z][_0-9a-z-*/]{0,255}$/;
const VALID_KEY_CHAR_RANGE = '[_0-9a-z-*/]';
const VALID_KEY = `[a-z]${VALID_KEY_CHAR_RANGE}{0,255}`;
const VALID_VENDOR_KEY = `[a-z0-9]${VALID_KEY_CHAR_RANGE}{0,240}@[a-z]${VALID_KEY_CHAR_RANGE}{0,13}`;
const VALID_KEY_REGEX = new RegExp(`^(?:${VALID_KEY}|${VALID_VENDOR_KEY})$`);
const VALID_VALUE_BASE_REGEX = /^[ -~]{0,255}[!-~]$/;
const INVALID_VALUE_COMMA_EQUAL_REGEX = /,|=/;

/**
* Key is opaque string up to 256 characters printable. It MUST begin with a
* lowercase letter, and can only contain lowercase letters a-z, digits 0-9,
* underscores _, dashes -, asterisks *, and forward slashes /.
* For multi-tenant vendor scenarios, an at sign (@) can be used to prefix the
* vendor name. Vendors SHOULD set the tenant ID at the beginning of the key.
* see https://www.w3.org/TR/trace-context/#key
*/
export function validateKey(key: string): boolean {
return VALID_KEY_REGEX.test(key);
Expand Down
7 changes: 5 additions & 2 deletions packages/opentelemetry-core/test/internal/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ describe('validators', () => {
'baz*bar',
'baz/',
'tracestate',
'fw529a3039@dt',
'6cab5bb-29a@dt',
];
validKeysTestCases.forEach(testCase =>
it(`returns true when key contains valid chars ${testCase}`, () => {
assert.ok(validateKey(testCase));
assert.ok(validateKey(testCase), `${testCase} should be valid`);
})
);

Expand All @@ -42,10 +44,11 @@ describe('validators', () => {
'TrAcEsTaTE',
'TRACESTATE',
'',
'6num',
];
invalidKeysTestCases.forEach(testCase =>
it(`returns true when key contains invalid chars ${testCase}`, () => {
assert.ok(!validateKey(testCase));
assert.ok(!validateKey(testCase), `${testCase} should be invalid`);
})
);
});
Expand Down

0 comments on commit 39b5f27

Please sign in to comment.