Skip to content

Commit

Permalink
feat: enforce TraceParams Limits (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Sep 19, 2019
1 parent 03cbd00 commit c8e9ddc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/opentelemetry-basic-tracer/src/Span.ts
Expand Up @@ -19,6 +19,7 @@ import { hrTime, hrTimeDuration, timeInputToHrTime } from '@opentelemetry/core';
import { ReadableSpan } from './export/ReadableSpan';
import { BasicTracer } from './BasicTracer';
import { SpanProcessor } from './SpanProcessor';
import { TraceParams } from './types';

/**
* This class represents a span.
Expand All @@ -43,6 +44,7 @@ export class Span implements types.Span, ReadableSpan {
private _duration: types.HrTime = [-1, -1];
private readonly _logger: types.Logger;
private readonly _spanProcessor: SpanProcessor;
private readonly _traceParams: TraceParams;

/** Constructs a new Span instance. */
constructor(
Expand All @@ -60,6 +62,7 @@ export class Span implements types.Span, ReadableSpan {
this.kind = kind;
this.startTime = timeInputToHrTime(startTime);
this._logger = parentTracer.logger;
this._traceParams = parentTracer.getActiveTraceParams();
this._spanProcessor = parentTracer.activeSpanProcessor;
this._spanProcessor.onStart(this);
}
Expand All @@ -74,6 +77,19 @@ export class Span implements types.Span, ReadableSpan {

setAttribute(key: string, value: unknown): this {
if (this._isSpanEnded()) return this;

if (
Object.keys(this.attributes).length >=
this._traceParams.numberOfAttributesPerSpan!
) {
const attributeKeyToDelete = Object.keys(this.attributes).shift();
if (attributeKeyToDelete) {
this._logger.warn(
`Dropping extra attributes : ${attributeKeyToDelete}`
);
delete this.attributes[attributeKeyToDelete];
}
}
this.attributes[key] = value;
return this;
}
Expand All @@ -87,16 +103,21 @@ export class Span implements types.Span, ReadableSpan {

addEvent(name: string, attributes?: types.Attributes): this {
if (this._isSpanEnded()) return this;
this.events.push({
name,
attributes,
time: hrTime(),
});
if (this.events.length >= this._traceParams.numberOfEventsPerSpan!) {
this._logger.warn('Dropping extra events.');
this.events.shift();
}
this.events.push({ name, attributes, time: hrTime() });
return this;
}

addLink(spanContext: types.SpanContext, attributes?: types.Attributes): this {
if (this._isSpanEnded()) return this;

if (this.links.length >= this._traceParams.numberOfLinksPerSpan!) {
this._logger.warn('Dropping extra links.');
this.links.shift();
}
this.links.push({ spanContext, attributes });
return this;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Expand Up @@ -127,6 +127,23 @@ describe('Span', () => {
span.end();
});

it('should drop extra links, attributes and events', () => {
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
for (let i = 0; i < 150; i++) {
span.addLink(spanContext);
span.setAttribute('foo' + i, 'bar' + i);
span.addEvent('sent' + i);
}
span.end();

assert.strictEqual(span.links.length, 32);
assert.strictEqual(span.events.length, 128);
assert.strictEqual(Object.keys(span.attributes).length, 32);
assert.strictEqual(span.events[span.events.length - 1].name, 'sent149');
assert.strictEqual(span.attributes['foo0'], undefined);
assert.strictEqual(span.attributes['foo149'], 'bar149');
});

it('should set an error status', () => {
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
span.setStatus({
Expand Down

0 comments on commit c8e9ddc

Please sign in to comment.