Skip to content

Commit 385636a

Browse files
committed
align span & function names
Use regular `Span` type from `@opentelemetry/api` wherever possible.
1 parent 613269d commit 385636a

27 files changed

+240
-184
lines changed

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * as Handlers from './sdk/handlers';
1414
export * from './sdk/trace';
1515
export { getActiveSpan } from './utils/getActiveSpan';
1616
export { getCurrentHub, getHubFromCarrier } from './sdk/hub';
17+
export type { Span } from './types';
1718

1819
export {
1920
makeNodeTransport,
@@ -67,10 +68,8 @@ export type {
6768
Exception,
6869
Session,
6970
SeverityLevel,
70-
Span,
7171
StackFrame,
7272
Stacktrace,
7373
Thread,
74-
Transaction,
7574
User,
7675
} from '@sentry/node';

packages/node-experimental/src/integrations/express.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -31,7 +31,7 @@ export class Express extends NodePerformanceIntegration<void> implements Integra
3131
return [
3232
new ExpressInstrumentation({
3333
requestHook(span) {
34-
addOriginToOtelSpan(span, 'auto.http.otel.express');
34+
addOriginToSpan(span, 'auto.http.otel.express');
3535
},
3636
}),
3737
];

packages/node-experimental/src/integrations/fastify.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -31,7 +31,7 @@ export class Fastify extends NodePerformanceIntegration<void> implements Integra
3131
return [
3232
new FastifyInstrumentation({
3333
requestHook(span) {
34-
addOriginToOtelSpan(span, 'auto.http.otel.fastify');
34+
addOriginToSpan(span, 'auto.http.otel.fastify');
3535
},
3636
}),
3737
];

packages/node-experimental/src/integrations/graphql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -32,7 +32,7 @@ export class GraphQL extends NodePerformanceIntegration<void> implements Integra
3232
new GraphQLInstrumentation({
3333
ignoreTrivialResolveSpans: true,
3434
responseHook(span) {
35-
addOriginToOtelSpan(span, 'auto.graphql.otel.graphql');
35+
addOriginToSpan(span, 'auto.graphql.otel.graphql');
3636
},
3737
}),
3838
];

packages/node-experimental/src/integrations/http.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Span } from '@opentelemetry/api';
12
import { SpanKind } from '@opentelemetry/api';
23
import { registerInstrumentations } from '@opentelemetry/instrumentation';
34
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
@@ -7,12 +8,12 @@ import { stringMatchesSomePattern } from '@sentry/utils';
78
import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';
89

910
import { OTEL_ATTR_ORIGIN } from '../constants';
10-
import { setOtelSpanMetadata } from '../opentelemetry/spanData';
11+
import { setSpanMetadata } from '../opentelemetry/spanData';
1112
import type { NodeExperimentalClient } from '../sdk/client';
1213
import { getCurrentHub } from '../sdk/hub';
13-
import type { OtelSpan } from '../types';
1414
import { getRequestSpanData } from '../utils/getRequestSpanData';
1515
import { getRequestUrl } from '../utils/getRequestUrl';
16+
import { getSpanKind } from '../utils/getSpanKind';
1617

1718
interface HttpOptions {
1819
/**
@@ -128,10 +129,10 @@ export class Http implements Integration {
128129
requireParentforOutgoingSpans: true,
129130
requireParentforIncomingSpans: false,
130131
requestHook: (span, req) => {
131-
this._updateSpan(span as unknown as OtelSpan, req);
132+
this._updateSpan(span, req);
132133
},
133134
responseHook: (span, res) => {
134-
this._addRequestBreadcrumb(span as unknown as OtelSpan, res);
135+
this._addRequestBreadcrumb(span, res);
135136
},
136137
}),
137138
],
@@ -146,17 +147,17 @@ export class Http implements Integration {
146147
}
147148

148149
/** Update the span with data we need. */
149-
private _updateSpan(span: OtelSpan, request: ClientRequest | IncomingMessage): void {
150+
private _updateSpan(span: Span, request: ClientRequest | IncomingMessage): void {
150151
span.setAttribute(OTEL_ATTR_ORIGIN, 'auto.http.otel.http');
151152

152-
if (span.kind === SpanKind.SERVER) {
153-
setOtelSpanMetadata(span, { request });
153+
if (getSpanKind(span) === SpanKind.SERVER) {
154+
setSpanMetadata(span, { request });
154155
}
155156
}
156157

157158
/** Add a breadcrumb for outgoing requests. */
158-
private _addRequestBreadcrumb(span: OtelSpan, response: IncomingMessage | ServerResponse): void {
159-
if (!this._breadcrumbs || span.kind !== SpanKind.CLIENT) {
159+
private _addRequestBreadcrumb(span: Span, response: IncomingMessage | ServerResponse): void {
160+
if (!this._breadcrumbs || getSpanKind(span) !== SpanKind.CLIENT) {
160161
return;
161162
}
162163

packages/node-experimental/src/integrations/mongo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -31,7 +31,7 @@ export class Mongo extends NodePerformanceIntegration<void> implements Integrati
3131
return [
3232
new MongoDBInstrumentation({
3333
responseHook(span) {
34-
addOriginToOtelSpan(span, 'auto.db.otel.mongo');
34+
addOriginToSpan(span, 'auto.db.otel.mongo');
3535
},
3636
}),
3737
];

packages/node-experimental/src/integrations/mongoose.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { MongooseInstrumentation } from '@opentelemetry/instrumentation-mongoose';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -31,7 +31,7 @@ export class Mongoose extends NodePerformanceIntegration<void> implements Integr
3131
return [
3232
new MongooseInstrumentation({
3333
responseHook(span) {
34-
addOriginToOtelSpan(span, 'auto.db.otel.mongoose');
34+
addOriginToSpan(span, 'auto.db.otel.mongoose');
3535
},
3636
}),
3737
];

packages/node-experimental/src/integrations/mysql2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { MySQL2Instrumentation } from '@opentelemetry/instrumentation-mysql2';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -31,7 +31,7 @@ export class Mysql2 extends NodePerformanceIntegration<void> implements Integrat
3131
return [
3232
new MySQL2Instrumentation({
3333
responseHook(span) {
34-
addOriginToOtelSpan(span, 'auto.db.otel.mysql2');
34+
addOriginToSpan(span, 'auto.db.otel.mysql2');
3535
},
3636
}),
3737
];

packages/node-experimental/src/integrations/postgres.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
33
import type { Integration } from '@sentry/types';
44

5-
import { addOriginToOtelSpan } from '../utils/addOriginToSpan';
5+
import { addOriginToSpan } from '../utils/addOriginToSpan';
66
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
77

88
/**
@@ -32,7 +32,7 @@ export class Postgres extends NodePerformanceIntegration<void> implements Integr
3232
new PgInstrumentation({
3333
requireParentSpan: true,
3434
requestHook(span) {
35-
addOriginToOtelSpan(span, 'auto.db.otel.postgres');
35+
addOriginToSpan(span, 'auto.db.otel.postgres');
3636
},
3737
}),
3838
];
Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
1-
import type { Span as OtelSpan } from '@opentelemetry/api';
1+
import type { Span } from '@opentelemetry/api';
2+
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
23
import type { Hub, Scope, TransactionMetadata } from '@sentry/types';
34

5+
// We allow passing either a Span (=which is bascially a WriteableSpan), or a ReadableSpan
6+
// As we check by identity anyhow we don't really care
7+
type AbstractSpan = Span | ReadableSpan;
8+
49
// We store the parent span, scope & metadata in separate weakmaps, so we can access them for a given span
510
// This way we can enhance the data that an OTEL Span natively gives us
611
// and since we are using weakmaps, we do not need to clean up after ourselves
7-
const otelSpanScope = new WeakMap<OtelSpan, Scope>();
8-
const otelSpanHub = new WeakMap<OtelSpan, Hub>();
9-
const otelSpanParent = new WeakMap<OtelSpan, OtelSpan>();
10-
const otelSpanMetadata = new WeakMap<OtelSpan, Partial<TransactionMetadata>>();
12+
const SpanScope = new WeakMap<AbstractSpan, Scope>();
13+
const SpanHub = new WeakMap<AbstractSpan, Hub>();
14+
const SpanParent = new WeakMap<AbstractSpan, Span>();
15+
const SpanMetadata = new WeakMap<AbstractSpan, Partial<TransactionMetadata>>();
1116

1217
/** Set the Sentry scope on an OTEL span. */
13-
export function setOtelSpanScope(span: OtelSpan, scope: Scope): void {
14-
otelSpanScope.set(span, scope);
18+
export function setSpanScope(span: AbstractSpan, scope: Scope): void {
19+
SpanScope.set(span, scope);
1520
}
1621

1722
/** Get the Sentry scope of an OTEL span. */
18-
export function getOtelSpanScope(span: OtelSpan): Scope | undefined {
19-
return otelSpanScope.get(span);
23+
export function getSpanScope(span: AbstractSpan): Scope | undefined {
24+
return SpanScope.get(span);
2025
}
2126

2227
/** Set the Sentry hub on an OTEL span. */
23-
export function setOtelSpanHub(span: OtelSpan, hub: Hub): void {
24-
otelSpanHub.set(span, hub);
28+
export function setSpanHub(span: AbstractSpan, hub: Hub): void {
29+
SpanHub.set(span, hub);
2530
}
2631

2732
/** Get the Sentry hub of an OTEL span. */
28-
export function getOtelSpanHub(span: OtelSpan): Hub | undefined {
29-
return otelSpanHub.get(span);
33+
export function getSpanHub(span: AbstractSpan): Hub | undefined {
34+
return SpanHub.get(span);
3035
}
3136

3237
/** Set the parent OTEL span on an OTEL span. */
33-
export function setOtelSpanParent(span: OtelSpan, parentSpan: OtelSpan): void {
34-
otelSpanParent.set(span, parentSpan);
38+
export function setSpanParent(span: AbstractSpan, parentSpan: Span): void {
39+
SpanParent.set(span, parentSpan);
3540
}
3641

3742
/** Get the parent OTEL span of an OTEL span. */
38-
export function getOtelSpanParent(span: OtelSpan): OtelSpan | undefined {
39-
return otelSpanParent.get(span);
43+
export function getSpanParent(span: AbstractSpan): Span | undefined {
44+
return SpanParent.get(span);
4045
}
4146

4247
/** Set metadata for an OTEL span. */
43-
export function setOtelSpanMetadata(span: OtelSpan, metadata: Partial<TransactionMetadata>): void {
44-
otelSpanMetadata.set(span, metadata);
48+
export function setSpanMetadata(span: AbstractSpan, metadata: Partial<TransactionMetadata>): void {
49+
SpanMetadata.set(span, metadata);
4550
}
4651

4752
/** Get metadata for an OTEL span. */
48-
export function getOtelSpanMetadata(span: OtelSpan): Partial<TransactionMetadata> | undefined {
49-
return otelSpanMetadata.get(span);
53+
export function getSpanMetadata(span: AbstractSpan): Partial<TransactionMetadata> | undefined {
54+
return SpanMetadata.get(span);
5055
}

0 commit comments

Comments
 (0)