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

Commit 2c56a2a

Browse files
authored
add option of passing in an object to the createChildSpan (#349)
1 parent 5b8dced commit 2c56a2a

File tree

13 files changed

+97
-31
lines changed

13 files changed

+97
-31
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
- Add optional `compressedSize` and `uncompressedSize` params to `Span.addMessageEvent`
88
- Add support for ```tags```, ```status``` and ```annotation``` in Zipkin exporter.
99
- Add support for Binary propagation format.
10+
- Add support for object(```SpanOptions```) as an argument for ```startChildSpan``` function, similar to ```startRootSpan```.
1011

1112
## 0.0.9 - 2019-02-12
1213
- Add Metrics API.

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ export class RootSpan extends SpanBase implements types.RootSpan {
106106
* @param kind Span kind.
107107
* @param parentSpanId Span parent ID.
108108
*/
109-
startChildSpan(name: string, kind: types.SpanKind, parentSpanId?: string):
110-
types.Span {
109+
startChildSpan(
110+
nameOrOptions?: string|types.SpanOptions, kind?: types.SpanKind,
111+
parentSpanId?: string): types.Span {
111112
if (this.ended) {
112113
this.logger.debug(
113114
'calling %s.startSpan() on ended %s %o', this.className,
@@ -121,11 +122,21 @@ export class RootSpan extends SpanBase implements types.RootSpan {
121122
return null;
122123
}
123124
const newSpan = new Span(this);
124-
if (name) {
125-
newSpan.name = name;
125+
let spanName;
126+
let spanKind;
127+
if (typeof nameOrOptions === 'object') {
128+
spanName = nameOrOptions.name;
129+
spanKind = nameOrOptions.kind;
130+
} else {
131+
spanName = nameOrOptions;
132+
spanKind = kind;
126133
}
127-
if (kind) {
128-
newSpan.kind = kind;
134+
135+
if (spanName) {
136+
newSpan.name = spanName;
137+
}
138+
if (spanKind) {
139+
newSpan.kind = spanKind;
129140
}
130141
newSpan.start();
131142
this.spansLocal.push(newSpan);

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,15 @@ export class CoreTracer implements types.Tracer {
239239
* @param kind optional The span kind.
240240
* @param parentSpanId The parent span ID.
241241
*/
242-
startChildSpan(name?: string, kind?: types.SpanKind): types.Span {
242+
startChildSpan(
243+
nameOrOptions?: string|types.SpanOptions,
244+
kind?: types.SpanKind): types.Span {
243245
let newSpan: types.Span = null;
244246
if (!this.currentRootSpan) {
245247
this.logger.debug(
246248
'no current trace found - must start a new root span first');
247249
} else {
248-
newSpan = this.currentRootSpan.startChildSpan(name, kind);
250+
newSpan = this.currentRootSpan.startChildSpan(nameOrOptions, kind);
249251
}
250252
return newSpan;
251253
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ export interface TraceOptions {
234234
kind?: SpanKind;
235235
}
236236

237+
/** Defines the span options */
238+
export interface SpanOptions {
239+
/** Span name */
240+
name: string;
241+
/** Span kind */
242+
kind?: SpanKind;
243+
/** Span parent ID */
244+
parentSpanId?: string;
245+
}
246+
237247
export type TraceState = string;
238248

239249
/** Defines the span context */
@@ -449,7 +459,9 @@ export interface RootSpan extends Span {
449459
readonly spans: Span[];
450460

451461
/** Starts a new Span instance in the RootSpan instance */
452-
startChildSpan(name: string, kind: SpanKind): Span;
462+
startChildSpan(name?: string, kind?: SpanKind, parentSpanId?: string): Span;
463+
startChildSpan(options?: SpanOptions): Span;
464+
startChildSpan(nameOrOptions?: string|SpanOptions, kind?: SpanKind): Span;
453465
}
454466

455467

@@ -514,9 +526,11 @@ export interface Tracer extends SpanEventListener {
514526
* @param name Span name
515527
* @param type Span type
516528
* @param parentSpanId Parent SpanId
529+
* @param options Span Options
517530
* @returns The new Span instance started
518531
*/
519532
startChildSpan(name?: string, type?: SpanKind, parentSpanId?: string): Span;
533+
startChildSpan(options?: SpanOptions): Span;
520534

521535
/**
522536
* Binds the trace context to the given function.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ 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', types.SpanKind.CLIENT);
59+
const span =
60+
root.startChildSpan({name: 'spanName', kind: types.SpanKind.CLIENT});
61+
6062
assert.strictEqual(root.spans.length, 1);
6163
assert.strictEqual(span, root.spans[0]);
6264
assert.strictEqual(span.kind, types.SpanKind.CLIENT);

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,9 @@ describe('Tracer', () => {
291291
/** Should create and start a Span instance into a rootSpan */
292292
describe('startChildSpan()', () => {
293293
let span: types.Span;
294+
let tracer: types.Tracer;
294295
before(() => {
295-
const tracer = new CoreTracer();
296+
tracer = new CoreTracer();
296297
tracer.start(defaultConfig);
297298
tracer.startRootSpan(options, (rootSpan) => {
298299
span = tracer.startChildSpan('spanName', types.SpanKind.CLIENT);
@@ -306,15 +307,46 @@ describe('Tracer', () => {
306307
assert.strictEqual(span.name, 'spanName');
307308
assert.strictEqual(span.kind, types.SpanKind.CLIENT);
308309
});
310+
311+
it('should start a span with SpanObject', () => {
312+
let spanWithObject: types.Span;
313+
tracer.startRootSpan(options, (rootSpan) => {
314+
spanWithObject = tracer.startChildSpan(
315+
{name: 'my-span', kind: types.SpanKind.SERVER});
316+
});
317+
assert.ok(spanWithObject.started);
318+
assert.strictEqual(spanWithObject.name, 'my-span');
319+
assert.strictEqual(spanWithObject.kind, types.SpanKind.SERVER);
320+
});
321+
322+
it('should start a span with SpanObject-name', () => {
323+
let spanWithObject: types.Span;
324+
tracer.startRootSpan(options, (rootSpan) => {
325+
spanWithObject = tracer.startChildSpan({name: 'my-span1'});
326+
});
327+
assert.ok(spanWithObject.started);
328+
assert.strictEqual(spanWithObject.name, 'my-span1');
329+
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
330+
});
331+
332+
it('should start a span without params', () => {
333+
let spanWithObject: types.Span;
334+
tracer.startRootSpan(options, (rootSpan) => {
335+
spanWithObject = tracer.startChildSpan();
336+
});
337+
assert.ok(spanWithObject.started);
338+
assert.strictEqual(spanWithObject.name, null);
339+
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
340+
});
309341
});
310342

311343
/** Should not create a Span instance */
312344
describe('startChildSpan() before startRootSpan()', () => {
313345
it('should not create a Span instance, without a rootspan', () => {
314346
const tracer = new CoreTracer();
315347
tracer.start(defaultConfig);
316-
const span =
317-
tracer.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
348+
const span = tracer.startChildSpan(
349+
{name: 'spanName', kind: types.SpanKind.UNSPECIFIED});
318350
assert.equal(span, null);
319351
});
320352
});

packages/opencensus-exporter-instana/test/test-instana.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ describe('Instana Exporter', function() {
6060
.startRootSpan(
6161
{name: 'root-test'},
6262
async (rootSpan: RootSpan) => {
63-
const span =
64-
rootSpan.startChildSpan('spanTest', SpanKind.CLIENT);
63+
const span = rootSpan.startChildSpan(
64+
{name: 'spanTest', kind: SpanKind.CLIENT});
6565
span.end();
6666
rootSpan.end();
6767
return exporter.publish([rootSpan, rootSpan]);

packages/opencensus-exporter-zipkin/test/test-zipkin.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe('Zipkin Exporter', function() {
7474
tracer.start(defaultConfig);
7575

7676
tracer.startRootSpan({name: 'root-test'}, (rootSpan: RootSpan) => {
77-
const span = rootSpan.startChildSpan('spanTest', SpanKind.CLIENT);
77+
const span =
78+
rootSpan.startChildSpan({name: 'spanTest', kind: SpanKind.CLIENT});
7879
span.end();
7980
rootSpan.end();
8081
assert.ok(exporter.buffer.getQueue().length > 0);
@@ -91,7 +92,8 @@ describe('Zipkin Exporter', function() {
9192

9293
return tracer.startRootSpan(
9394
{name: 'root-test'}, async (rootSpan: RootSpan) => {
94-
const span = rootSpan.startChildSpan('spanTest', SpanKind.CLIENT);
95+
const span = rootSpan.startChildSpan(
96+
{name: 'spanTest', kind: SpanKind.CLIENT});
9597
span.end();
9698
rootSpan.end();
9799
return exporter.publish([rootSpan, rootSpan]).then((result) => {
@@ -108,7 +110,8 @@ describe('Zipkin Exporter', function() {
108110
tracer.start(defaultConfig);
109111

110112
return tracer.startRootSpan({name: 'root-test'}, (rootSpan: RootSpan) => {
111-
const span = rootSpan.startChildSpan('spanTest', SpanKind.CLIENT);
113+
const span =
114+
rootSpan.startChildSpan({name: 'spanTest', kind: SpanKind.CLIENT});
112115
span.addAttribute('my-int-attribute', 100);
113116
span.addAttribute('my-str-attribute', 'value');
114117
span.addAttribute('my-bool-attribute', true);
@@ -183,8 +186,8 @@ describe('Zipkin Exporter', function() {
183186

184187
return tracer.startRootSpan(
185188
{name: 'root-test'}, async (rootSpan: RootSpan) => {
186-
const span =
187-
rootSpan.startChildSpan('spanTest', SpanKind.CLIENT);
189+
const span = rootSpan.startChildSpan(
190+
{name: 'spanTest', kind: SpanKind.CLIENT});
188191
span.end();
189192
rootSpan.end();
190193
return exporter.publish([rootSpan]).then((result) => {

packages/opencensus-exporter-zpages/test/test-zpages.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AggregationType, CountData, DistributionData, globalStats, Measure, Measurement, MeasureUnit, RootSpan, SumData, TagMap, TracerConfig} from '@opencensus/core';
17+
import {AggregationType, CountData, DistributionData, globalStats, Measure, Measurement, MeasureUnit, RootSpan, SpanKind, SumData, TagMap, TracerConfig} from '@opencensus/core';
1818
import * as assert from 'assert';
1919
import axios from 'axios';
2020
import * as http from 'http';
2121
import * as qs from 'querystring';
22+
2223
import {ZpagesExporter, ZpagesExporterOptions} from '../src/zpages';
2324
import {RpczData} from '../src/zpages-frontend/page-handlers/rpcz.page-handler';
2425
import {StatsViewData, StatszParams} from '../src/zpages-frontend/page-handlers/statsz.page-handler';
@@ -133,8 +134,8 @@ describe('Zpages Exporter', () => {
133134

134135
tracing.tracer.startRootSpan(
135136
{name: 'rootSpanTest'}, (rootSpan: RootSpan) => {
136-
const span =
137-
tracing.tracer.startChildSpan('spanNameTest', 'spanType');
137+
const span = tracing.tracer.startChildSpan(
138+
{name: 'spanNameTest', kind: SpanKind.CLIENT});
138139
span.end();
139140
rootSpan.end();
140141
});

packages/opencensus-instrumentation-grpc/src/grpc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export class GrpcPlugin extends BasePlugin {
319319
plugin.makeGrpcClientRemoteCall(original, args, this, plugin));
320320
} else {
321321
const span = plugin.tracer.startChildSpan(
322-
traceOptions.name, traceOptions.kind);
322+
{name: traceOptions.name, kind: traceOptions.kind});
323323
return (plugin.makeGrpcClientRemoteCall(
324324
original, args, this, plugin))(span);
325325
}

packages/opencensus-instrumentation-http/src/http.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class HttpPlugin extends BasePlugin {
323323
} else {
324324
plugin.logger.debug('outgoingRequest starting a child span');
325325
const span = plugin.tracer.startChildSpan(
326-
traceOptions.name, traceOptions.kind);
326+
{name: traceOptions.name, kind: traceOptions.kind});
327327
return (plugin.getMakeRequestTraceFunction(request, options, plugin))(
328328
span);
329329
}

packages/opencensus-instrumentation-http2/src/http2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class Http2Plugin extends HttpPlugin {
112112
request, headers, authority, plugin));
113113
} else {
114114
const span = plugin.tracer.startChildSpan(
115-
traceOptions.name, traceOptions.kind);
115+
{name: traceOptions.name, kind: traceOptions.kind});
116116
return (plugin.getMakeHttp2RequestTraceFunction(
117117
request, headers, authority, plugin))(span);
118118
}

packages/opencensus-instrumentation-mongodb/src/mongodb.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ export class MongoDBPlugin extends BasePlugin {
9696
type = 'command';
9797
}
9898

99-
const span =
100-
plugin.tracer.startChildSpan(ns + '.' + type, SpanKind.SERVER);
99+
const span = plugin.tracer.startChildSpan(
100+
{name: `${ns}.${type}`, kind: SpanKind.SERVER});
101101
resultHandler = plugin.patchEnd(span, resultHandler);
102102
}
103103

@@ -116,8 +116,8 @@ export class MongoDBPlugin extends BasePlugin {
116116
let resultHandler = args[args.length - 1];
117117
if (plugin.tracer.currentRootSpan && arguments.length > 0 &&
118118
typeof resultHandler === 'function') {
119-
const span =
120-
plugin.tracer.startChildSpan(ns + '.query', SpanKind.SERVER);
119+
const span = plugin.tracer.startChildSpan(
120+
{name: `${ns}.query`, kind: SpanKind.SERVER});
121121
resultHandler = plugin.patchEnd(span, resultHandler);
122122
}
123123

@@ -136,7 +136,7 @@ export class MongoDBPlugin extends BasePlugin {
136136
if (plugin.tracer.currentRootSpan && arguments.length > 0 &&
137137
typeof resultHandler === 'function') {
138138
const span = plugin.tracer.startChildSpan(
139-
this.ns + '.cursor', SpanKind.SERVER);
139+
{name: `${this.ns}.cursor`, kind: SpanKind.SERVER});
140140
resultHandler = plugin.patchEnd(span, resultHandler);
141141
}
142142

0 commit comments

Comments
 (0)