Skip to content

Commit

Permalink
test(node): Add integration test to demonstrate sample rate propagati…
Browse files Browse the repository at this point in the history
…on of incoming trace (#14788)
  • Loading branch information
Lms24 authored Dec 19, 2024
1 parent 61660df commit a2562e2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
// disable attaching headers to /test/* endpoints
tracePropagationTargets: [/^(?!.*test).*$/],
tracesSampleRate: 1.0,
transport: loggingTransport,
});

// express must be required after Sentry is initialized
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');

const app = express();

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.raw());

app.get('/test', (req, res) => {
res.send({ headers: req.headers });
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';

describe('tracesSampleRate propagation', () => {
afterAll(() => {
cleanupChildProcesses();
});

const traceId = '12345678123456781234567812345678';

test('uses sample rate from incoming baggage header in trace envelope item', done => {
createRunner(__dirname, 'server.js')
.expectHeader({
transaction: {
trace: {
sample_rate: '0.05',
sampled: 'true',
trace_id: traceId,
transaction: 'myTransaction',
},
},
})
.start(done)
.makeRequest('get', '/test', {
headers: {
'sentry-trace': `${traceId}-1234567812345678-1`,
baggage: `sentry-sample_rate=0.05,sentry-trace_id=${traceId},sentry-sampled=true,sentry-transaction=myTransaction`,
},
});
});
});

0 comments on commit a2562e2

Please sign in to comment.