Skip to content

feat(node-core): Add node-core package #16531

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

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
The table of contents is too big for display.
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,39 @@
{
"name": "node-core-express-app",
"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/node-core": "latest || *",
"@sentry/opentelemetry": "latest || *",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/context-async-hooks": "^1.30.1",
"@opentelemetry/core": "^1.30.1",
"@opentelemetry/instrumentation": "^0.57.1",
"@opentelemetry/instrumentation-http": "^0.57.1",
"@opentelemetry/resources": "^1.30.1",
"@opentelemetry/sdk-trace-node": "^1.30.1",
"@opentelemetry/semantic-conventions": "^1.30.0",
"@types/express": "^4.17.21",
"@types/node": "^18.19.1",
"express": "^4.21.2",
"typescript": "~5.0.0"
},
"devDependencies": {
"@playwright/test": "~1.50.0",
"@sentry-internal/test-utils": "link:../../../test-utils"
},
"resolutions": {
"@types/qs": "6.9.17"
},
"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,57 @@
// Import this first!
import './instrument';

// Now import other modules
import * as Sentry from '@sentry/node-core';
import express from 'express';

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

app.get('/test-transaction', function (req, res) {
Sentry.withActiveSpan(null, async () => {
Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => {
Sentry.startSpan({ name: 'test-span' }, () => undefined);
});

await Sentry.flush();

res.send({
transactionIds: global.transactionIds || [],
});
});
});

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

app.get('/test-local-variables-caught', function (req, res) {
const randomVariableToRecord = Math.random();

let exceptionId: string;
try {
throw new Error('Local Variable Error');
} catch (e) {
exceptionId = Sentry.captureException(e);
}

res.send({ exceptionId, randomVariableToRecord });
});

// @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,46 @@
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import * as Sentry from '@sentry/node-core';
import { SentrySpanProcessor, SentryPropagator, SentrySampler } from '@sentry/opentelemetry';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';

declare global {
namespace globalThis {
var transactionIds: string[];
}
}

const sentryClient = 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,
openTelemetryInstrumentations: [new HttpInstrumentation()],
});

const provider = new NodeTracerProvider({
sampler: sentryClient ? new SentrySampler(sentryClient) : undefined,
spanProcessors: [new SentrySpanProcessor()],
});

provider.register({
propagator: new SentryPropagator(),
contextManager: new Sentry.SentryContextManager(),
});

Sentry.validateOpenTelemetrySetup();

Sentry.addEventProcessor(event => {
global.transactionIds = global.transactionIds || [];

if (event.type === 'transaction') {
const eventId = event.event_id;

if (eventId) {
global.transactionIds.push(eventId);
}
}

return event;
});
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-core-express-otel-v1',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';

test('Sends correct error event', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-core-express-otel-v1', 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.request).toEqual({
method: 'GET',
cookies: {},
headers: expect.any(Object),
url: 'http://localhost:3030/test-exception/123',
});

expect(errorEvent.transaction).toEqual('GET /test-exception/123');

expect(errorEvent.contexts?.trace).toEqual({
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
});
});

test('Should record caught exceptions with local variable', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-core-express-otel-v1', event => {
return event.transaction === 'GET /test-local-variables-caught';
});

await fetch(`${baseURL}/test-local-variables-caught`);

const errorEvent = await errorEventPromise;

const frames = errorEvent.exception?.values?.[0].stacktrace?.frames;
expect(frames?.[frames.length - 1].vars?.randomVariableToRecord).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('Sends an API route transaction', async ({ baseURL }) => {
const pageloadTransactionEventPromise = waitForTransaction('node-core-express-otel-v1', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent?.transaction === 'GET /test-transaction'
);
});

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

const transactionEvent = await pageloadTransactionEventPromise;

expect(transactionEvent.contexts?.trace).toEqual({
data: {
'sentry.source': 'url',
'sentry.origin': 'manual',
'sentry.op': 'http.server',
'sentry.sample_rate': 1,
url: 'http://localhost:3030/test-transaction',
'otel.kind': 'SERVER',
'http.response.status_code': 200,
'http.url': 'http://localhost:3030/test-transaction',
'http.host': 'localhost:3030',
'net.host.name': 'localhost',
'http.method': 'GET',
'http.scheme': 'http',
'http.target': '/test-transaction',
'http.user_agent': 'node',
'http.flavor': '1.1',
'net.transport': 'ip_tcp',
'net.host.ip': expect.any(String),
'net.host.port': expect.any(Number),
'net.peer.ip': expect.any(String),
'net.peer.port': expect.any(Number),
'http.status_code': 200,
'http.status_text': 'OK',
},
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
origin: 'manual',
});

expect(transactionEvent.contexts?.response).toEqual({
status_code: 200,
});

expect(transactionEvent).toEqual(
expect.objectContaining({
transaction: 'GET /test-transaction',
type: 'transaction',
transaction_info: {
source: 'url',
},
}),
);
});

test('Sends an API route transaction for an errored route', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-core-express-otel-v1', transactionEvent => {
return (
transactionEvent.contexts?.trace?.op === 'http.server' &&
transactionEvent.transaction === 'GET /test-exception/777' &&
transactionEvent.request?.url === 'http://localhost:3030/test-exception/777'
);
});

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

const transactionEvent = await transactionEventPromise;

expect(transactionEvent.contexts?.trace?.op).toEqual('http.server');
expect(transactionEvent.transaction).toEqual('GET /test-exception/777');
expect(transactionEvent.contexts?.trace?.status).toEqual('internal_error');
expect(transactionEvent.contexts?.trace?.data?.['http.status_code']).toEqual(500);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"types": ["node"],
"esModuleInterop": true,
"lib": ["es2020"],
"strict": true,
"outDir": "dist",
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
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,39 @@
{
"name": "node-core-express-otel-v2-app",
"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/node-core": "latest || *",
"@sentry/opentelemetry": "latest || *",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/context-async-hooks": "^2.0.0",
"@opentelemetry/core": "^2.0.0",
"@opentelemetry/instrumentation": "^0.200.0",
"@opentelemetry/instrumentation-http": "^0.200.0",
"@opentelemetry/resources": "^2.0.0",
"@opentelemetry/sdk-trace-node": "^2.0.0",
"@opentelemetry/semantic-conventions": "^1.30.0",
"@types/express": "^4.17.21",
"@types/node": "^18.19.1",
"express": "^4.21.2",
"typescript": "~5.0.0"
},
"devDependencies": {
"@playwright/test": "~1.50.0",
"@sentry-internal/test-utils": "link:../../../test-utils"
},
"resolutions": {
"@types/qs": "6.9.17"
},
"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;
Loading
Loading