Skip to content

Commit

Permalink
chore: disallow null attribute values (#1555)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Sep 28, 2020
1 parent 7831bd6 commit e298eec
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 13 deletions.
4 changes: 1 addition & 3 deletions packages/opentelemetry-api/src/trace/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/

export interface Attributes {
[attributeKey: string]: AttributeValue;
[attributeKey: string]: AttributeValue | undefined;
}

export type AttributeValue =
| undefined
| null
| string
| number
| boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Span {
* @param key the key for this attribute.
* @param value the value for this attribute.
*/
setAttribute(key: string, value: AttributeValue): this;
setAttribute(key: string, value?: AttributeValue): this;

/**
* Sets attributes to the span.
Expand Down
9 changes: 2 additions & 7 deletions packages/opentelemetry-tracing/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export class Span implements api.Span, ReadableSpan {
return this.spanContext;
}

setAttribute(key: string, value: AttributeValue): this;
setAttribute(key: string, value?: AttributeValue): this;
setAttribute(key: string, value: unknown): this {
if (this._isSpanEnded()) return this;
if (value == null || this._isSpanEnded()) return this;
if (key.length === 0) {
this._logger.warn(`Invalid attribute key: ${key}`);
return this;
Expand All @@ -100,11 +100,6 @@ export class Span implements api.Span, ReadableSpan {
return this;
}

if (value == null) {
delete this.attributes[key];
return this;
}

if (
Object.keys(this.attributes).length >=
this._traceParams.numberOfAttributesPerSpan!
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-tracing/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ describe('Span', () => {
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);

span.setAttribute('overwrite', 'initial value');
span.setAttribute('remove', 'initial value');
span.setAttribute('overwrite', 'overwritten value');
span.setAttribute('remove', null);

assert.deepStrictEqual(span.attributes, {
overwrite: 'overwritten value',
Expand Down

0 comments on commit e298eec

Please sign in to comment.