Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Add validation for tagkey & tagvalue. Fix issue #268 (#280)
Browse files Browse the repository at this point in the history
* Add validation for tagkey & tagvalue. Fix issue #268

* Fix formatting errors

* Log warning to screen when invalid tag has been used

* Remove unnecessary import

* Fix formatting error

* Replace long string in test case
  • Loading branch information
vigneshtdev authored and mayurkale22 committed Jan 16, 2019
1 parent 3b76356 commit f1983b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/opencensus-core/src/stats/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export class BaseView implements View {
/** An object to log information to */
// @ts-ignore
private logger: loggerTypes.Logger;

/**
* Max Length of a TagKey
*/
private readonly MAX_LENGTH: number = 256;
/**
* Creates a new View instance. This constructor is used by Stats. User should
* prefer using Stats.createView() instead.
Expand Down Expand Up @@ -152,15 +155,38 @@ export class BaseView implements View {
}

/**
* Checks if tag keys and values have only printable characters.
* Checks if tag keys and values are valid.
* @param tags The tags to be checked
*/
private invalidTags(tags: Tags): boolean {
const result: boolean =
this.invalidPrintableCharacters(tags) || this.invalidLength(tags);
if (result) {
this.logger.warn(
'Unable to create tagkey/tagvalue with the specified tags.');
}
return result;
}

/**
* Checks if tag keys and values have only printable characters.
* @param tags The tags to be checked
*/
private invalidPrintableCharacters(tags: Tags): boolean {
return Object.keys(tags).some(tagKey => {
return invalidString.test(tagKey) || invalidString.test(tags[tagKey]);
});
}

/**
* Checks if length of tagkey is greater than 0 & less than 256.
* @param tags The tags to be checked
*/
private invalidLength(tags: Tags): boolean {
return Object.keys(tags).some(tagKey => {
return tagKey.length <= 0 || tagKey.length >= this.MAX_LENGTH;
});
}
/**
* Creates an empty aggregation data for a given tags.
* @param tags The tags for that aggregation data
Expand Down
24 changes: 24 additions & 0 deletions packages/opencensus-core/test/test-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ describe('BaseView', () => {
view.recordMeasurement(measurement);
assert.ok(!view.getSnapshot(measurement.tags));
});

it('should not record a measurement when a tag key is longer than 255 characters',
() => {
const tagkey = 'a'.repeat(256);
const measurement = {
measure,
tags: {[tagkey]: 'testValue'},
value: 10
};
view.recordMeasurement(measurement);
assert.ok(!view.getSnapshot(measurement.tags));
});

it('should not record a measurement when tag key is 0 character long',
() => {
const tagkey = '';
const measurement = {
measure,
tags: {[tagkey]: 'testValue'},
value: 10
};
view.recordMeasurement(measurement);
assert.ok(!view.getSnapshot(measurement.tags));
});
});

describe('getMetric()', () => {
Expand Down

0 comments on commit f1983b9

Please sign in to comment.