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

Commit 74ada26

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Sync @opencensus/web-types to @opencensus/core v0.0.12 (#80)
1 parent b1a7b09 commit 74ada26

7 files changed

Lines changed: 40 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,13 @@ export class Span implements webTypes.Span {
192192
/**
193193
* Adds an attribute to the span.
194194
* @param key Describes the value added.
195-
* @param value What value to set for the attribute.
195+
* @param value What value to set for the attribute. If the value is a typeof object
196+
* it has to be JSON.stringify-able, cannot contain circular dependencies.
196197
*/
197-
addAttribute(key: string, value: string | number | boolean) {
198-
this.attributes[key] = value;
198+
addAttribute(key: string, value: string | number | boolean | object) {
199+
const serializedValue =
200+
typeof value === 'object' ? JSON.stringify(value) : value;
201+
this.attributes[key] = serializedValue;
199202
}
200203

201204
/**

packages/opencensus-web-core/src/trace/model/tracer-base.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export class TracerBase implements webTypes.TracerBase {
5252
*/
5353
readonly activeTraceParams = {};
5454

55+
/** A configuration for starting the tracer */
56+
private config: webTypes.TracerConfig = {};
57+
5558
/**
5659
* Starts the tracer. This makes the tracer active and sets `logger` and
5760
* `propagation` based on the given config. The `samplingRate` property of
@@ -60,6 +63,7 @@ export class TracerBase implements webTypes.TracerBase {
6063
start(config: webTypes.TracerConfig): this {
6164
this.logger = config.logger || console;
6265
this.propagation = config.propagation || NO_HEADERS_PROPAGATION;
66+
this.config = config;
6367
return this;
6468
}
6569

@@ -76,6 +80,13 @@ export class TracerBase implements webTypes.TracerBase {
7680
*/
7781
startRootSpan<T>(options: webTypes.TraceOptions, fn: (root: Span) => T): T {
7882
const rootSpan = new RootSpan(this, options);
83+
// Add default attributes
84+
const defaultAttributes = this.config && this.config.defaultAttributes;
85+
if (defaultAttributes) {
86+
for (const key of Object.keys(defaultAttributes)) {
87+
rootSpan.addAttribute(key, defaultAttributes[key]);
88+
}
89+
}
7990
rootSpan.start();
8091
return fn(rootSpan);
8192
}
@@ -108,10 +119,18 @@ export class TracerBase implements webTypes.TracerBase {
108119
* @returns The new Span instance started
109120
*/
110121
startChildSpan(options?: webTypes.SpanOptions): Span {
111-
let span = new Span();
122+
let rootSpan = new Span();
112123
if (options && options.childOf) {
113-
span = options.childOf as Span;
124+
rootSpan = options.childOf as Span;
125+
}
126+
const span = rootSpan.startChildSpan(options);
127+
// Add default attributes
128+
const defaultAttributes = this.config && this.config.defaultAttributes;
129+
if (defaultAttributes) {
130+
for (const key of Object.keys(defaultAttributes)) {
131+
rootSpan.addAttribute(key, defaultAttributes[key]);
132+
}
114133
}
115-
return span.startChildSpan(options);
134+
return span;
116135
}
117136
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ describe('Span', () => {
103103

104104
it('sets attribute when addAttribute called', () => {
105105
span.addAttribute('attr1', 23);
106-
107106
expect(span.attributes).toEqual({ attr1: 23 });
107+
108+
span.addAttribute('object', { foo: 'bar' });
109+
expect(span.attributes['object']).toEqual('{"foo":"bar"}');
110+
111+
span.addAttribute('array', [1, 2, 3]);
112+
expect(span.attributes['array']).toEqual('[1,2,3]');
108113
});
109114

110115
it('adds link when addLink called', () => {

packages/opencensus-web-types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"build": "npm run compile",
99
"clean": "rimraf build/*",
10-
"copytypes": "node scripts/copy-types.js '92edaa9d406ac74e1805a4e80b756acedc413001' && npm run fix",
10+
"copytypes": "node scripts/copy-types.js 'v0.0.12' && npm run fix",
1111
"check": "gts check",
1212
"compile": "tsc -p .",
1313
"fix": "gts fix",

packages/opencensus-web-types/src/stats/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export interface Stats {
9898

9999
/**
100100
* Gets a collection of produced Metric`s to be exported.
101-
* @returns {Metric[]} List of metrics
101+
* @returns The List of metrics.
102102
*/
103103
getMetrics(): Metric[];
104104

packages/opencensus-web-types/src/trace/config/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Logger } from '../../common/types';
1818
import { Exporter } from '../../exporters/types';
1919
import { Stats } from '../../stats/types';
2020
import { PluginNames } from '../instrumentation/types';
21+
import { Attributes } from '../model/types';
2122
import { Propagation } from '../propagation/types';
2223

2324
/** Interface configuration for a buffer. */
@@ -32,6 +33,8 @@ export interface BufferConfig {
3233

3334
/** Defines tracer configuration parameters */
3435
export interface TracerConfig {
36+
/** A set of default attributes each in the format [KEY]:[VALUE] */
37+
defaultAttributes?: Attributes;
3538
/** Determines the sampling rate. Ranges from 0.0 to 1.0 */
3639
samplingRate?: number;
3740
/** A logger object */

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

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

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

0 commit comments

Comments
 (0)