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

Add validation for tagkey & tagvalue. Fix issue #268 #280

Merged
merged 6 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 22 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,32 @@ 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 {
return this.invalidPrintableCharacters(tags) || this.invalidLength(tags);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should log the warn or error, in case of invalid tags. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about modifying the method like this:

private invalidTags(tags: Tags): boolean {
  const result: boolean =  this.invalidPrintableCharacters(tags) || this.invalidLength(tags);
  if(result) {
    console.log('Unable to create tagkey/tagvalue with the specified tags. Check Tag specifications here: https://github.com/census-instrumentation/opencensus-specs/blob/master/tags/TagMap.md');
  }
  return result;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/console.log/this.logger.warn and no need to add specs link.

}

/**
* 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
25 changes: 25 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,31 @@ 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 =
'NHHcUMQtGf7kFjSvy86aZZMIp5zx1aoRH4FFjOJnU4AvwVmAD5GPcmQmwnP6NuWx5NmHdts3xgqyAjn57i9Yc5mak22duPlf7JehY3bMcjbacocAEC1TepDCEt3ihzSOuI9mRvKL4vop7AZ3Uahge6OL3ogIJOhulRlIkK2qeT2avh8FeoGcnNV3O6yKHNovvUoUf01vxnwhG3ruPTh6j2E8G51q1tGAbSUd0UE0Sf2KceRQTr28GOp8zGlIj6lpqJXg';
vigneshtdev marked this conversation as resolved.
Show resolved Hide resolved
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