Skip to content

Commit 935bb61

Browse files
authored
feat(core): Align Span interface with OTEL (#12898)
1 parent ce1e7c7 commit 935bb61

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

packages/core/src/tracing/sentryNonRecordingSpan.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,37 @@ export class SentryNonRecordingSpan implements Span {
6868
): this {
6969
return this;
7070
}
71+
72+
/**
73+
* This should generally not be used,
74+
* but we need it for being comliant with the OTEL Span interface.
75+
*
76+
* @hidden
77+
* @internal
78+
*/
79+
public addLink(_link: unknown): this {
80+
return this;
81+
}
82+
83+
/**
84+
* This should generally not be used,
85+
* but we need it for being comliant with the OTEL Span interface.
86+
*
87+
* @hidden
88+
* @internal
89+
*/
90+
public addLinks(_links: unknown[]): this {
91+
return this;
92+
}
93+
94+
/**
95+
* This should generally not be used,
96+
* but we need it for being comliant with the OTEL Span interface.
97+
*
98+
* @hidden
99+
* @internal
100+
*/
101+
public recordException(_exception: unknown, _time?: number | undefined): void {
102+
// noop
103+
}
71104
}

packages/core/src/tracing/sentrySpan.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,39 @@ export class SentrySpan implements Span {
107107
}
108108
}
109109

110+
/**
111+
* This should generally not be used,
112+
* but it is needed for being compliant with the OTEL Span interface.
113+
*
114+
* @hidden
115+
* @internal
116+
*/
117+
public addLink(_link: unknown): this {
118+
return this;
119+
}
120+
121+
/**
122+
* This should generally not be used,
123+
* but it is needed for being compliant with the OTEL Span interface.
124+
*
125+
* @hidden
126+
* @internal
127+
*/
128+
public addLinks(_links: unknown[]): this {
129+
return this;
130+
}
131+
132+
/**
133+
* This should generally not be used,
134+
* but it is needed for being compliant with the OTEL Span interface.
135+
*
136+
* @hidden
137+
* @internal
138+
*/
139+
public recordException(_exception: unknown, _time?: number | undefined): void {
140+
// noop
141+
}
142+
110143
/** @inheritdoc */
111144
public spanContext(): SpanContextData {
112145
const { _spanId: spanId, _traceId: traceId, _sampled: sampled } = this;
@@ -118,18 +151,21 @@ export class SentrySpan implements Span {
118151
}
119152

120153
/** @inheritdoc */
121-
public setAttribute(key: string, value: SpanAttributeValue | undefined): void {
154+
public setAttribute(key: string, value: SpanAttributeValue | undefined): this {
122155
if (value === undefined) {
123156
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
124157
delete this._attributes[key];
125158
} else {
126159
this._attributes[key] = value;
127160
}
161+
162+
return this;
128163
}
129164

130165
/** @inheritdoc */
131-
public setAttributes(attributes: SpanAttributes): void {
166+
public setAttributes(attributes: SpanAttributes): this {
132167
Object.keys(attributes).forEach(key => this.setAttribute(key, attributes[key]));
168+
return this;
133169
}
134170

135171
/**

packages/opentelemetry/src/trace.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ export function continueTrace<T>(options: Parameters<typeof baseContinueTrace>[0
289289
function getActiveSpanWrapper<T>(parentSpan: Span | SentrySpan | undefined | null): (callback: () => T) => T {
290290
return parentSpan !== undefined
291291
? (callback: () => T) => {
292-
// We cast this, because the OTEL Span has a few more methods than our Span interface
293-
// TODO: Add these missing methods to the Span interface
294-
return withActiveSpan(parentSpan as Span, callback);
292+
return withActiveSpan(parentSpan, callback);
295293
}
296294
: (callback: () => T) => callback();
297295
}

packages/types/src/span.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ export interface Span {
219219
* Set a single attribute on the span.
220220
* Set it to `undefined` to remove the attribute.
221221
*/
222-
setAttribute(key: string, value: SpanAttributeValue | undefined): void;
222+
setAttribute(key: string, value: SpanAttributeValue | undefined): this;
223223

224224
/**
225225
* Set multiple attributes on the span.
226226
* Any attribute set to `undefined` will be removed.
227227
*/
228-
setAttributes(attributes: SpanAttributes): void;
228+
setAttributes(attributes: SpanAttributes): this;
229229

230230
/**
231231
* Sets the status attribute on the current span.
@@ -247,4 +247,19 @@ export interface Span {
247247
* Adds an event to the Span.
248248
*/
249249
addEvent(name: string, attributesOrStartTime?: SpanAttributes | SpanTimeInput, startTime?: SpanTimeInput): this;
250+
251+
/**
252+
* NOT USED IN SENTRY, only added for compliance with OTEL Span interface
253+
*/
254+
addLink(link: unknown): this;
255+
256+
/**
257+
* NOT USED IN SENTRY, only added for compliance with OTEL Span interface
258+
*/
259+
addLinks(links: unknown): this;
260+
261+
/**
262+
* NOT USED IN SENTRY, only added for compliance with OTEL Span interface
263+
*/
264+
recordException(exception: unknown, time?: number): void;
250265
}

0 commit comments

Comments
 (0)