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

Commit

Permalink
feat: add tag format verification
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoemery committed Aug 9, 2018
1 parent b6f0384 commit 923a9f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
42 changes: 37 additions & 5 deletions packages/opencensus-core/src/stats/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
import {Recorder} from './recorder';
import {AggregationData, AggregationMetadata, AggregationType, Bucket, CountData, DistributionData, LastValueData, Measure, Measurement, MeasureType, SumData, Tags, View} from './types';

const RECORD_SEPARATOR = String.fromCharCode(30);
const UNIT_SEPARATOR = String.fromCharCode(31);
const LOW_LIMIT_CHAR = String.fromCharCode(0);
const HIGH_LIMIT_CHAR = String.fromCharCode(31);
const DELL_CHAR = String.fromCharCode(127);

export class BaseView implements View {
/**
* A string by which the View will be referred to, e.g. "rpc_latency". Names
Expand Down Expand Up @@ -93,7 +99,12 @@ export class BaseView implements View {
* @param measurement The measurement to record
*/
recordMeasurement(measurement: Measurement) {
// Checks if measurements has all tags in views
// Checks if measurement has valid tags
if (this.invalidTags(measurement.tags)) {
return;
}

// Checks if measurement has all tags in views
for (const tagKey of this.columns) {
if (!Object.keys(measurement.tags).some((key) => key === tagKey)) {
return;
Expand All @@ -112,10 +123,31 @@ export class BaseView implements View {
* @param tags The tags to encode
*/
private encodeTags(tags: Tags): string {
const encodedTags = Object.keys(tags).sort().reduce((strTags, tagKey) => {
return strTags + `${tagKey}:${tags[tagKey]},`;
}, '');
return `{${encodedTags}}`;
return Object.keys(tags)
.sort()
.map(tagKey => {
return tagKey + UNIT_SEPARATOR + tags[tagKey];
})
.join(RECORD_SEPARATOR);
}

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

/**
* Checks if a string has only printable characters.
* @param str The string to be checked
*/
private invalidString(str: string): boolean {
return RegExp(`[${LOW_LIMIT_CHAR}-${HIGH_LIMIT_CHAR}]`).test(str) ||
RegExp(DELL_CHAR).test(str);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/opencensus-core/test/test-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('BaseView', () => {
});
});

describe('record()', () => {
describe('recordMeasurement()', () => {
const measurementValues = [1.1, -2.3, 3.2, -4.3, 5.2];
const bucketBoundaries = [0, 2, 4, 6];
const emptyAggregation = {};
Expand Down Expand Up @@ -160,6 +160,16 @@ describe('BaseView', () => {
assert.ok(!view.getSnapshot(measurement.tags));
});

it('should not record a measurement when tags are not valid', () => {
const measurement = {
measure,
tags: {testKey3: String.fromCharCode(30) + 'testValue'},
value: 10
};
view.recordMeasurement(measurement);
assert.ok(!view.getSnapshot(measurement.tags));
});

it('should not record a measurement when it has not enough tag keys',
() => {
const measurement = {
Expand Down

0 comments on commit 923a9f2

Please sign in to comment.