Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow adding links after span creation #4536

Merged
merged 21 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0276fd2
feat: allow adding links after span creation
seemk Mar 11, 2024
93e0aec
Merge branch 'main' into span-add-links-after-creation
seemk Mar 11, 2024
a32adac
update changelog
seemk Mar 11, 2024
ddeb79b
Merge branch 'main' into span-add-links-after-creation
seemk Mar 14, 2024
c74d51e
improve docs
seemk Mar 25, 2024
4a3046f
Merge branch 'main' into span-add-links-after-creation
seemk Mar 25, 2024
b79411a
test: increase coverage
seemk Mar 27, 2024
c0efb25
Merge branch 'main' into span-add-links-after-creation
seemk Mar 27, 2024
e9c385b
Merge branch 'main' into span-add-links-after-creation
seemk Apr 1, 2024
fc9acbc
Merge branch 'main' into span-add-links-after-creation
seemk Apr 3, 2024
be1b848
Merge branch 'main' into span-add-links-after-creation
seemk Apr 8, 2024
e44df75
Update api/CHANGELOG.md
seemk Apr 8, 2024
90f48ea
update changelog
seemk Apr 8, 2024
c70bf11
Merge branch 'main' into span-add-links-after-creation
seemk Apr 11, 2024
ac5a351
Merge branch 'main' into span-add-links-after-creation
seemk Apr 15, 2024
ede8a63
Merge branch 'main' into span-add-links-after-creation
seemk Apr 17, 2024
a11433d
Merge branch 'main' into span-add-links-after-creation
seemk Apr 23, 2024
86b235a
Merge branch 'main' into span-add-links-after-creation
seemk Apr 25, 2024
a3758aa
Merge branch 'main' into span-add-links-after-creation
seemk Apr 26, 2024
7006fbd
Merge branch 'main' into span-add-links-after-creation
seemk May 2, 2024
a0047d3
Merge branch 'main' into span-add-links-after-creation
seemk May 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
* feat(instrumentation): Make `init()` method public [#4418](https://github.com/open-telemetry/opentelemetry-js/pull/4418)
* feat(context-zone-peer-dep, context-zone): support zone.js 0.13.x, 0.14.x [#4469](https://github.com/open-telemetry/opentelemetry-js/pull/4469) @pichlermarc
* chore: Semantic Conventions export individual strings [4185](https://github.com/open-telemetry/opentelemetry-js/issues/4185)
* feat(sdk-trace-base): allow adding span links after span creation [#4536](https://github.com/open-telemetry/opentelemetry-js/pull/4536) @seemk

### :bug: (Bug Fix)

Expand Down
1 change: 1 addition & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
### :bug: (Bug Fix)

* fix(api): fix unreachable @opentelemetry/api/experimental entry [#4446](https://github.com/open-telemetry/opentelemetry-js/pull/4446) @legendecas
* feat(api): allow adding span links after span creation [#4536](https://github.com/open-telemetry/opentelemetry-js/pull/4536) @seemk
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
seemk marked this conversation as resolved.
Show resolved Hide resolved

## 1.7.0

Expand Down
9 changes: 9 additions & 0 deletions api/src/trace/NonRecordingSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import { Span } from './span';
import { SpanContext } from './span_context';
import { SpanStatus } from './status';
import { Link } from './link';

/**
* The NonRecordingSpan is the default {@link Span} that is used when no Span
Expand Down Expand Up @@ -52,6 +53,14 @@
return this;
}

addLink(_link: Link): this {
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
return this;

Check warning on line 57 in api/src/trace/NonRecordingSpan.ts

View check run for this annotation

Codecov / codecov/patch

api/src/trace/NonRecordingSpan.ts#L56-L57

Added lines #L56 - L57 were not covered by tests
}

addLinks(_links: Link[]): this {
return this;

Check warning on line 61 in api/src/trace/NonRecordingSpan.ts

View check run for this annotation

Codecov / codecov/patch

api/src/trace/NonRecordingSpan.ts#L60-L61

Added lines #L60 - L61 were not covered by tests
}

// By default does nothing
setStatus(_status: SpanStatus): this {
return this;
Expand Down
21 changes: 21 additions & 0 deletions api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TimeInput } from '../common/Time';
import { SpanAttributes, SpanAttributeValue } from './attributes';
import { SpanContext } from './span_context';
import { SpanStatus } from './status';
import { Link } from './link';

/**
* An interface that represents a span. A span represents a single operation
Expand Down Expand Up @@ -76,6 +77,26 @@ export interface Span {
startTime?: TimeInput
): this;

/**
* Adds a single link to the span.
*
* Links added after the creation will not affect the sampling decision.
martinkuba marked this conversation as resolved.
Show resolved Hide resolved
* It is preferred span links be added at span creation.
*
* @param link the link to add.
*/
addLink(link: Link): this;

/**
* Adds multiple links to the span.
*
* Links added after the creation will not affect the sampling decision.
* It is preferred span links be added at span creation.
*
* @param links the links to add.
*/
addLinks(links: Link[]): this;

/**
* Sets a status to the span. If used, this will override the default Span
* status. Default is {@link SpanStatusCode.UNSET}. SetStatus overrides the value
Expand Down
10 changes: 10 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ export class Span implements APISpan, ReadableSpan {
return this;
}

addLink(link: Link): this {
this.links.push(link);
return this;
}

addLinks(links: Link[]): this {
this.links.push(...links);
return this;
}

setStatus(status: SpanStatus): this {
if (this._isSpanEnded()) return this;
this.status = status;
Expand Down
76 changes: 52 additions & 24 deletions packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,30 +771,6 @@ describe('Span', () => {
});
});

it('should set a link', () => {
martinkuba marked this conversation as resolved.
Show resolved Hide resolved
const spanContext: SpanContext = {
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
const linkContext: SpanContext = {
traceId: 'b3cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
const attributes = { attr1: 'value', attr2: 123, attr3: true };
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT,
'12345',
[{ context: linkContext }, { context: linkContext, attributes }]
);
span.end();
});

it('should drop extra events', () => {
const span = new Span(
tracer,
Expand Down Expand Up @@ -959,6 +935,58 @@ describe('Span', () => {
span.end();
});

it('should be possible to add a link after span creation', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
'my-span',
spanContext,
SpanKind.CONSUMER
);

span.addLink({ context: linkContext });

span.end();

assert.strictEqual(span.links.length, 1);
assert.deepStrictEqual(span.links, [
{
context: linkContext,
},
]);
});

it('should be possible to add multiple links after span creation', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
'my-span',
spanContext,
SpanKind.CONSUMER
);

span.addLinks([
{ context: linkContext },
{
context: linkContext,
attributes: { attr1: 'value', attr2: 123, attr3: true },
},
]);

span.end();

assert.strictEqual(span.links.length, 2);
assert.deepStrictEqual(span.links, [
{
context: linkContext,
},
{
attributes: { attr1: 'value', attr2: 123, attr3: true },
context: linkContext,
},
]);
});

it('should return ReadableSpan with events', () => {
const span = new Span(
tracer,
Expand Down