Skip to content

Commit c3387e4

Browse files
committed
Add convertSpanLinkType helper for OTel span links
1 parent c5fd1bb commit c3387e4

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎packages/deno/src/opentelemetry/tracer.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SENTRY_ORIGIN } from '@sentry/conventions/attributes';
2-
import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api';
2+
import type { Context, Link, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api';
33
import { trace } from '@opentelemetry/api';
44
import { isObjectLike, startInactiveSpan, startSpanManual } from '@sentry/core';
55
import type { SpanLink } from '@sentry/core';
@@ -33,7 +33,7 @@ class SentryDenoTracer implements Tracer {
3333
public startSpan(name: string, options?: SpanOptions): Span {
3434
return startInactiveSpan({
3535
...options,
36-
links: options?.links as SpanLink[] | undefined,
36+
links: convertSpanLinkType(options?.links),
3737
name,
3838
attributes: {
3939
...options?.attributes,
@@ -64,7 +64,7 @@ class SentryDenoTracer implements Tracer {
6464

6565
const spanOpts = {
6666
...opts,
67-
links: opts.links as SpanLink[] | undefined,
67+
links: convertSpanLinkType(opts.links),
6868
name,
6969
attributes: {
7070
...opts.attributes,
@@ -87,3 +87,14 @@ class SentryDenoTracer implements Tracer {
8787
return startSpanManual(spanOpts, callback) as ReturnType<F>;
8888
}
8989
}
90+
91+
/**
92+
* Converts OpenTelemetry span links to Sentry span links.
93+
*
94+
* Since `@opentelemetry/api` 1.10.0, link attribute values are typed as `unknown`.
95+
* Sentry's `SpanLink` only accepts strings, numbers, booleans and arrays of these as attribute values,
96+
* so OTel links do not match its type.
97+
*/
98+
function convertSpanLinkType(links: Link[] | undefined): SpanLink[] | undefined {
99+
return links as SpanLink[] | undefined;
100+
}

‎packages/opentelemetry/src/tracer.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {
1717
startNewTrace,
1818
withScope,
1919
} from '@sentry/core';
20-
import type { Span, SpanAttributes, SpanLink } from '@sentry/core';
20+
import type { Span, SpanAttributes } from '@sentry/core';
2121
import { SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY } from './constants';
22+
import { convertSpanLinkType } from './utils/convertSpanLinkType';
2223
import { SENTRY_KIND } from '@sentry/conventions/attributes';
2324

2425
export class SentryTracer implements Tracer {
@@ -134,7 +135,7 @@ export class SentryTracer implements Tracer {
134135
const sentryOptions = {
135136
name,
136137
attributes: (options.attributes as SpanAttributes) || {},
137-
links: options.links as SpanLink[] | undefined,
138+
links: convertSpanLinkType(options.links),
138139
startTime: options.startTime,
139140
};
140141

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Link } from '@opentelemetry/api';
2+
import type { SpanLink } from '@sentry/core';
3+
4+
/**
5+
* Converts OpenTelemetry span links to Sentry span links.
6+
*
7+
* Since `@opentelemetry/api` 1.10.0, link attribute values are typed as `unknown`.
8+
* Sentry's `SpanLink` only accepts strings, numbers, booleans and arrays of these as attribute values,
9+
* so OTel links do not match its type.
10+
*/
11+
export function convertSpanLinkType(links: Link[] | undefined): SpanLink[] | undefined {
12+
return links as SpanLink[] | undefined;
13+
}

0 commit comments

Comments
 (0)