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

Commit

Permalink
feat(core): support object type attribute on spans (#496)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Peter Marton authored and mayurkale22 committed May 6, 2019
1 parent d9aac97 commit ee02283
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class NoRecordSpan implements types.Span {
}

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

/** No-op implementation of this method. */
addAnnotation(
Expand Down
9 changes: 6 additions & 3 deletions packages/opencensus-core/src/trace/model/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ export class Span implements types.Span {
/**
* Adds an atribute to the span.
* @param key Describes the value added.
* @param value The result of an operation.
* @param value The result of an operation. If the value is a typeof object
* it has to be JSON.stringify-able, cannot contain circular dependencies.
*/
addAttribute(key: string, value: string|number|boolean) {
addAttribute(key: string, value: string|number|boolean|object) {
if (this.attributes[key]) {
delete this.attributes[key];
}
Expand All @@ -208,7 +209,9 @@ export class Span implements types.Span {
delete this.attributes[attributeKeyToDelete];
}
}
this.attributes[key] = value;
const serializedValue =
typeof value === 'object' ? JSON.stringify(value) : value;
this.attributes[key] = serializedValue;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/opencensus-core/src/trace/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export interface Span {
* @param key Describes the value added.
* @param value The result of an operation.
*/
addAttribute(key: string, value: string|number|boolean): void;
addAttribute(key: string, value: string|number|boolean|object): void;

/**
* Adds an annotation to the span.
Expand Down
4 changes: 4 additions & 0 deletions packages/opencensus-core/test/test-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ describe('Span', () => {
assert.equal(
span.attributes['testKey' + attType], 'testValue' + attType);
});
span.addAttribute('object', {foo: 'bar'});
assert.equal(span.attributes['object'], '{"foo":"bar"}');
span.addAttribute('array', [1, 2, 3]);
assert.equal(span.attributes['array'], '[1,2,3]');
});

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

0 comments on commit ee02283

Please sign in to comment.