Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Add openTelemetryInstrumentations option #14484

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ServerRuntimeClientOptions } from '@sentry/core';
import { SDK_VERSION, ServerRuntimeClient, applySdkMetadata } from '@sentry/core';
Expand All @@ -26,6 +27,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
Loading