Skip to content

feat(opentelemetry): Add addOpenTelemetryInstrumentation #11667

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

Merged
merged 1 commit into from
Apr 18, 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
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export {
hapiIntegration,
setupHapiErrorHandler,
spotlightIntegration,
addOpenTelemetryInstrumentation,
} from '@sentry/node';

// We can still leave this for the carrier init and type exports
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export {
spanToJSON,
spanToTraceHeader,
trpcMiddleware,
addOpenTelemetryInstrumentation,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export {
spanToJSON,
spanToTraceHeader,
trpcMiddleware,
addOpenTelemetryInstrumentation,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export {
spanToJSON,
spanToTraceHeader,
trpcMiddleware,
addOpenTelemetryInstrumentation,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type { NodeOptions } from './types';
export { addRequestDataToEvent, DEFAULT_USER_INCLUDES, extractRequestData } from '@sentry/utils';

export {
addOpenTelemetryInstrumentation,
// These are custom variants that need to be used instead of the core one
// As they have slightly different implementations
continueTrace,
Expand Down
10 changes: 3 additions & 7 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';

import {
addBreadcrumb,
Expand Down Expand Up @@ -52,7 +52,7 @@ const _httpIntegration = ((options: HttpOptions = {}) => {
return {
name: 'Http',
setupOnce() {
const instrumentations = [
addOpenTelemetryInstrumentation(
new HttpInstrumentation({
ignoreOutgoingRequestHook: request => {
const url = getRequestUrl(request);
Expand Down Expand Up @@ -141,11 +141,7 @@ const _httpIntegration = ((options: HttpOptions = {}) => {
}
},
}),
];

registerInstrumentations({
instrumentations,
});
);
},
};
}) satisfies IntegrationFn;
Expand Down
43 changes: 20 additions & 23 deletions packages/node/src/integrations/node-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import type { Instrumentation } from '@opentelemetry/instrumentation';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { addBreadcrumb, defineIntegration } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import { getRequestSpanData, getSpanKind } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import { NODE_MAJOR } from '../nodeVersion';

import type { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node';

import { addOriginToSpan } from '../utils/addOriginToSpan';

interface NodeFetchOptions {
Expand All @@ -29,7 +30,7 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
const _breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;

async function getInstrumentation(): Promise<[Instrumentation] | void> {
async function getInstrumentation(): Promise<FetchInstrumentation | void> {
// Only add NodeFetch if Node >= 18, as previous versions do not support it
if (NODE_MAJOR < 18) {
DEBUG_BUILD && logger.log('NodeFetch is not supported on Node < 18, skipping instrumentation...');
Expand All @@ -38,22 +39,20 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {

try {
const pkg = await import('opentelemetry-instrumentation-fetch-node');
return [
new pkg.FetchInstrumentation({
ignoreRequestHook: (request: { origin?: string }) => {
const url = request.origin;
return _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url);
},
onRequest: ({ span }: { span: Span }) => {
_updateSpan(span);
return new pkg.FetchInstrumentation({
ignoreRequestHook: (request: { origin?: string }) => {
const url = request.origin;
return _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url);
},
onRequest: ({ span }: { span: Span }) => {
_updateSpan(span);

if (_breadcrumbs) {
_addRequestBreadcrumb(span);
}
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
];
if (_breadcrumbs) {
_addRequestBreadcrumb(span);
}
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
} catch (error) {
// Could not load instrumentation
DEBUG_BUILD && logger.log('Could not load NodeFetch instrumentation.');
Expand All @@ -64,11 +63,9 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
name: 'NodeFetch',
setupOnce() {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
getInstrumentation().then(instrumentations => {
if (instrumentations) {
registerInstrumentations({
instrumentations,
});
getInstrumentation().then(instrumentation => {
if (instrumentation) {
addOpenTelemetryInstrumentation(instrumentation);
}
});
},
Expand Down
8 changes: 4 additions & 4 deletions packages/node/src/integrations/tracing/connect.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect';
import { captureException, defineIntegration } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';

type ConnectApp = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
use: (middleware: any) => void;
};

const _connectIntegration = (() => {
return {
name: 'Connect',
setupOnce() {
registerInstrumentations({
instrumentations: [new ConnectInstrumentation({})],
});
addOpenTelemetryInstrumentation(new ConnectInstrumentation({}));
},
};
}) satisfies IntegrationFn;

export const connectIntegration = defineIntegration(_connectIntegration);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function connectErrorMiddleware(err: any, req: any, res: any, next: any): void {
captureException(err);
next(err);
Expand Down
44 changes: 21 additions & 23 deletions packages/node/src/integrations/tracing/express.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as http from 'http';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { defineIntegration, getDefaultIsolationScope } from '@sentry/core';
import { captureException, getClient, getIsolationScope } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';

import { logger } from '@sentry/utils';
Expand All @@ -14,29 +14,27 @@ const _expressIntegration = (() => {
return {
name: 'Express',
setupOnce() {
registerInstrumentations({
instrumentations: [
new ExpressInstrumentation({
requestHook(span) {
addOriginToSpan(span, 'auto.http.otel.express');
},
spanNameHook(info, defaultName) {
if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is still default isolation scope - skipping setting transactionName');
return defaultName;
}
if (info.layerType === 'request_handler') {
// type cast b/c Otel unfortunately types info.request as any :(
const req = info.request as { method?: string };
const method = req.method ? req.method.toUpperCase() : 'GET';
getIsolationScope().setTransactionName(`${method} ${info.route}`);
}
addOpenTelemetryInstrumentation(
new ExpressInstrumentation({
requestHook(span) {
addOriginToSpan(span, 'auto.http.otel.express');
},
spanNameHook(info, defaultName) {
if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is still default isolation scope - skipping setting transactionName');
return defaultName;
},
}),
],
});
}
if (info.layerType === 'request_handler') {
// type cast b/c Otel unfortunately types info.request as any :(
const req = info.request as { method?: string };
const method = req.method ? req.method.toUpperCase() : 'GET';
getIsolationScope().setTransactionName(`${method} ${info.route}`);
}
return defaultName;
},
}),
);
},
};
}) satisfies IntegrationFn;
Expand Down
18 changes: 8 additions & 10 deletions packages/node/src/integrations/tracing/fastify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
import { captureException, defineIntegration, getIsolationScope } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';

import { addOriginToSpan } from '../../utils/addOriginToSpan';
Expand All @@ -9,15 +9,13 @@ const _fastifyIntegration = (() => {
return {
name: 'Fastify',
setupOnce() {
registerInstrumentations({
instrumentations: [
new FastifyInstrumentation({
requestHook(span) {
addOriginToSpan(span, 'auto.http.otel.fastify');
},
}),
],
});
addOpenTelemetryInstrumentation(
new FastifyInstrumentation({
requestHook(span) {
addOriginToSpan(span, 'auto.http.otel.fastify');
},
}),
);
},
};
}) satisfies IntegrationFn;
Expand Down
20 changes: 9 additions & 11 deletions packages/node/src/integrations/tracing/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
import { defineIntegration } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';

import { addOriginToSpan } from '../../utils/addOriginToSpan';
Expand All @@ -9,16 +9,14 @@ const _graphqlIntegration = (() => {
return {
name: 'Graphql',
setupOnce() {
registerInstrumentations({
instrumentations: [
new GraphQLInstrumentation({
ignoreTrivialResolveSpans: true,
responseHook(span) {
addOriginToSpan(span, 'auto.graphql.otel.graphql');
},
}),
],
});
addOpenTelemetryInstrumentation(
new GraphQLInstrumentation({
ignoreTrivialResolveSpans: true,
responseHook(span) {
addOriginToSpan(span, 'auto.graphql.otel.graphql');
},
}),
);
},
};
}) satisfies IntegrationFn;
Expand Down
6 changes: 2 additions & 4 deletions packages/node/src/integrations/tracing/hapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
import {
SDK_VERSION,
Expand All @@ -10,6 +9,7 @@ import {
getIsolationScope,
getRootSpan,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../../debug-build';
Expand All @@ -19,9 +19,7 @@ const _hapiIntegration = (() => {
return {
name: 'Hapi',
setupOnce() {
registerInstrumentations({
instrumentations: [new HapiInstrumentation()],
});
addOpenTelemetryInstrumentation(new HapiInstrumentation());
},
};
}) satisfies IntegrationFn;
Expand Down
39 changes: 19 additions & 20 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import {
Expand All @@ -8,6 +7,7 @@ import {
getIsolationScope,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../debug-build';
Expand All @@ -16,31 +16,30 @@ const _koaIntegration = (() => {
return {
name: 'Koa',
setupOnce() {
registerInstrumentations({
instrumentations: [
new KoaInstrumentation({
requestHook(span, info) {
if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
return;
}
const attributes = spanToJSON(span).data;
const route = attributes && attributes[SEMATTRS_HTTP_ROUTE];
const method = info.context.request.method.toUpperCase() || 'GET';
if (route) {
getIsolationScope().setTransactionName(`${method} ${route}`);
}
},
}),
],
});
addOpenTelemetryInstrumentation(
new KoaInstrumentation({
requestHook(span, info) {
if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
return;
}
const attributes = spanToJSON(span).data;
const route = attributes && attributes[SEMATTRS_HTTP_ROUTE];
const method = info.context.request.method.toUpperCase() || 'GET';
if (route) {
getIsolationScope().setTransactionName(`${method} ${route}`);
}
},
}),
);
},
};
}) satisfies IntegrationFn;

export const koaIntegration = defineIntegration(_koaIntegration);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) => Promise<void>) => void }): void => {
app.use(async (ctx, next) => {
try {
Expand Down
Loading
Loading