Skip to content

Commit 09a31d1

Browse files
authored
feat(node): Add openTelemetryInstrumentations option (#14484)
1 parent 23e3783 commit 09a31d1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

packages/node/src/sdk/client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as os from 'node:os';
22
import type { Tracer } from '@opentelemetry/api';
33
import { trace } from '@opentelemetry/api';
4+
import { registerInstrumentations } from '@opentelemetry/instrumentation';
45
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
56
import type { Scope, ServerRuntimeClientOptions } from '@sentry/core';
67
import { SDK_VERSION, ServerRuntimeClient, applySdkMetadata, logger } from '@sentry/core';
@@ -27,6 +28,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
2728
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
2829
};
2930

31+
if (options.openTelemetryInstrumentations) {
32+
registerInstrumentations({
33+
instrumentations: options.openTelemetryInstrumentations,
34+
});
35+
}
36+
3037
applySdkMetadata(clientOptions, 'node');
3138

3239
logger.log(

packages/node/src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Span as WriteableSpan } from '@opentelemetry/api';
2+
import type { Instrumentation } from '@opentelemetry/instrumentation';
23
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
34
import type { ClientOptions, Options, SamplingContext, Scope, Span, TracePropagationTargets } from '@sentry/types';
45

@@ -90,6 +91,13 @@ export interface BaseNodeOptions {
9091
*/
9192
skipOpenTelemetrySetup?: boolean;
9293

94+
/**
95+
* Provide an array of OpenTelemetry Instrumentations that should be registered.
96+
*
97+
* Use this option if you want to register OpenTelemetry instrumentation that the Sentry SDK does not yet have support for.
98+
*/
99+
openTelemetryInstrumentations?: Instrumentation[];
100+
93101
/**
94102
* The max. duration in seconds that the SDK will wait for parent spans to be finished before discarding a span.
95103
* The SDK will automatically clean up spans that have no finished parent after this duration.
@@ -156,7 +164,7 @@ export interface CurrentScopes {
156164
* The base `Span` type is basically a `WriteableSpan`.
157165
* There are places where we basically want to allow passing _any_ span,
158166
* so in these cases we type this as `AbstractSpan` which could be either a regular `Span` or a `ReadableSpan`.
159-
* You'll have to make sur to check revelant fields before accessing them.
167+
* You'll have to make sur to check relevant fields before accessing them.
160168
*
161169
* Note that technically, the `Span` exported from `@opentelemetry/sdk-trace-base` matches this,
162170
* but we cannot be 100% sure that we are actually getting such a span, so this type is more defensive.

packages/node/test/sdk/client.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as os from 'os';
22
import { ProxyTracer } from '@opentelemetry/api';
3+
import * as opentelemetryInstrumentationPackage from '@opentelemetry/instrumentation';
34
import {
45
SDK_VERSION,
56
SessionFlusher,
@@ -495,6 +496,21 @@ describe('NodeClient', () => {
495496
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(0);
496497
});
497498
});
499+
500+
it('registers instrumentations provided with `openTelemetryInstrumentations`', () => {
501+
const registerInstrumentationsSpy = jest
502+
.spyOn(opentelemetryInstrumentationPackage, 'registerInstrumentations')
503+
.mockImplementationOnce(() => () => undefined);
504+
const instrumentationsArray = ['foobar'] as unknown as opentelemetryInstrumentationPackage.Instrumentation[];
505+
506+
new NodeClient(getDefaultNodeClientOptions({ openTelemetryInstrumentations: instrumentationsArray }));
507+
508+
expect(registerInstrumentationsSpy).toHaveBeenCalledWith(
509+
expect.objectContaining({
510+
instrumentations: instrumentationsArray,
511+
}),
512+
);
513+
});
498514
});
499515

500516
describe('flush/close', () => {

0 commit comments

Comments
 (0)