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

Commit f1983b9

Browse files
vigneshtdevmayurkale22
authored andcommitted
Add validation for tagkey & tagvalue. Fix issue #268 (#280)
* 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
1 parent 3b76356 commit f1983b9

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

packages/opencensus-core/src/stats/view.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export class BaseView implements View {
7474
/** An object to log information to */
7575
// @ts-ignore
7676
private logger: loggerTypes.Logger;
77-
77+
/**
78+
* Max Length of a TagKey
79+
*/
80+
private readonly MAX_LENGTH: number = 256;
7881
/**
7982
* Creates a new View instance. This constructor is used by Stats. User should
8083
* prefer using Stats.createView() instead.
@@ -152,15 +155,38 @@ export class BaseView implements View {
152155
}
153156

154157
/**
155-
* Checks if tag keys and values have only printable characters.
158+
* Checks if tag keys and values are valid.
156159
* @param tags The tags to be checked
157160
*/
158161
private invalidTags(tags: Tags): boolean {
162+
const result: boolean =
163+
this.invalidPrintableCharacters(tags) || this.invalidLength(tags);
164+
if (result) {
165+
this.logger.warn(
166+
'Unable to create tagkey/tagvalue with the specified tags.');
167+
}
168+
return result;
169+
}
170+
171+
/**
172+
* Checks if tag keys and values have only printable characters.
173+
* @param tags The tags to be checked
174+
*/
175+
private invalidPrintableCharacters(tags: Tags): boolean {
159176
return Object.keys(tags).some(tagKey => {
160177
return invalidString.test(tagKey) || invalidString.test(tags[tagKey]);
161178
});
162179
}
163180

181+
/**
182+
* Checks if length of tagkey is greater than 0 & less than 256.
183+
* @param tags The tags to be checked
184+
*/
185+
private invalidLength(tags: Tags): boolean {
186+
return Object.keys(tags).some(tagKey => {
187+
return tagKey.length <= 0 || tagKey.length >= this.MAX_LENGTH;
188+
});
189+
}
164190
/**
165191
* Creates an empty aggregation data for a given tags.
166192
* @param tags The tags for that aggregation data

packages/opencensus-core/test/test-view.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ describe('BaseView', () => {
193193
view.recordMeasurement(measurement);
194194
assert.ok(!view.getSnapshot(measurement.tags));
195195
});
196+
197+
it('should not record a measurement when a tag key is longer than 255 characters',
198+
() => {
199+
const tagkey = 'a'.repeat(256);
200+
const measurement = {
201+
measure,
202+
tags: {[tagkey]: 'testValue'},
203+
value: 10
204+
};
205+
view.recordMeasurement(measurement);
206+
assert.ok(!view.getSnapshot(measurement.tags));
207+
});
208+
209+
it('should not record a measurement when tag key is 0 character long',
210+
() => {
211+
const tagkey = '';
212+
const measurement = {
213+
measure,
214+
tags: {[tagkey]: 'testValue'},
215+
value: 10
216+
};
217+
view.recordMeasurement(measurement);
218+
assert.ok(!view.getSnapshot(measurement.tags));
219+
});
196220
});
197221

198222
describe('getMetric()', () => {

0 commit comments

Comments
 (0)