-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(node-core): Add node-core otel v1 and v2 apps #17214
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
.vscode |
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,36 @@ | ||
{ | ||
"name": "node-core-express-otel-v1-custom-sampler", | ||
"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": { | ||
"@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", | ||
"@sentry/node-core": "latest || *", | ||
"@sentry/opentelemetry": "latest || *", | ||
"@types/express": "4.17.17", | ||
"@types/node": "^18.19.1", | ||
"express": "4.19.2", | ||
"typescript": "~5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "~1.53.2", | ||
"@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,51 @@ | ||
import './instrument'; | ||
|
||
import * as Sentry from '@sentry/node-core'; | ||
import express from 'express'; | ||
|
||
const PORT = 3030; | ||
const app = express(); | ||
|
||
const wait = (duration: number) => { | ||
return new Promise<void>(res => { | ||
setTimeout(() => res(), duration); | ||
}); | ||
}; | ||
|
||
app.get('/task', async (_req, res) => { | ||
await Sentry.startSpan({ name: 'Long task', op: 'custom.op' }, async () => { | ||
await wait(200); | ||
}); | ||
res.send('ok'); | ||
}); | ||
|
||
app.get('/unsampled/task', async (_req, res) => { | ||
await wait(200); | ||
res.send('ok'); | ||
}); | ||
|
||
app.get('/test-error', async function (req, res) { | ||
const exceptionId = Sentry.captureException(new Error('This is an error')); | ||
|
||
await Sentry.flush(2000); | ||
|
||
res.send({ exceptionId }); | ||
}); | ||
|
||
app.get('/test-exception/:id', function (req, _res) { | ||
throw new Error(`This is an exception with id ${req.params.id}`); | ||
}); | ||
|
||
app.use(function onError(err: unknown, req: any, res: any, next: any) { | ||
// Explicitly capture the error with Sentry | ||
Sentry.captureException(err); | ||
|
||
// 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'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
app.listen(PORT, () => { | ||
console.log('App listening on ', PORT); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Attributes, Context, Link, SpanKind } from '@opentelemetry/api'; | ||
import { Sampler, SamplingResult } from '@opentelemetry/sdk-trace-node'; | ||
import { wrapSamplingDecision } from '@sentry/opentelemetry'; | ||
|
||
export class CustomSampler implements Sampler { | ||
public shouldSample( | ||
context: Context, | ||
_traceId: string, | ||
_spanName: string, | ||
_spanKind: SpanKind, | ||
attributes: Attributes, | ||
_links: Link[], | ||
): SamplingResult { | ||
const route = attributes['http.route']; | ||
const target = attributes['http.target']; | ||
const decision = | ||
(typeof route === 'string' && route.includes('/unsampled')) || | ||
(typeof target === 'string' && target.includes('/unsampled')) | ||
? 0 | ||
: 1; | ||
return wrapSamplingDecision({ | ||
decision, | ||
context, | ||
spanAttributes: attributes, | ||
}); | ||
} | ||
|
||
public toString(): string { | ||
return CustomSampler.name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import * as Sentry from '@sentry/node-core'; | ||
import { SentryPropagator, SentrySpanProcessor } from '@sentry/opentelemetry'; | ||
import { CustomSampler } from './custom-sampler'; | ||
|
||
Sentry.init({ | ||
environment: 'qa', // dynamic sampling bias to keep transactions | ||
dsn: | ||
process.env.E2E_TEST_DSN || | ||
'https://3b6c388182fb435097f41d181be2b2ba@o4504321058471936.ingest.sentry.io/4504321066008576', | ||
chargome marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
includeLocalVariables: true, | ||
debug: !!process.env.DEBUG, | ||
tunnel: `http://localhost:3031/`, // proxy server | ||
tracesSampleRate: 1, | ||
openTelemetryInstrumentations: [new HttpInstrumentation()], | ||
}); | ||
|
||
const provider = new NodeTracerProvider({ | ||
sampler: new CustomSampler(), | ||
spanProcessors: [new SentrySpanProcessor()], | ||
}); | ||
|
||
provider.register({ | ||
propagator: new SentryPropagator(), | ||
contextManager: new Sentry.SentryContextManager(), | ||
}); | ||
|
||
Sentry.validateOpenTelemetrySetup(); |
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-custom-sampler', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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-custom-sampler', 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', | ||
}); | ||
|
||
// For node-core without Express integration, transaction name is the actual URL | ||
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}/), | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
||
test('Sends a sampled API route transaction', async ({ baseURL }) => { | ||
const transactionEventPromise = waitForTransaction('node-core-express-otel-v1-custom-sampler', transactionEvent => { | ||
return transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'GET /task'; | ||
}); | ||
|
||
await fetch(`${baseURL}/task`); | ||
|
||
const transactionEvent = await transactionEventPromise; | ||
|
||
expect(transactionEvent.contexts?.trace).toEqual({ | ||
span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
data: { | ||
'sentry.source': 'url', | ||
'sentry.op': 'http.server', | ||
'sentry.origin': 'manual', | ||
url: 'http://localhost:3030/task', | ||
'otel.kind': 'SERVER', | ||
'http.response.status_code': 200, | ||
'http.url': 'http://localhost:3030/task', | ||
'http.host': 'localhost:3030', | ||
'net.host.name': 'localhost', | ||
'http.method': 'GET', | ||
'http.scheme': 'http', | ||
'http.target': '/task', | ||
'http.user_agent': 'node', | ||
'http.flavor': '1.1', | ||
'net.transport': 'ip_tcp', | ||
'net.host.ip': expect.any(String), | ||
'net.host.port': 3030, | ||
'net.peer.ip': expect.any(String), | ||
'net.peer.port': expect.any(Number), | ||
'http.status_code': 200, | ||
'http.status_text': 'OK', | ||
}, | ||
origin: 'manual', | ||
op: 'http.server', | ||
status: 'ok', | ||
}); | ||
|
||
expect(transactionEvent.spans?.length).toBe(1); | ||
|
||
expect(transactionEvent.spans).toContainEqual({ | ||
span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
data: { | ||
'sentry.origin': 'manual', | ||
'sentry.op': 'custom.op', | ||
}, | ||
description: 'Long task', | ||
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
status: 'ok', | ||
op: 'custom.op', | ||
origin: 'manual', | ||
}); | ||
}); | ||
|
||
test('Does not send an unsampled API route transaction', async ({ baseURL }) => { | ||
const unsampledTransactionEventPromise = waitForTransaction( | ||
'node-core-express-otel-v1-custom-sampler', | ||
transactionEvent => { | ||
return ( | ||
transactionEvent?.contexts?.trace?.op === 'http.server' && | ||
transactionEvent?.transaction === 'GET /unsampled/task' | ||
); | ||
}, | ||
); | ||
|
||
await fetch(`${baseURL}/unsampled/task`); | ||
|
||
const promiseShouldNotResolve = () => | ||
new Promise<void>((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
resolve(); // Test passes because promise did not resolve within timeout | ||
}, 1000); | ||
|
||
unsampledTransactionEventPromise.then( | ||
() => { | ||
clearTimeout(timeout); | ||
reject(new Error('Promise should not have resolved')); | ||
}, | ||
() => { | ||
clearTimeout(timeout); | ||
reject(new Error('Promise should not have been rejected')); | ||
}, | ||
); | ||
}); | ||
|
||
expect(promiseShouldNotResolve()).resolves.not.toThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Incorrect Promise Assertion UsageThe test assertion Locations (2) |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["node"], | ||
"esModuleInterop": true, | ||
"lib": ["es2018"], | ||
"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,38 @@ | ||
{ | ||
"name": "node-core-express-otel-v1-sdk-node", | ||
"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": { | ||
"@opentelemetry/api": "^1.9.0", | ||
"@opentelemetry/context-async-hooks": "^1.30.1", | ||
"@opentelemetry/core": "^1.30.1", | ||
"@opentelemetry/instrumentation": "^0.57.2", | ||
"@opentelemetry/instrumentation-http": "^0.57.2", | ||
"@opentelemetry/resources": "^1.30.1", | ||
"@opentelemetry/sdk-trace-node": "^1.30.1", | ||
"@opentelemetry/semantic-conventions": "^1.30.0", | ||
"@opentelemetry/sdk-node": "^0.57.2", | ||
"@opentelemetry/exporter-trace-otlp-http": "^0.57.2", | ||
"@sentry/node-core": "latest || *", | ||
"@sentry/opentelemetry": "latest || *", | ||
"@types/express": "4.17.17", | ||
"@types/node": "^18.19.1", | ||
"express": "4.19.2", | ||
"typescript": "~5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "~1.53.2", | ||
"@sentry-internal/test-utils": "link:../../../test-utils" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
||
const config = getPlaywrightConfig( | ||
{ | ||
startCommand: `pnpm start`, | ||
}, | ||
{ | ||
webServer: [ | ||
{ | ||
command: `node ./start-event-proxy.mjs`, | ||
port: 3031, | ||
stdout: 'pipe', | ||
stderr: 'pipe', | ||
}, | ||
{ | ||
command: `node ./start-otel-proxy.mjs`, | ||
port: 3032, | ||
stdout: 'pipe', | ||
stderr: 'pipe', | ||
}, | ||
{ | ||
command: 'pnpm start', | ||
port: 3030, | ||
stdout: 'pipe', | ||
stderr: 'pipe', | ||
env: { | ||
PORT: 3030, | ||
}, | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
export default config; |
Uh oh!
There was an error while loading. Please reload this page.