Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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.
Expand Down Expand Up @@ -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
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 @@ -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()', () => {
Expand Down