Skip to content

Commit 444c1d1

Browse files
author
Luca Forstner
committed
feat(node): Add openTelemetryInstrumentations option
1 parent 91a4985 commit 444c1d1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-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 { ServerRuntimeClientOptions } from '@sentry/core';
67
import { SDK_VERSION, ServerRuntimeClient, applySdkMetadata } from '@sentry/core';
@@ -26,6 +27,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
2627
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
2728
};
2829

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

3138
logger.log(

packages/node/src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export interface BaseNodeOptions {
9090
*/
9191
skipOpenTelemetrySetup?: boolean;
9292

93+
/**
94+
* Provide an array of OpenTelemetry Instrumentations that should be registered.
95+
*
96+
* Use this option if you want to register OpenTelemetry instrumentation that the Sentry SDK does not yet have support for.
97+
*/
98+
openTelemetryInstrumentations?: Instrumentation[];
99+
93100
/**
94101
* The max. duration in seconds that the SDK will wait for parent spans to be finished before discarding a span.
95102
* The SDK will automatically clean up spans that have no finished parent after this duration.
@@ -156,7 +163,7 @@ export interface CurrentScopes {
156163
* The base `Span` type is basically a `WriteableSpan`.
157164
* There are places where we basically want to allow passing _any_ span,
158165
* 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.
166+
* You'll have to make sur to check relevant fields before accessing them.
160167
*
161168
* Note that technically, the `Span` exported from `@opentelemetry/sdk-trace-base` matches this,
162169
* 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)