Skip to content

Commit

Permalink
feat(node): Add openTelemetryInstrumentations option (#14484)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Nov 27, 2024
1 parent 23e3783 commit 09a31d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/node/src/sdk/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as os from 'node:os';
import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import type { Scope, ServerRuntimeClientOptions } from '@sentry/core';
import { SDK_VERSION, ServerRuntimeClient, applySdkMetadata, logger } from '@sentry/core';
Expand All @@ -27,6 +28,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
};

if (options.openTelemetryInstrumentations) {
registerInstrumentations({
instrumentations: options.openTelemetryInstrumentations,
});
}

applySdkMetadata(clientOptions, 'node');

logger.log(
Expand Down
10 changes: 9 additions & 1 deletion packages/node/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Span as WriteableSpan } from '@opentelemetry/api';
import type { Instrumentation } from '@opentelemetry/instrumentation';
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import type { ClientOptions, Options, SamplingContext, Scope, Span, TracePropagationTargets } from '@sentry/types';

Expand Down Expand Up @@ -90,6 +91,13 @@ export interface BaseNodeOptions {
*/
skipOpenTelemetrySetup?: boolean;

/**
* Provide an array of OpenTelemetry Instrumentations that should be registered.
*
* Use this option if you want to register OpenTelemetry instrumentation that the Sentry SDK does not yet have support for.
*/
openTelemetryInstrumentations?: Instrumentation[];

/**
* The max. duration in seconds that the SDK will wait for parent spans to be finished before discarding a span.
* The SDK will automatically clean up spans that have no finished parent after this duration.
Expand Down Expand Up @@ -156,7 +164,7 @@ export interface CurrentScopes {
* The base `Span` type is basically a `WriteableSpan`.
* There are places where we basically want to allow passing _any_ span,
* so in these cases we type this as `AbstractSpan` which could be either a regular `Span` or a `ReadableSpan`.
* You'll have to make sur to check revelant fields before accessing them.
* You'll have to make sur to check relevant fields before accessing them.
*
* Note that technically, the `Span` exported from `@opentelemetry/sdk-trace-base` matches this,
* but we cannot be 100% sure that we are actually getting such a span, so this type is more defensive.
Expand Down
16 changes: 16 additions & 0 deletions packages/node/test/sdk/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as os from 'os';
import { ProxyTracer } from '@opentelemetry/api';
import * as opentelemetryInstrumentationPackage from '@opentelemetry/instrumentation';
import {
SDK_VERSION,
SessionFlusher,
Expand Down Expand Up @@ -495,6 +496,21 @@ describe('NodeClient', () => {
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(0);
});
});

it('registers instrumentations provided with `openTelemetryInstrumentations`', () => {
const registerInstrumentationsSpy = jest
.spyOn(opentelemetryInstrumentationPackage, 'registerInstrumentations')
.mockImplementationOnce(() => () => undefined);
const instrumentationsArray = ['foobar'] as unknown as opentelemetryInstrumentationPackage.Instrumentation[];

new NodeClient(getDefaultNodeClientOptions({ openTelemetryInstrumentations: instrumentationsArray }));

expect(registerInstrumentationsSpy).toHaveBeenCalledWith(
expect.objectContaining({
instrumentations: instrumentationsArray,
}),
);
});
});

describe('flush/close', () => {
Expand Down

0 comments on commit 09a31d1

Please sign in to comment.