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

Commit ee02283

Browse files
Peter Martonmayurkale22
authored andcommitted
feat(core): support object type attribute on spans (#496)
* feat(core): support object type attribute on spans * docs(span): document JSON.stringify-able span.addAttribute value * fix(opencensus-core): update no record span addAttribute params * fix(core): no record span addAttribute args
1 parent d9aac97 commit ee02283

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

packages/opencensus-core/src/trace/model/no-record/no-record-span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class NoRecordSpan implements types.Span {
149149
}
150150

151151
/** No-op implementation of this method. */
152-
addAttribute(key: string, value: string|number|boolean) {}
152+
addAttribute(key: string, value: string|number|boolean|object) {}
153153

154154
/** No-op implementation of this method. */
155155
addAnnotation(

packages/opencensus-core/src/trace/model/span.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ export class Span implements types.Span {
193193
/**
194194
* Adds an atribute to the span.
195195
* @param key Describes the value added.
196-
* @param value The result of an operation.
196+
* @param value The result of an operation. If the value is a typeof object
197+
* it has to be JSON.stringify-able, cannot contain circular dependencies.
197198
*/
198-
addAttribute(key: string, value: string|number|boolean) {
199+
addAttribute(key: string, value: string|number|boolean|object) {
199200
if (this.attributes[key]) {
200201
delete this.attributes[key];
201202
}
@@ -208,7 +209,9 @@ export class Span implements types.Span {
208209
delete this.attributes[attributeKeyToDelete];
209210
}
210211
}
211-
this.attributes[key] = value;
212+
const serializedValue =
213+
typeof value === 'object' ? JSON.stringify(value) : value;
214+
this.attributes[key] = serializedValue;
212215
}
213216

214217
/**

packages/opencensus-core/src/trace/model/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export interface Span {
406406
* @param key Describes the value added.
407407
* @param value The result of an operation.
408408
*/
409-
addAttribute(key: string, value: string|number|boolean): void;
409+
addAttribute(key: string, value: string|number|boolean|object): void;
410410

411411
/**
412412
* Adds an annotation to the span.

packages/opencensus-core/test/test-span.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ describe('Span', () => {
195195
assert.equal(
196196
span.attributes['testKey' + attType], 'testValue' + attType);
197197
});
198+
span.addAttribute('object', {foo: 'bar'});
199+
assert.equal(span.attributes['object'], '{"foo":"bar"}');
200+
span.addAttribute('array', [1, 2, 3]);
201+
assert.equal(span.attributes['array'], '[1,2,3]');
198202
});
199203

200204
it('should drop extra attributes', () => {

0 commit comments

Comments
 (0)