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

Commit aeb6c8a

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Update @opencensus/web-types with @opencensus/core (#75)
* Add option of passing in an object to the createChildSpan. * Add support for child span count. * Update types to add support for unregister stats exporter. * update CHANGELOG * Sync with @opencensus/core commit 36f63e6d. * Fix Root Span tests.
1 parent 8932b9a commit aeb6c8a

10 files changed

Lines changed: 108 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66

7+
- Add support for object(`SpanOptions`) as an argument for `startChildSpan` function, similar to `startRootSpan`.
8+
79
## 0.0.2 - 2019-04-29
810

911
Fix: add JS bundles and source maps to the NPM files for @opencensus/web-all

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class RootSpan extends Span implements webTypes.RootSpan {
2323
/** A list of child spans. */
2424
spans: Span[] = [];
2525

26+
/** A number of children. */
27+
private numberOfChildrenLocal: number;
28+
2629
constructor(
2730
/** Trace associated with this root span. */
2831
private readonly tracer: webTypes.Tracer,
@@ -44,17 +47,26 @@ export class RootSpan extends Span implements webTypes.RootSpan {
4447
} else {
4548
this.traceId = randomTraceId();
4649
}
50+
51+
this.numberOfChildrenLocal = 0;
52+
}
53+
54+
/** Gets the number of child span created for this span. */
55+
get numberOfChildren(): number {
56+
return this.numberOfChildrenLocal;
4757
}
4858

4959
/**
5060
* Starts a new child span in the root span.
51-
* @param nameOrOptions Span name string or object with `name` and `kind`
61+
* @param nameOrOptions Span name string or SpanOptions object.
5262
* @param kind Span kind if not using options object.
63+
* @param parentSpanId Span parent ID.
5364
*/
5465
startChildSpan(
55-
nameOrOptions?: string | { name: string; kind: webTypes.SpanKind },
66+
nameOrOptions?: string | webTypes.SpanOptions,
5667
kind?: webTypes.SpanKind
5768
): Span {
69+
this.numberOfChildrenLocal++;
5870
const child = new Span();
5971
child.traceId = this.traceId;
6072
child.traceState = this.traceState;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,17 @@ export class Span implements webTypes.Span {
187187
addMessageEvent(
188188
type: webTypes.MessageEventType,
189189
id: string,
190-
timestamp: number = performance.now()
190+
timestamp: number = performance.now(),
191+
uncompressedSize?: number,
192+
compressedSize?: number
191193
) {
192-
this.messageEvents.push({ type, id, timestamp });
194+
this.messageEvents.push({
195+
type,
196+
id,
197+
timestamp,
198+
uncompressedSize,
199+
compressedSize,
200+
});
193201
}
194202

195203
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,15 @@ export class Tracer implements webTypes.Tracer {
117117

118118
/**
119119
* Start a new Span instance to the currentRootSpan
120-
* @param name Span name
120+
* @param name Span name or SpanOptions object.
121121
* @param kind Span kind
122122
* @returns The new Span instance started
123123
*/
124-
startChildSpan(name?: string, kind?: webTypes.SpanKind): Span {
125-
return this.currentRootSpan.startChildSpan(name, kind);
124+
startChildSpan(
125+
nameOrOptions?: string | webTypes.SpanOptions,
126+
kind?: webTypes.SpanKind
127+
): Span {
128+
return this.currentRootSpan.startChildSpan(nameOrOptions, kind);
126129
}
127130

128131
/**

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { SpanKind } from '@opencensus/web-types';
17+
import { SpanKind, SpanOptions } from '@opencensus/web-types';
1818
import { RootSpan } from '../src/trace/model/root-span';
1919
import { Tracer } from '../src/trace/model/tracer';
2020

@@ -66,14 +66,15 @@ describe('RootSpan', () => {
6666
expect(root.spans).toEqual([childSpan]);
6767
});
6868

69-
it('allows specifying span options object with name and kind', () => {
69+
it('allows specifying SpanOptions object with name and kind', () => {
7070
root.traceId = '00000000000000000000000000000001';
7171
root.traceState = 'a=b';
7272

73-
const childSpan = root.startChildSpan({
73+
const spanOptions: SpanOptions = {
7474
name: 'child1',
7575
kind: SpanKind.CLIENT,
76-
});
76+
};
77+
const childSpan = root.startChildSpan(spanOptions);
7778

7879
expect(childSpan.traceId).toBe('00000000000000000000000000000001');
7980
expect(childSpan.traceState).toBe('a=b');
@@ -119,4 +120,19 @@ describe('RootSpan', () => {
119120
expect(tracer.onEndSpan).toHaveBeenCalledWith(root);
120121
});
121122
});
123+
124+
describe('get numberOfChildren()', () => {
125+
it('should get numberOfChildren from rootspan instance', () => {
126+
root = new RootSpan(tracer);
127+
root.start();
128+
expect(root.numberOfChildren).toBe(0);
129+
root.startChildSpan('spanName', SpanKind.UNSPECIFIED);
130+
expect(root.numberOfChildren).toBe(1);
131+
132+
for (let i = 0; i < 10; i++) {
133+
root.startChildSpan('spanName' + i, SpanKind.UNSPECIFIED);
134+
}
135+
expect(root.numberOfChildren).toBe(11);
136+
});
137+
});
122138
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ describe('Span', () => {
160160
type: MessageEventType.SENT,
161161
id: 'id22',
162162
timestamp: 25,
163+
uncompressedSize: undefined,
164+
compressedSize: undefined,
163165
},
164166
]);
165167
});
@@ -173,6 +175,8 @@ describe('Span', () => {
173175
type: MessageEventType.RECEIVED,
174176
id: 'id23',
175177
timestamp: 33,
178+
uncompressedSize: undefined,
179+
compressedSize: undefined,
176180
},
177181
]);
178182
});

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 'v0.0.9' && npm run fix",
10+
"copytypes": "node scripts/copy-types.js '36f63e6dee9e6ad9af5ece9c5458b47786d41f06' && npm run fix",
1111
"check": "gts check",
1212
"compile": "tsc -p .",
1313
"fix": "gts fix",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export interface StatsEventListener {
5656
* batched data to backend.
5757
*/
5858
start(): void;
59+
60+
/** Stops the exporter. */
61+
stop(): void;
5962
}
6063

6164
export type ExporterConfig = configTypes.BufferConfig;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ export interface Stats {
9696
* @param exporter An stats exporter
9797
*/
9898
registerExporter(exporter: StatsEventListener): void;
99+
100+
/**
101+
* Unregisters an exporter. It should be called whenever the exporter is not
102+
* needed anymore.
103+
* @param exporter An stats exporter
104+
*/
105+
unregisterExporter(exporter: StatsEventListener): void;
99106
}
100107

101108
/**

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
import * as loggerTypes from '../../common/types';
18-
import { NodeJsEventEmitter } from '../../node/types';
1918
import * as configTypes from '../config/types';
2019
import { Propagation } from '../propagation/types';
2120
import * as samplerTypes from '../sampler/types';
21+
import { NodeJsEventEmitter } from '../../node/types';
2222

2323
/** Default type for functions */
2424
// tslint:disable:no-any
@@ -191,7 +191,13 @@ export interface MessageEvent {
191191
timestamp: number;
192192
/** Indicates whether the message was sent or received. */
193193
type: MessageEventType;
194-
/** An identifier for the MessageEvent's message. */
194+
/**
195+
* An identifier for the MessageEvent's message. This should be a hexadecimal
196+
* value that fits within 64-bits. Message event ids should start with 1 for
197+
* both sent and received messages and increment by 1 for each message
198+
* sent/received. See:
199+
* https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/gRPC.md#message-events
200+
*/
195201
id: string;
196202
/** The number of uncompressed bytes sent or received. */
197203
uncompressedSize?: number;
@@ -225,6 +231,18 @@ export interface TraceOptions {
225231
spanContext?: SpanContext;
226232
/** Span kind */
227233
kind?: SpanKind;
234+
/** Determines the sampling rate. Ranges from 0.0 to 1.0 */
235+
samplingRate?: number;
236+
}
237+
238+
/** Defines the span options */
239+
export interface SpanOptions {
240+
/** Span name */
241+
name: string;
242+
/** Span kind */
243+
kind?: SpanKind;
244+
/** Span parent ID */
245+
parentSpanId?: string;
228246
}
229247

230248
export type TraceState = string;
@@ -417,8 +435,17 @@ export interface Span {
417435
* @param type The type of message event.
418436
* @param id An identifier for the message event.
419437
* @param timestamp A timestamp for this event.
420-
*/
421-
addMessageEvent(type: MessageEventType, id: string, timestamp?: number): void;
438+
* @param uncompressedSize The number of uncompressed bytes sent or received.
439+
* @param compressedSize The number of compressed bytes sent or received. If
440+
* zero or undefined, assumed to be the same size as uncompressed.
441+
*/
442+
addMessageEvent(
443+
type: MessageEventType,
444+
id: string,
445+
timestamp?: number,
446+
uncompressedSize?: number,
447+
compressedSize?: number
448+
): void;
422449

423450
/**
424451
* Sets a status to the span.
@@ -442,8 +469,13 @@ export interface RootSpan extends Span {
442469
/** Get the span list from RootSpan instance */
443470
readonly spans: Span[];
444471

472+
/** Gets the number of child span created for this span. */
473+
readonly numberOfChildren: number;
474+
445475
/** Starts a new Span instance in the RootSpan instance */
446-
startChildSpan(name: string, kind: SpanKind): Span;
476+
startChildSpan(name?: string, kind?: SpanKind): Span;
477+
startChildSpan(options?: SpanOptions): Span;
478+
startChildSpan(nameOrOptions?: string | SpanOptions, kind?: SpanKind): Span;
447479
}
448480

449481
/** Interface for Tracer */
@@ -505,11 +537,12 @@ export interface Tracer extends SpanEventListener {
505537
/**
506538
* Start a new Span instance to the currentRootSpan
507539
* @param name Span name
508-
* @param type Span type
509-
* @param parentSpanId Parent SpanId
540+
* @param kind Span kind
541+
* @param options Span Options
510542
* @returns The new Span instance started
511543
*/
512-
startChildSpan(name?: string, type?: SpanKind, parentSpanId?: string): Span;
544+
startChildSpan(name?: string, kind?: SpanKind): Span;
545+
startChildSpan(options?: SpanOptions): Span;
513546

514547
/**
515548
* Binds the trace context to the given function.

0 commit comments

Comments
 (0)