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 context info for missing instrumentation #12639

Merged
merged 5 commits into from
Jun 26, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@sentry:registry=http://127.0.0.1:4873
@sentry-internal:registry=http://127.0.0.1:4873
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "node-express-incorrect-instrumentation",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "tsc",
"start": "node dist/app.js",
"test": "playwright test",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"test:build": "pnpm install && pnpm build",
"test:assert": "pnpm test"
},
"dependencies": {
"@sentry/core": "latest || *",
"@sentry/node": "latest || *",
"@sentry/types": "latest || *",
"@trpc/server": "10.45.2",
"@trpc/client": "10.45.2",
"@types/express": "4.17.17",
"@types/node": "18.15.1",
"express": "4.19.2",
"typescript": "4.9.5",
"zod": "~3.22.4"
},
"devDependencies": {
"@playwright/test": "^1.44.1",
"@sentry-internal/test-utils": "link:../../../test-utils"
},
"volta": {
"extends": "../../package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getPlaywrightConfig } from '@sentry-internal/test-utils';

const config = getPlaywrightConfig({
startCommand: `pnpm start`,
});

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
declare global {
namespace globalThis {
var transactionIds: string[];
}
}

import express from 'express';

const app = express();
const port = 3030;

// import and init sentry last for missing instrumentation
chargome marked this conversation as resolved.
Show resolved Hide resolved
import * as Sentry from '@sentry/node';
Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
includeLocalVariables: true,
debug: !!process.env.DEBUG,
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1,
});

app.get('/test-exception/:id', function (req, _res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});

Sentry.setupExpressErrorHandler(app);

// @ts-ignore
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'node-express',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';

test('Sends correct context when instrumentation was set up incorrectly', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-express', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

await fetch(`${baseURL}/test-exception/123`);

const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

expect(errorEvent.contexts?.missing_instrumentation).toEqual({
package: 'express',
'javascript.is_cjs': true,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"types": ["node"],
"esModuleInterop": true,
"lib": ["es2018"],
"strict": true,
"outDir": "dist"
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MissingInstrumentationContext } from '@sentry/types';
import { isCjs } from './commonjs';

export const createMissingInstrumentationContext = (pkg: string): MissingInstrumentationContext => ({
package: pkg,
'javascript.is_cjs': isCjs(),
});
5 changes: 4 additions & 1 deletion packages/node/src/utils/ensureIsWrapped.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isWrapped } from '@opentelemetry/core';
import { hasTracingEnabled, isEnabled } from '@sentry/core';
import { getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
import { consoleSandbox } from '@sentry/utils';
import { isCjs } from './commonjs';
import { createMissingInstrumentationContext } from './createMissingInstrumentationContext';

/**
* Checks and warns if a framework isn't wrapped by opentelemetry.
Expand All @@ -24,5 +25,7 @@ export function ensureIsWrapped(
);
}
});

getGlobalScope().setContext('missing_instrumentation', createMissingInstrumentationContext(name));
}
}
5 changes: 5 additions & 0 deletions packages/types/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ export interface CloudResourceContext extends Record<string, unknown> {
export interface ProfileContext extends Record<string, unknown> {
profile_id: string;
}

export interface MissingInstrumentationContext extends Record<string, unknown> {
package: string;
['javascript.is_cjs']?: boolean;
}
andreiborza marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
CultureContext,
TraceContext,
CloudResourceContext,
MissingInstrumentationContext,
} from './context';
export type { DataCategory } from './datacategory';
export type { DsnComponents, DsnLike, DsnProtocol } from './dsn';
Expand Down
Loading