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

Commit 95be16e

Browse files
authored
Use enum for MessageEvent Type, Link Type and SpanKind (#334)
* Use enum for MessageEvent Type, Link Type and SpanKind * fix build
1 parent a692467 commit 95be16e

File tree

21 files changed

+172
-110
lines changed

21 files changed

+172
-110
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
1919
- The ```new Stats()``` has been deprecated on Stats class. The global singleton ```globalStats``` object should be used instead. Also, ```registerView()``` is separated out from ```createView()```.
2020
- Use ```TagKey```, ```TagValue``` and ```TagMap``` to create the tag keys, tag values.
2121
- The `status` field on `Span` is no longer a number, use `CanonicalCode` instead.
22+
- Add enum type for `MessageEvent`, `Link` and `SpanKind`, instead of string.
2223

2324
##### Old code
2425
```js

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export class RootSpan extends SpanBase implements types.RootSpan {
5252
this.traceStateLocal = context.spanContext.traceState;
5353
}
5454
this.spansLocal = [];
55-
this.kind = context && context.kind ? context.kind : null;
55+
this.kind =
56+
context && context.kind ? context.kind : types.SpanKind.UNSPECIFIED;
5657
this.logger = tracer.logger || logger.logger();
5758
this.activeTraceParams = tracer.activeTraceParams;
5859
}
@@ -105,7 +106,7 @@ export class RootSpan extends SpanBase implements types.RootSpan {
105106
* @param kind Span kind.
106107
* @param parentSpanId Span parent ID.
107108
*/
108-
startChildSpan(name: string, kind: string, parentSpanId?: string):
109+
startChildSpan(name: string, kind: types.SpanKind, parentSpanId?: string):
109110
types.Span {
110111
if (this.ended) {
111112
this.logger.debug(

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export abstract class SpanBase implements types.Span {
5454
/** The resource name of the span */
5555
name: string = null;
5656
/** Kind of span. */
57-
kind: string = null;
57+
kind: types.SpanKind = types.SpanKind.UNSPECIFIED;
5858
/** A final status for this span */
5959
status: types.Status = STATUS_OK;
6060
/** set isRootSpan */
@@ -189,7 +189,7 @@ export abstract class SpanBase implements types.Span {
189189
* @param attributes A set of attributes on the link.
190190
*/
191191
addLink(
192-
traceId: string, spanId: string, type: string,
192+
traceId: string, spanId: string, type: types.LinkType,
193193
attributes?: types.Attributes) {
194194
if (this.links.length >= this.activeTraceParams.numberOfLinksPerSpan) {
195195
this.links.shift();
@@ -210,12 +210,13 @@ export abstract class SpanBase implements types.Span {
210210
* @param id An identifier for the message event.
211211
* @param timestamp A time in milliseconds. Defaults to Date.now()
212212
*/
213-
addMessageEvent(type: string, id: string, timestamp = 0) {
213+
addMessageEvent(type: types.MessageEventType, id: string, timestamp = 0) {
214214
if (this.messageEvents.length >=
215215
this.activeTraceParams.numberOfMessageEventsPerSpan) {
216216
this.messageEvents.shift();
217217
this.droppedMessageEventsCount++;
218218
}
219+
219220
this.messageEvents.push({
220221
'type': type,
221222
'id': id,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,16 @@ export class CoreTracer implements types.Tracer {
236236
/**
237237
* Starts a span.
238238
* @param name The span name.
239-
* @param type The span type.
239+
* @param kind optional The span kind.
240240
* @param parentSpanId The parent span ID.
241241
*/
242-
startChildSpan(name?: string, type?: string): types.Span {
242+
startChildSpan(name?: string, kind?: types.SpanKind): types.Span {
243243
let newSpan: types.Span = null;
244244
if (!this.currentRootSpan) {
245245
this.logger.debug(
246246
'no current trace found - must start a new root span first');
247247
} else {
248-
newSpan = this.currentRootSpan.startChildSpan(name, type);
248+
newSpan = this.currentRootSpan.startChildSpan(name, kind);
249249
}
250250
return newSpan;
251251
}

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export interface MessageEvent {
191191
/** A timestamp for the event. */
192192
timestamp: number;
193193
/** Indicates whether the message was sent or received. */
194-
type: string;
194+
type: MessageEventType;
195195
/** An identifier for the MessageEvent's message. */
196196
id: string;
197197
/** The number of uncompressed bytes sent or received. */
@@ -213,7 +213,7 @@ export interface Link {
213213
/** The span ID for a span within a trace. */
214214
spanId: string;
215215
/** The relationship of the current span relative to the linked. */
216-
type: string;
216+
type: LinkType;
217217
/** A set of attributes on the link. */
218218
attributes: Attributes;
219219
}
@@ -225,7 +225,7 @@ export interface TraceOptions {
225225
/** Trace context */
226226
spanContext?: SpanContext;
227227
/** Span kind */
228-
kind?: string;
228+
kind?: SpanKind;
229229
}
230230

231231
export type TraceState = string;
@@ -249,6 +249,51 @@ export interface SpanEventListener {
249249
onEndSpan(span: RootSpan): void;
250250
}
251251

252+
/** An event describing a message sent/received between Spans. */
253+
export enum MessageEventType {
254+
/** Unknown event type. */
255+
UNSPECIFIED = 0,
256+
/** Indicates a sent message. */
257+
SENT = 1,
258+
/** Indicates a received message. */
259+
RECEIVED = 2
260+
}
261+
262+
/**
263+
* Type of span. Can be used to specify additional relationships between spans
264+
* in addition to a parent/child relationship.
265+
*/
266+
export enum SpanKind {
267+
/** Unspecified */
268+
UNSPECIFIED = 0,
269+
/**
270+
* Indicates that the span covers server-side handling of an RPC or other
271+
* remote network request.
272+
*/
273+
SERVER = 1,
274+
/**
275+
* Indicates that the span covers the client-side wrapper around an RPC or
276+
* other remote request.
277+
*/
278+
CLIENT = 2
279+
}
280+
281+
/**
282+
* Type of link. The relationship of the current span relative to the linked
283+
* span.
284+
*/
285+
export enum LinkType {
286+
/**
287+
* The relationship of the two spans is unknown, or known but other
288+
* than parent-child.
289+
*/
290+
UNSPECIFIED = 0,
291+
/** The linked span is a child of the current span. */
292+
CHILD_LINKED_SPAN = 1,
293+
/** The linked span is a parent of the current span. */
294+
PARENT_LINKED_SPAN = 2
295+
}
296+
252297
/** Interface for Span */
253298
export interface Span {
254299
/** The Span ID of this span */
@@ -264,7 +309,7 @@ export interface Span {
264309
name: string;
265310

266311
/** Kind of span. */
267-
kind: string;
312+
kind: SpanKind;
268313

269314
/** An object to log information to */
270315
logger: loggerTypes.Logger;
@@ -359,7 +404,7 @@ export interface Span {
359404
* @param attributes A set of attributes on the link.
360405
*/
361406
addLink(
362-
traceId: string, spanId: string, type: string,
407+
traceId: string, spanId: string, type: LinkType,
363408
attributes?: Attributes): void;
364409

365410
/**
@@ -368,7 +413,7 @@ export interface Span {
368413
* @param id An identifier for the message event.
369414
* @param timestamp A timestamp for this event.
370415
*/
371-
addMessageEvent(type: string, id: string, timestamp?: number): void;
416+
addMessageEvent(type: MessageEventType, id: string, timestamp?: number): void;
372417

373418
/**
374419
* Sets a status to the span.
@@ -393,7 +438,7 @@ export interface RootSpan extends Span {
393438
readonly spans: Span[];
394439

395440
/** Starts a new Span instance in the RootSpan instance */
396-
startChildSpan(name: string, type: string): Span;
441+
startChildSpan(name: string, kind: SpanKind): Span;
397442
}
398443

399444

@@ -460,7 +505,7 @@ export interface Tracer extends SpanEventListener {
460505
* @param parentSpanId Parent SpanId
461506
* @returns The new Span instance started
462507
*/
463-
startChildSpan(name?: string, type?: string, parentSpanId?: string): Span;
508+
startChildSpan(name?: string, type?: SpanKind, parentSpanId?: string): Span;
464509

465510
/**
466511
* Binds the trace context to the given function.

packages/opencensus-core/test/test-console-exporter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as assert from 'assert';
18+
import {SpanKind} from '../src';
1819
import {ConsoleExporter, NoopExporter} from '../src/exporters/console-exporter';
1920
import {RootSpan} from '../src/trace/model/root-span';
2021
import {CoreTracer} from '../src/trace/model/tracer';
@@ -86,7 +87,7 @@ describe('ConsoleLogExporter', () => {
8687
const exporter = new ConsoleExporter(defaultBufferConfig);
8788
const rootSpan = new RootSpan(tracer);
8889
rootSpan.start();
89-
rootSpan.startChildSpan('name', 'type', rootSpan.traceId);
90+
rootSpan.startChildSpan('name', SpanKind.UNSPECIFIED, rootSpan.traceId);
9091
const queue: RootSpan[] = [rootSpan];
9192

9293
return exporter.publish(queue).then(() => {
@@ -98,4 +99,4 @@ describe('ConsoleLogExporter', () => {
9899
});
99100
});
100101
});
101-
});
102+
});

packages/opencensus-core/test/test-exporter-buffer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as assert from 'assert';
18+
import {SpanKind} from '../src';
1819
import * as logger from '../src/common/console-logger';
1920
import {NoopExporter} from '../src/exporters/console-exporter';
2021
import {ExporterBuffer} from '../src/exporters/exporter-buffer';
@@ -40,7 +41,7 @@ const createRootSpans = (num: number): RootSpan[] => {
4041
const rootSpan = new RootSpan(tracer, {name: `rootSpan.${i}`});
4142
rootSpan.start();
4243
for (let j = 0; j < 10; j++) {
43-
rootSpan.startChildSpan(`childSpan.${i}.${j}`, 'client');
44+
rootSpan.startChildSpan(`childSpan.${i}.${j}`, SpanKind.CLIENT);
4445
}
4546
rootSpans.push(rootSpan);
4647
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ describe('RootSpan', () => {
5656
// TODO: Suggetion: make sure that root.spans.length is 1,
5757
// and that it's the same as the earlier (shadowed) span object
5858
root.start();
59-
const span = root.startChildSpan('spanName', 'spanType');
59+
const span = root.startChildSpan('spanName', types.SpanKind.CLIENT);
6060
assert.strictEqual(root.spans.length, 1);
6161
assert.strictEqual(span, root.spans[0]);
62+
assert.strictEqual(span.kind, types.SpanKind.CLIENT);
6263
assert.strictEqual(root.parentSpanId, null);
6364

6465
for (const span of root.spans) {
@@ -97,7 +98,7 @@ describe('RootSpan', () => {
9798
before(() => {
9899
root = new RootSpan(tracer);
99100
root.start();
100-
span = root.startChildSpan('spanName', 'spanType');
101+
span = root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
101102
});
102103

103104
it('should create span instance', () => {
@@ -115,7 +116,7 @@ describe('RootSpan', () => {
115116
describe('startSpan() before start rootspan', () => {
116117
it('should not create span', () => {
117118
const root = new RootSpan(tracer);
118-
const span = root.startChildSpan('spanName', 'spanType');
119+
const span = root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
119120
assert.strictEqual(span, null);
120121
});
121122
});
@@ -128,7 +129,7 @@ describe('RootSpan', () => {
128129
const root = new RootSpan(tracer);
129130
root.start();
130131
root.end();
131-
const span = root.startChildSpan('spanName', 'spanType');
132+
const span = root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
132133
assert.strictEqual(span, null);
133134
});
134135
});
@@ -163,7 +164,7 @@ describe('RootSpan', () => {
163164
it('should end all spans inside rootspan', () => {
164165
const root = new RootSpan(tracer);
165166
root.start();
166-
root.startChildSpan('spanName', 'spanType');
167+
root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
167168
root.end();
168169

169170
for (const span of root.spans) {
@@ -227,8 +228,8 @@ describe('RootSpan', () => {
227228
const span = new Span(rootSpan);
228229
span.start();
229230

230-
const LINK_TYPE = 'CHILD_LINKED_SPAN';
231-
rootSpan.addLink(rootSpan.traceId, span.id, LINK_TYPE);
231+
rootSpan.addLink(
232+
rootSpan.traceId, span.id, types.LinkType.CHILD_LINKED_SPAN);
232233

233234
assert.ok(rootSpan.links.length > 0);
234235
assert.equal(rootSpan.droppedLinksCount, 0);
@@ -249,7 +250,8 @@ describe('RootSpan', () => {
249250
const rootSpan = new RootSpan(tracer);
250251
rootSpan.start();
251252

252-
rootSpan.addMessageEvent('TYPE_UNSPECIFIED', 'message_event_test_id');
253+
rootSpan.addMessageEvent(
254+
types.MessageEventType.UNSPECIFIED, 'message_event_test_id');
253255

254256
assert.ok(rootSpan.messageEvents.length > 0);
255257
assert.equal(rootSpan.droppedMessageEventsCount, 0);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ describe('Span', () => {
263263
const span = new Span(rootSpan);
264264
span.start();
265265

266-
const LINK_TYPE = 'PARENT_LINKED_SPAN';
267-
span.addLink(span.traceId, rootSpan.id, LINK_TYPE);
266+
span.addLink(
267+
span.traceId, rootSpan.id, types.LinkType.PARENT_LINKED_SPAN);
268268

269269
assert.ok(span.links.length > 0);
270270
assert.equal(span.droppedLinksCount, 0);
@@ -277,9 +277,9 @@ describe('Span', () => {
277277
const span = new Span(rootSpan);
278278
span.start();
279279

280-
const LINK_TYPE = 'PARENT_LINKED_SPAN';
281280
for (let i = 0; i < 35; i++) {
282-
span.addLink(span.traceId, rootSpan.id, LINK_TYPE);
281+
span.addLink(
282+
span.traceId, rootSpan.id, types.LinkType.PARENT_LINKED_SPAN);
283283
}
284284

285285
assert.equal(span.links.length, 32);
@@ -303,7 +303,8 @@ describe('Span', () => {
303303
const span = new Span(rootSpan);
304304
span.start();
305305

306-
span.addMessageEvent('TYPE_UNSPECIFIED', 'message_event_test_id');
306+
span.addMessageEvent(
307+
types.MessageEventType.UNSPECIFIED, 'message_event_test_id');
307308

308309
assert.ok(span.messageEvents.length > 0);
309310
assert.equal(span.droppedMessageEventsCount, 0);
@@ -317,7 +318,8 @@ describe('Span', () => {
317318
const span = new Span(rootSpan);
318319
span.start();
319320
for (let i = 0; i < 35; i++) {
320-
span.addMessageEvent('TYPE_UNSPECIFIED', 'message_event_test_id');
321+
span.addMessageEvent(
322+
types.MessageEventType.UNSPECIFIED, 'message_event_test_id');
321323
}
322324

323325
assert.equal(span.messageEvents.length, 32);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('Tracer', () => {
194194
});
195195

196196
describe('startRootSpan() with context propagation', () => {
197-
const traceOptions = {name: 'rootName', kind: 'spanType'} as
197+
const traceOptions = {name: 'rootName', kind: types.SpanKind.UNSPECIFIED} as
198198
types.TraceOptions;
199199

200200
it('should create new RootSpan instance, no propagation', () => {
@@ -295,7 +295,7 @@ describe('Tracer', () => {
295295
const tracer = new CoreTracer();
296296
tracer.start(defaultConfig);
297297
tracer.startRootSpan(options, (rootSpan) => {
298-
span = tracer.startChildSpan('spanName', 'spanType');
298+
span = tracer.startChildSpan('spanName', types.SpanKind.CLIENT);
299299
});
300300
});
301301
it('should create a Span instance', () => {
@@ -304,7 +304,7 @@ describe('Tracer', () => {
304304
it('should start a span', () => {
305305
assert.ok(span.started);
306306
assert.strictEqual(span.name, 'spanName');
307-
assert.strictEqual(span.kind, 'spanType');
307+
assert.strictEqual(span.kind, types.SpanKind.CLIENT);
308308
});
309309
});
310310

@@ -313,7 +313,8 @@ describe('Tracer', () => {
313313
it('should not create a Span instance, without a rootspan', () => {
314314
const tracer = new CoreTracer();
315315
tracer.start(defaultConfig);
316-
const span = tracer.startChildSpan('spanName', 'spanType');
316+
const span =
317+
tracer.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
317318
assert.equal(span, null);
318319
});
319320
});

0 commit comments

Comments
 (0)