Skip to content

Commit f33dc7c

Browse files
authored
fix: allow recording links only at Span creation time (#449)
* fix: allow recording links only at Span creation time * fix: build
1 parent b21ed97 commit f33dc7c

File tree

6 files changed

+32
-54
lines changed

6 files changed

+32
-54
lines changed

packages/opentelemetry-shim-opentracing/src/shim.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ function translateSpanOptions(
4141
startTime: options.startTime,
4242
};
4343

44-
// because there's no `Links` in SpanOptions, we set them in `TracerShim.startSpan()`
44+
if (options.references) {
45+
opts.links = translateReferences(options.references);
46+
}
47+
4548
if (options.childOf) {
4649
if (options.childOf instanceof SpanShim) {
4750
opts.parent = (options.childOf as SpanShim).getSpan();
@@ -111,13 +114,6 @@ export class TracerShim extends opentracing.Tracer {
111114
span.setAttributes(options.tags);
112115
}
113116

114-
if (options.references) {
115-
const links = translateReferences(options.references);
116-
for (const link of links) {
117-
span.addLink(link.spanContext, link.attributes);
118-
}
119-
}
120-
121117
return new SpanShim(this, span);
122118
}
123119

packages/opentelemetry-tracing/src/BasicTracer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export class BasicTracer implements types.Tracer {
100100
spanContext,
101101
options.kind || types.SpanKind.INTERNAL,
102102
parentContext ? parentContext.spanId : undefined,
103+
options.links || [],
103104
options.startTime
104105
);
105106
// Set default attributes

packages/opentelemetry-tracing/src/Span.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ export class Span implements types.Span, ReadableSpan {
5757
spanContext: types.SpanContext,
5858
kind: types.SpanKind,
5959
parentSpanId?: string,
60+
links: types.Link[] = [],
6061
startTime: types.TimeInput = hrTime()
6162
) {
6263
this.name = spanName;
6364
this.spanContext = spanContext;
6465
this.parentSpanId = parentSpanId;
6566
this.kind = kind;
67+
this.links = links;
6668
this.startTime = timeInputToHrTime(startTime);
6769
this._logger = parentTracer.logger;
6870
this._traceParams = parentTracer.getActiveTraceParams();
@@ -134,17 +136,6 @@ export class Span implements types.Span, ReadableSpan {
134136
return this;
135137
}
136138

137-
addLink(spanContext: types.SpanContext, attributes?: types.Attributes): this {
138-
if (this._isSpanEnded()) return this;
139-
140-
if (this.links.length >= this._traceParams.numberOfLinksPerSpan!) {
141-
this._logger.warn('Dropping extra links.');
142-
this.links.shift();
143-
}
144-
this.links.push({ spanContext, attributes });
145-
return this;
146-
}
147-
148139
setStatus(status: types.Status): this {
149140
if (this._isSpanEnded()) return this;
150141
this.status = status;

packages/opentelemetry-tracing/test/Span.test.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ describe('Span', () => {
9494
spanContext,
9595
SpanKind.SERVER,
9696
undefined,
97+
[],
9798
0
9899
);
99100
const timeMS = 123;
@@ -114,6 +115,7 @@ describe('Span', () => {
114115
spanContext,
115116
SpanKind.SERVER,
116117
undefined,
118+
[],
117119
0
118120
);
119121
const timeMS = 123;
@@ -167,22 +169,22 @@ describe('Span', () => {
167169
spanId: '5e0c63257de34c92',
168170
traceFlags: TraceFlags.SAMPLED,
169171
};
170-
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
171-
span.addLink(spanContext);
172-
span.addLink(spanContext, { attr1: 'value', attr2: 123, attr3: true });
172+
const attributes = { attr1: 'value', attr2: 123, attr3: true };
173+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT, '12345', [
174+
{ spanContext },
175+
{ spanContext, attributes },
176+
]);
173177
span.end();
174178
});
175179

176180
it('should drop extra links, attributes and events', () => {
177181
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
178182
for (let i = 0; i < 150; i++) {
179-
span.addLink(spanContext);
180183
span.setAttribute('foo' + i, 'bar' + i);
181184
span.addEvent('sent' + i);
182185
}
183186
span.end();
184187

185-
assert.strictEqual(span.links.length, 32);
186188
assert.strictEqual(span.events.length, 128);
187189
assert.strictEqual(Object.keys(span.attributes).length, 32);
188190
assert.strictEqual(span.events[span.events.length - 1].name, 'sent149');
@@ -246,27 +248,24 @@ describe('Span', () => {
246248
});
247249

248250
it('should return ReadableSpan with links', () => {
249-
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
250-
span.addLink(spanContext);
251-
let readableSpan = span.toReadableSpan();
252-
assert.strictEqual(readableSpan.links.length, 1);
253-
assert.deepStrictEqual(readableSpan.links, [
254-
{
255-
attributes: undefined,
256-
spanContext: {
257-
spanId: '6e0c63257de34c92',
258-
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
259-
traceFlags: 1,
251+
const span = new Span(
252+
tracer,
253+
'my-span',
254+
spanContext,
255+
SpanKind.CLIENT,
256+
undefined,
257+
[
258+
{ spanContext },
259+
{
260+
spanContext,
261+
attributes: { attr1: 'value', attr2: 123, attr3: true },
260262
},
261-
},
262-
]);
263-
264-
span.addLink(spanContext, { attr1: 'value', attr2: 123, attr3: true });
265-
readableSpan = span.toReadableSpan();
263+
]
264+
);
265+
const readableSpan = span.toReadableSpan();
266266
assert.strictEqual(readableSpan.links.length, 2);
267267
assert.deepStrictEqual(readableSpan.links, [
268268
{
269-
attributes: undefined,
270269
spanContext,
271270
},
272271
{
@@ -276,10 +275,6 @@ describe('Span', () => {
276275
]);
277276

278277
span.end();
279-
// shouldn't add new link
280-
span.addLink(spanContext);
281-
readableSpan = span.toReadableSpan();
282-
assert.strictEqual(readableSpan.links.length, 2);
283278
});
284279

285280
it('should return ReadableSpan with events', () => {

packages/opentelemetry-types/src/trace/SpanOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Span } from './span';
1818
import { Attributes } from './attributes';
1919
import { SpanKind } from './span_kind';
2020
import { SpanContext } from './span_context';
21+
import { Link } from './link';
2122

2223
/**
2324
* Options needed for span creation
@@ -32,6 +33,9 @@ export interface SpanOptions {
3233
/** Indicates that if this Span is active and recording information like events with the `AddEvent` operation and attributes using `setAttributes`. */
3334
isRecording?: boolean;
3435

36+
/** A spans links */
37+
links?: Link[];
38+
3539
/**
3640
* A parent SpanContext (or Span, for convenience) that the newly-started
3741
* span will be the child of.

packages/opentelemetry-types/src/trace/span.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,6 @@ export interface Span {
6464
startTime?: TimeInput
6565
): this;
6666

67-
/**
68-
* Adds a link to the Span.
69-
*
70-
* @param spanContext the context of the linked span.
71-
* @param [attributes] the attributes that will be added; these are
72-
* associated with this link.
73-
*/
74-
addLink(spanContext: SpanContext, attributes?: Attributes): this;
75-
7667
/**
7768
* Sets a status to the span. If used, this will override the default Span
7869
* status. Default is {@link CanonicalCode.OK}.

0 commit comments

Comments
 (0)