Skip to content

Commit

Permalink
ref: Use client hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jul 31, 2023
1 parent dd34b8f commit 4319909
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 100 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/** @inheritdoc */
public on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext) => void): void;

/** @inheritdoc */
public on(hook: 'otelSpanEnd', callback: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void): void;

/** @inheritdoc */
public on(hook: string, callback: unknown): void {
if (!this._hooks[hook]) {
Expand All @@ -413,6 +416,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/** @inheritdoc */
public emit(hook: 'createDsc', dsc: DynamicSamplingContext): void;

/** @inheritdoc */
public emit(hook: 'otelSpanEnd', otelSpan: unknown, mutableOptions: { drop: boolean }): void;

/** @inheritdoc */
public emit(hook: string, ...rest: unknown[]): void {
if (this._hooks[hook]) {
Expand Down
20 changes: 11 additions & 9 deletions packages/node-experimental/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,34 @@ export class Http implements Integration {
// eslint-disable-next-line deprecation/deprecation
this._shouldCreateSpanForRequest || clientOptions?.shouldCreateSpanForRequest;

client?.otelHooks.on('spanEnd', this._onSpanEnd);
client?.on?.('otelSpanEnd', this._onSpanEnd);
}

/**
* Unregister this integration.
*/
public unregister(): void {
this._unload?.();

const client = getCurrentHub().getClient<NodeExperimentalClient>();
client?.otelHooks.off('spanEnd', this._onSpanEnd);
}

private _onSpanEnd: (otelSpan: OtelSpan) => void | false = (otelSpan: OtelSpan) => {
private _onSpanEnd: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void = (
otelSpan: unknown,
mutableOptions: { drop: boolean },
) => {
if (!this._shouldCreateSpans) {
return false;
mutableOptions.drop = true;
return;
}

if (this._shouldCreateSpanForRequest) {
const url = getHttpUrl(otelSpan.attributes);
const url = getHttpUrl((otelSpan as OtelSpan).attributes);
if (url && !this._shouldCreateSpanForRequest(url)) {
return false;
mutableOptions.drop = true;
return;
}
}

return undefined;
return;
};

/** Handle an emitted span from the HTTP instrumentation. */
Expand Down
5 changes: 0 additions & 5 deletions packages/node-experimental/src/sdk/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import { NodeClient, SDK_VERSION } from '@sentry/node';
import { OtelHooks } from '@sentry/opentelemetry-node';

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

/**
* A client built on top of the NodeClient, which provides some otel-specific things on top.
*/
export class NodeExperimentalClient extends NodeClient {
public otelHooks: OtelHooks;

private _tracer: Tracer | undefined;

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

super(options);

this.otelHooks = new OtelHooks();
}

/** Get the OTEL tracer. */
Expand Down
5 changes: 1 addition & 4 deletions packages/node-experimental/src/sdk/initOtel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ export function initOtel(): () => void {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
}

// We use the custom otelHooks from the client to communicate with @sentry/opentelemetry-node
const hooks = client?.otelHooks;

// Create and configure NodeTracerProvider
const provider = new NodeTracerProvider({
sampler: new AlwaysOnSampler(),
});
provider.addSpanProcessor(new SentrySpanProcessor({ hooks }));
provider.addSpanProcessor(new SentrySpanProcessor());

// Initialize the provider
provider.register({
Expand Down
69 changes: 0 additions & 69 deletions packages/opentelemetry-node/src/hooks.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/opentelemetry-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { SentrySpanProcessor } from './spanprocessor';
export { SentryPropagator } from './propagator';
export { OtelHooks } from './hooks';
export * from './utils/spanData';
20 changes: 9 additions & 11 deletions packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { DynamicSamplingContext, Span as SentrySpan, TraceparentData, Trans
import { isString, logger } from '@sentry/utils';

import { SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY, SENTRY_TRACE_PARENT_CONTEXT_KEY } from './constants';
import type { OtelHooks } from './hooks';
import { isSentryRequestSpan } from './utils/isSentryRequest';
import { mapOtelStatus } from './utils/mapOtelStatus';
import { parseSpanDescription } from './utils/parseOtelSpanDescription';
Expand All @@ -29,11 +28,7 @@ function clearSpan(otelSpanId: string): void {
* the Sentry SDK.
*/
export class SentrySpanProcessor implements OtelSpanProcessor {
private _hooks?: OtelHooks;

public constructor({ hooks }: { hooks?: OtelHooks } = {}) {
this._hooks = hooks;

public constructor() {
addTracingExtensions();

addGlobalEventProcessor(event => {
Expand Down Expand Up @@ -114,11 +109,14 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
return;
}

if (this._hooks) {
if (this._hooks.emit('spanEnd', otelSpan, sentrySpan) === false) {
clearSpan(otelSpanId);
return;
}
const client = getCurrentHub().getClient();

const mutableOptions = { drop: false };
client && client.emit && client?.emit('otelSpanEnd', otelSpan, mutableOptions);

if (mutableOptions.drop) {
clearSpan(otelSpanId);
return;
}

otelSpan.events.forEach(event => {
Expand Down
15 changes: 14 additions & 1 deletion packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,16 @@ export interface Client<O extends ClientOptions = ClientOptions> {
on?(hook: 'beforeAddBreadcrumb', callback: (breadcrumb: Breadcrumb, hint?: BreadcrumbHint) => void): void;

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

/**
* Register a callback when an OpenTelemetry span is ended (in @sentry/opentelemetry-node).
* The option argument may be mutated to drop the span.
*/
on?(hook: 'otelSpanEnd', callback: (otelSpan: unknown, mutableOptions: { drop: boolean }) => void): void;

/**
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
* second argument.
Expand Down Expand Up @@ -221,4 +227,11 @@ export interface Client<O extends ClientOptions = ClientOptions> {
* Fire a hook for when a DSC (Dynamic Sampling Context) is created. Expects the DSC as second argument.
*/
emit?(hook: 'createDsc', dsc: DynamicSamplingContext): void;

/**
* Fire a hook for when an OpenTelemetry span is ended (in @sentry/opentelemetry-node).
* Expects the OTEL span & as second argument, and an option object as third argument.
* The option argument may be mutated to drop the span.
*/
emit?(hook: 'otelSpanEnd', otelSpan: unknown, mutableOptions: { drop: boolean }): void;
}

0 comments on commit 4319909

Please sign in to comment.