diff --git a/packages/opencensus-core/src/stats/view.ts b/packages/opencensus-core/src/stats/view.ts index 4720221e5..9d0bc6f6f 100644 --- a/packages/opencensus-core/src/stats/view.ts +++ b/packages/opencensus-core/src/stats/view.ts @@ -73,7 +73,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. @@ -151,15 +154,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 diff --git a/packages/opencensus-core/test/test-view.ts b/packages/opencensus-core/test/test-view.ts index f557f25b0..82570203e 100644 --- a/packages/opencensus-core/test/test-view.ts +++ b/packages/opencensus-core/test/test-view.ts @@ -192,6 +192,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()', () => {