Skip to content

Commit 74900c0

Browse files
committed
ref: Use client hooks
1 parent cfcab85 commit 74900c0

File tree

8 files changed

+41
-100
lines changed

8 files changed

+41
-100
lines changed

packages/core/src/baseclient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
388388
/** @inheritdoc */
389389
public on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext) => void): void;
390390

391+
/** @inheritdoc */
392+
public on(hook: 'otelSpanEnd', callback: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void): void;
393+
391394
/** @inheritdoc */
392395
public on(hook: string, callback: unknown): void {
393396
if (!this._hooks[hook]) {
@@ -413,6 +416,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
413416
/** @inheritdoc */
414417
public emit(hook: 'createDsc', dsc: DynamicSamplingContext): void;
415418

419+
/** @inheritdoc */
420+
public emit(hook: 'otelSpanEnd', otelSpan: unknown, mutableOptions: { drop: boolean }): void;
421+
416422
/** @inheritdoc */
417423
public emit(hook: string, ...rest: unknown[]): void {
418424
if (this._hooks[hook]) {

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,32 +105,34 @@ export class Http implements Integration {
105105
// eslint-disable-next-line deprecation/deprecation
106106
this._shouldCreateSpanForRequest || clientOptions?.shouldCreateSpanForRequest;
107107

108-
client?.otelHooks.on('spanEnd', this._onSpanEnd);
108+
client?.on?.('otelSpanEnd', this._onSpanEnd);
109109
}
110110

111111
/**
112112
* Unregister this integration.
113113
*/
114114
public unregister(): void {
115115
this._unload?.();
116-
117-
const client = getCurrentHub().getClient<NodeExperimentalClient>();
118-
client?.otelHooks.off('spanEnd', this._onSpanEnd);
119116
}
120117

121-
private _onSpanEnd: (otelSpan: OtelSpan) => void | false = (otelSpan: OtelSpan) => {
118+
private _onSpanEnd: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void = (
119+
otelSpan: unknown,
120+
mutableOptions: { drop: boolean },
121+
) => {
122122
if (!this._shouldCreateSpans) {
123-
return false;
123+
mutableOptions.drop = true;
124+
return;
124125
}
125126

126127
if (this._shouldCreateSpanForRequest) {
127-
const url = getHttpUrl(otelSpan.attributes);
128+
const url = getHttpUrl((otelSpan as OtelSpan).attributes);
128129
if (url && !this._shouldCreateSpanForRequest(url)) {
129-
return false;
130+
mutableOptions.drop = true;
131+
return;
130132
}
131133
}
132134

133-
return undefined;
135+
return;
134136
};
135137

136138
/** Handle an emitted span from the HTTP instrumentation. */

packages/node-experimental/src/sdk/client.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import type { Tracer } from '@opentelemetry/api';
22
import { trace } from '@opentelemetry/api';
33
import { NodeClient, SDK_VERSION } from '@sentry/node';
4-
import { OtelHooks } from '@sentry/opentelemetry-node';
54

65
import type { NodeExperimentalClientOptions } from '../types';
76

87
/**
98
* A client built on top of the NodeClient, which provides some otel-specific things on top.
109
*/
1110
export class NodeExperimentalClient extends NodeClient {
12-
public otelHooks: OtelHooks;
13-
1411
private _tracer: Tracer | undefined;
1512

1613
public constructor(options: ConstructorParameters<typeof NodeClient>[0]) {
@@ -27,8 +24,6 @@ export class NodeExperimentalClient extends NodeClient {
2724
};
2825

2926
super(options);
30-
31-
this.otelHooks = new OtelHooks();
3227
}
3328

3429
/** Get the OTEL tracer. */

packages/node-experimental/src/sdk/initOtel.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ export function initOtel(): () => void {
1616
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
1717
}
1818

19-
// We use the custom otelHooks from the client to communicate with @sentry/opentelemetry-node
20-
const hooks = client?.otelHooks;
21-
2219
// Create and configure NodeTracerProvider
2320
const provider = new NodeTracerProvider({
2421
sampler: new AlwaysOnSampler(),
2522
});
26-
provider.addSpanProcessor(new SentrySpanProcessor({ hooks }));
23+
provider.addSpanProcessor(new SentrySpanProcessor());
2724

2825
// Initialize the provider
2926
provider.register({

packages/opentelemetry-node/src/hooks.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { SentrySpanProcessor } from './spanprocessor';
22
export { SentryPropagator } from './propagator';
3-
export { OtelHooks } from './hooks';
43
export * from './utils/spanData';

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { DynamicSamplingContext, Span as SentrySpan, TraceparentData, Trans
77
import { isString, logger } from '@sentry/utils';
88

99
import { SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY, SENTRY_TRACE_PARENT_CONTEXT_KEY } from './constants';
10-
import type { OtelHooks } from './hooks';
1110
import { isSentryRequestSpan } from './utils/isSentryRequest';
1211
import { mapOtelStatus } from './utils/mapOtelStatus';
1312
import { parseSpanDescription } from './utils/parseOtelSpanDescription';
@@ -29,11 +28,7 @@ function clearSpan(otelSpanId: string): void {
2928
* the Sentry SDK.
3029
*/
3130
export class SentrySpanProcessor implements OtelSpanProcessor {
32-
private _hooks?: OtelHooks;
33-
34-
public constructor({ hooks }: { hooks?: OtelHooks } = {}) {
35-
this._hooks = hooks;
36-
31+
public constructor() {
3732
addTracingExtensions();
3833

3934
addGlobalEventProcessor(event => {
@@ -114,11 +109,14 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
114109
return;
115110
}
116111

117-
if (this._hooks) {
118-
if (this._hooks.emit('spanEnd', otelSpan, sentrySpan) === false) {
119-
clearSpan(otelSpanId);
120-
return;
121-
}
112+
const client = getCurrentHub().getClient();
113+
114+
const mutableOptions = { drop: false };
115+
client && client.emit && client?.emit('otelSpanEnd', otelSpan, mutableOptions);
116+
117+
if (mutableOptions.drop) {
118+
clearSpan(otelSpanId);
119+
return;
122120
}
123121

124122
otelSpan.events.forEach(event => {

packages/types/src/client.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,16 @@ export interface Client<O extends ClientOptions = ClientOptions> {
190190
on?(hook: 'beforeAddBreadcrumb', callback: (breadcrumb: Breadcrumb, hint?: BreadcrumbHint) => void): void;
191191

192192
/**
193-
* Register a callback whena DSC (Dynamic Sampling Context) is created.
193+
* Register a callback when a DSC (Dynamic Sampling Context) is created.
194194
*/
195195
on?(hook: 'createDsc', callback: (dsc: DynamicSamplingContext) => void): void;
196196

197+
/**
198+
* Register a callback when an OpenTelemetry span is ended (in @sentry/opentelemetry-node).
199+
* The option argument may be mutated to drop the span.
200+
*/
201+
on?(hook: 'otelSpanEnd', callback: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void): void;
202+
197203
/**
198204
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
199205
* second argument.
@@ -221,4 +227,11 @@ export interface Client<O extends ClientOptions = ClientOptions> {
221227
* Fire a hook for when a DSC (Dynamic Sampling Context) is created. Expects the DSC as second argument.
222228
*/
223229
emit?(hook: 'createDsc', dsc: DynamicSamplingContext): void;
230+
231+
/**
232+
* Fire a hook for when an OpenTelemetry span is ended (in @sentry/opentelemetry-node).
233+
* Expects the OTEL span & as second argument, and an option object as third argument.
234+
* The option argument may be mutated to drop the span.
235+
*/
236+
emit?(hook: 'otelSpanEnd', otelSpan: unknown, mutableOptions: { drop: boolean }): void;
224237
}

0 commit comments

Comments
 (0)