Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nflx-spectator",
"version": "3.0.17",
"version": "3.0.18",
"license": "Apache-2.0",
"homepage": "https://github.com/Netflix/spectator-js",
"author": "Netflix Telemetry Engineering <[email protected]>",
Expand Down
9 changes: 5 additions & 4 deletions src/common_tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export function tags_from_env_vars(): Record<string, string> {
export function validate_tags(tags: Record<string, string>): Record<string, string> {
const valid_tags: Record<string, string> = {};

for (const key in tags) {
const val = tags[key];
// javascript protection check
if (typeof key !== "string" || typeof val !== "string") continue;
for (let key in tags) {
let val = tags[key];
// javascript protection checks
if (typeof key !== "string") key = String(key);
if (typeof val !== "string") val = String(val);
if (key.length < 2 || key.length > 60 || val.length < 1 || val.length > 120) continue;
valid_tags[key] = val;
}
Expand Down
12 changes: 8 additions & 4 deletions src/meter/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ export class Id {
}

private to_spectatord_id(name: string, tags?: Tags): string {
// javascript and length protection check
if (typeof name !== "string" || name.length < 2 || name.length > 255) {
this._logger.warn(`Id(name=${name}, tags=${tags_toString(tags)}) is invalid, because the name is not a string, ` +
`it is too short (< 2), or it is too long (> 255); metric will not be reported`);
// javascript protection check
if (typeof name !== "string") {
name = String(name);
}

if (name.length < 2 || name.length > 255) {
this._logger.warn(`Id(name=${name}, tags=${tags_toString(tags)}) is invalid, because the name ` +
`is too short (< 2), or it is too long (> 255); metric will not be reported`);
this.invalid = true;
return '';
}
Expand Down
42 changes: 22 additions & 20 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export class Registry {

private _config: Config;
private readonly _writer: WriterUnion;
private readonly _noop_writer: NoopWriter;

constructor(config: Config = new Config()) {
this._config = config;
this.logger = config.logger;
this._writer = new_writer(this._config.location, this.logger);
this._noop_writer = new NoopWriter();
this.logger.debug(`Create Registry with extra_common_tags=${tags_toString(this._config.extra_common_tags)}`);
}

Expand Down Expand Up @@ -71,111 +73,111 @@ export class Registry {

age_gauge(name: string, tags: Tags = {}): AgeGauge {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new AgeGauge(id, writer);
}

age_gauge_with_id(id: Id): AgeGauge {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new AgeGauge(id, writer);
}

counter(name: string, tags: Tags = {}): Counter {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Counter(id, writer);
}

counter_with_id(id: Id): Counter {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Counter(id, writer);
}

distribution_summary(name: string, tags: Tags = {}): DistributionSummary {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new DistributionSummary(id, writer);
}

distribution_summary_with_id(id: Id): DistributionSummary {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new DistributionSummary(id, writer);
}

gauge(name: string, tags: Tags = {}, ttl_seconds?: number): Gauge {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Gauge(id, writer, ttl_seconds);
}

gauge_with_id(id: Id, ttl_seconds?: number): Gauge {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Gauge(id, writer, ttl_seconds);
}

max_gauge(name: string, tags: Tags = {}): MaxGauge {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MaxGauge(id, writer);
}

max_gauge_with_id(id: Id): MaxGauge {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MaxGauge(id, writer);
}

monotonic_counter(name: string, tags: Tags = {}): MonotonicCounter {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MonotonicCounter(id, writer);
}

monotonic_counter_with_id(id: Id): MonotonicCounter {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MonotonicCounter(id, writer);
}

monotonic_counter_uint(name: string, tags: Tags = {}): MonotonicCounterUint {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MonotonicCounterUint(id, writer);
}

monotonic_counter_uint_with_id(id: Id): MonotonicCounterUint {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new MonotonicCounterUint(id, writer);
}

pct_distribution_summary(name: string, tags: Tags = {}): PercentileDistributionSummary {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new PercentileDistributionSummary(id, writer);
}

pct_distribution_summary_with_id(id: Id): PercentileDistributionSummary {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new PercentileDistributionSummary(id, writer);
}

pct_timer(name: string, tags: Tags = {}): PercentileTimer {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new PercentileTimer(id, writer);
}

pct_timer_with_id(id: Id): PercentileTimer {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new PercentileTimer(id, writer);
}

timer(name: string, tags: Tags = {}): Timer {
const id = this.new_id(name, tags);
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Timer(id, writer);
}

timer_with_id(id: Id): Timer {
const writer = id.invalid ? new NoopWriter() : this._writer;
const writer = id.invalid ? this._noop_writer : this._writer;
return new Timer(id, writer);
}
}
6 changes: 3 additions & 3 deletions test/meter/id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ describe("Id Tests", (): void => {
// name too long
const long = "a".repeat(256);
const id2 = new Id(long);
assert.equal(id2.toString(),"Id(name=" + long +", tags={})" );
assert.equal(id2.toString(),"Id(name=" + long + ", tags={})" );
assert.isTrue(id2.invalid);
assert.equal(id2.spectatord_id, "");

const expected: string[] = [
"WARN: Id(name=a, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=" + long + ", tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=a, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=" + long + ", tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];

assert.deepEqual(messages, expected);
Expand Down
40 changes: 20 additions & 20 deletions test/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -142,8 +142,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -198,8 +198,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=d, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -248,8 +248,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -320,8 +320,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=g, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -370,8 +370,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -420,8 +420,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=c, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -480,8 +480,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=d, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=d, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -530,8 +530,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=t, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down Expand Up @@ -580,8 +580,8 @@ describe("Registry Tests", (): void => {
assert.isTrue(writer.is_empty());

const expected: string[] = [
"WARN: Id(name=t, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is not a string, it is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
"WARN: Id(name=t, tags={}) is invalid, because the name is too short (< 2), or it is too long (> 255); metric will not be reported",
];
assert.deepEqual(messages, expected);
console.log = f;
Expand Down