From 0276fd2021a986c7d1181514e561fff7cd24090c Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 11 Mar 2024 11:59:39 +0200 Subject: [PATCH 1/6] feat: allow adding links after span creation --- api/src/trace/NonRecordingSpan.ts | 9 +++ api/src/trace/span.ts | 19 +++++ .../opentelemetry-sdk-trace-base/src/Span.ts | 10 +++ .../test/common/Span.test.ts | 76 +++++++++++++------ 4 files changed, 90 insertions(+), 24 deletions(-) diff --git a/api/src/trace/NonRecordingSpan.ts b/api/src/trace/NonRecordingSpan.ts index a9e5bcaf9b..9ee3d28837 100644 --- a/api/src/trace/NonRecordingSpan.ts +++ b/api/src/trace/NonRecordingSpan.ts @@ -21,6 +21,7 @@ import { INVALID_SPAN_CONTEXT } from './invalid-span-constants'; 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 @@ -52,6 +53,14 @@ export class NonRecordingSpan implements Span { return this; } + addLink(_link: Link): this { + return this; + } + + addLinks(_links: Link[]): this { + return this; + } + // By default does nothing setStatus(_status: SpanStatus): this { return this; diff --git a/api/src/trace/span.ts b/api/src/trace/span.ts index d80b8c2626..854296f9ee 100644 --- a/api/src/trace/span.ts +++ b/api/src/trace/span.ts @@ -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 @@ -76,6 +77,24 @@ export interface Span { startTime?: TimeInput ): this; + /** + * Adds a single link to the span. + * + * Links added after the creation will not affect the sampling decision. + * + * @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. + * + * @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 diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index 2a00be5d8a..f3590f3290 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -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; diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index 5dd7ec79e5..aa36407466 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -771,30 +771,6 @@ describe('Span', () => { }); }); - it('should set a link', () => { - 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, @@ -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, From a32adac5dde2a0f660bcf5b46b58f837d21ba536 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 11 Mar 2024 12:42:15 +0200 Subject: [PATCH 2/6] update changelog --- CHANGELOG.md | 1 + api/CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2980539eb..c54f6f234a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,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) diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index f33bcbaddf..b28ba5d9c3 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -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 ## 1.7.0 From c74d51e273ecfb925eabbbaa0c8bdd12d3280dd8 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 25 Mar 2024 11:31:04 +0200 Subject: [PATCH 3/6] improve docs --- api/src/trace/span.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/src/trace/span.ts b/api/src/trace/span.ts index 854296f9ee..e3b563f103 100644 --- a/api/src/trace/span.ts +++ b/api/src/trace/span.ts @@ -81,6 +81,7 @@ export interface Span { * Adds a single link 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 link the link to add. */ @@ -90,6 +91,7 @@ export interface Span { * 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. */ From b79411ab50336826b377f4a19e83af83a921a644 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Wed, 27 Mar 2024 14:28:12 +0200 Subject: [PATCH 4/6] test: increase coverage --- api/test/common/noop-implementations/noop-span.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/test/common/noop-implementations/noop-span.test.ts b/api/test/common/noop-implementations/noop-span.test.ts index 5bc341f31a..05020716de 100644 --- a/api/test/common/noop-implementations/noop-span.test.ts +++ b/api/test/common/noop-implementations/noop-span.test.ts @@ -36,6 +36,14 @@ describe('NonRecordingSpan', () => { my_number_attribute: 123, }); + const linkContext = { + traceId: 'e4cda95b652f4a1592b449d5929fda1b', + spanId: '7e0c63257de34c92', + traceFlags: TraceFlags.SAMPLED, + }; + span.addLink({ context: linkContext }); + span.addLinks([{ context: linkContext }]); + span.addEvent('sent'); span.addEvent('sent', { id: '42', key: 'value' }); From e44df75f858b6ea51ab373288b912fe3e000422c Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 8 Apr 2024 11:08:30 +0300 Subject: [PATCH 5/6] Update api/CHANGELOG.md Co-authored-by: Marc Pichler --- api/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index b28ba5d9c3..d73d706557 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file. * 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 + * This change is non-breaking for end-users, but breaking for Trace SDK implmentations in accordance with the [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/a03382ada8afa9415266a84dafac0510ec8c160f/specification/upgrading.md?plain=1#L97-L122) as new features need to be implemented. ## 1.7.0 From 90f48ead2ed0be444d6a12d9dd3b189cfd1ba279 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 8 Apr 2024 11:26:26 +0300 Subject: [PATCH 6/6] update changelog --- api/CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index d73d706557..94f20b2493 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -8,6 +8,9 @@ All notable changes to this project will be documented in this file. ### :rocket: (Enhancement) +* feat(api): allow adding span links after span creation [#4536](https://github.com/open-telemetry/opentelemetry-js/pull/4536) @seemk + * This change is non-breaking for end-users, but breaking for Trace SDK implmentations in accordance with the [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/a03382ada8afa9415266a84dafac0510ec8c160f/specification/upgrading.md?plain=1#L97-L122) as new features need to be implemented. + ### :bug: (Bug Fix) ### :books: (Refine Doc) @@ -23,8 +26,6 @@ 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 - * This change is non-breaking for end-users, but breaking for Trace SDK implmentations in accordance with the [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/a03382ada8afa9415266a84dafac0510ec8c160f/specification/upgrading.md?plain=1#L97-L122) as new features need to be implemented. ## 1.7.0