Skip to content
Closed
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
Expand Up @@ -4,6 +4,15 @@ import cors from 'cors';
import express from 'express';
import http from 'http';

function getOutgoingHeaders(options) {
const request = http.get(options);
const headers = request.getHeaders();
// Only header construction is under test; don't leave requests to the placeholder host running.
request.on('error', () => {});
request.destroy();
return headers;
}

const app = express();

// Set globally so we can assert user data is not leaked into propagated baggage.
Expand All @@ -13,7 +22,7 @@ app.use(cors());

// Plain outgoing request — used to assert propagated `sentry-trace`/`baggage` headers.
app.get('/test/express', (_req, res) => {
const headers = http.get('http://somewhere.not.sentry/').getHeaders();
const headers = getOutgoingHeaders('http://somewhere.not.sentry/');
res.send({ test_data: headers });
});

Expand All @@ -22,7 +31,7 @@ app.get('/test/express', (_req, res) => {
app.get('/test/express-replace-trace-id', (_req, res) => {
const span = Sentry.getActiveSpan();
const traceId = span?.spanContext().traceId;
const headers = http.get('http://somewhere.not.sentry/').getHeaders();
const headers = getOutgoingHeaders('http://somewhere.not.sentry/');
if (traceId) {
headers['baggage'] = headers['baggage'].replace(traceId, '__SENTRY_TRACE_ID__');
}
Expand All @@ -32,32 +41,31 @@ app.get('/test/express-replace-trace-id', (_req, res) => {
// Sets a third-party baggage header on the outgoing request, which the SDK should
// merge with the Sentry DSC entries.
app.get('/test/express-third-party-baggage', (_req, res) => {
const headers = http
.get({ hostname: 'somewhere.not.sentry', headers: { baggage: 'other=vendor,foo=bar,third=party' } })
.getHeaders();
const headers = getOutgoingHeaders({
hostname: 'somewhere.not.sentry',
headers: { baggage: 'other=vendor,foo=bar,third=party' },
});
res.send({ test_data: headers });
});

// Sets a third-party baggage header that also contains sentry-* entries, which the
// SDK should ignore/overwrite with the actual DSC.
app.get('/test/express-third-party-baggage-with-sentry', (_req, res) => {
const headers = http
.get({
hostname: 'somewhere.not.sentry',
headers: {
baggage:
'other=vendor,foo=bar,third=party,sentry-release=9.9.9,sentry-environment=staging,sentry-sample_rate=0.54,last=item',
},
})
.getHeaders();
const headers = getOutgoingHeaders({
hostname: 'somewhere.not.sentry',
headers: {
baggage:
'other=vendor,foo=bar,third=party,sentry-release=9.9.9,sentry-environment=staging,sentry-sample_rate=0.54,last=item',
},
});
res.send({ test_data: headers });
});

// Forwards the incoming baggage header to the outgoing request, to assert that
// property values with `=` signs are preserved during parsing & re-serialization.
app.get('/test/express-property-values', (req, res) => {
const incomingBaggage = req.headers.baggage;
const headers = http.get({ hostname: 'somewhere.not.sentry', headers: { baggage: incomingBaggage } }).getHeaders();
const headers = getOutgoingHeaders({ hostname: 'somewhere.not.sentry', headers: { baggage: incomingBaggage } });
res.send({ test_data: headers });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ describe('express sentry-trace', () => {
});

createCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
let started: ReturnType<ReturnType<typeof createRunner>['start']> | undefined;
// These cases vary request headers, so the same initialized server can handle each one.
const getRunner = () => (started ??= createRunner().start());

describe('baggage header assignment', () => {
test('Should overwrite baggage if the incoming request already has Sentry baggage data but no sentry-trace', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -30,7 +34,7 @@ describe('express sentry-trace', () => {
});

test('Should propagate sentry trace baggage data from an incoming to an outgoing request.', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -49,7 +53,7 @@ describe('express sentry-trace', () => {
});

test('Should not propagate baggage data from an incoming to an outgoing request if sentry-trace is faulty.', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -68,7 +72,7 @@ describe('express sentry-trace', () => {
});

test('Should not propagate baggage if sentry-trace header is present in incoming request but no baggage header', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -85,7 +89,7 @@ describe('express sentry-trace', () => {
});

test('Should not propagate baggage and ignore original 3rd party baggage entries if sentry-trace header is present', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -103,7 +107,7 @@ describe('express sentry-trace', () => {
});

test('Should populate and propagate sentry baggage if sentry-trace header does not exist', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express');

Expand All @@ -126,7 +130,7 @@ describe('express sentry-trace', () => {
});

test('Should populate Sentry and ignore 3rd party content if sentry-trace header does not exist', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -153,7 +157,7 @@ describe('express sentry-trace', () => {
});

test('should attach a baggage header to an outgoing request.', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express-replace-trace-id');

Expand Down Expand Up @@ -182,7 +186,7 @@ describe('express sentry-trace', () => {
});

test('should merge `baggage` header of a third party vendor with the Sentry DSC baggage items', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express-third-party-baggage', {
headers: {
Expand All @@ -203,7 +207,7 @@ describe('express sentry-trace', () => {

describe('third party baggage with sentry entries', () => {
test('should ignore sentry-values in `baggage` header of a third party vendor and overwrite them with incoming DSC', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>(
'get',
Expand Down Expand Up @@ -238,7 +242,7 @@ describe('express sentry-trace', () => {
});

test('should ignore sentry-values in `baggage` header of a third party vendor and overwrite them with new DSC', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>(
'get',
Expand Down Expand Up @@ -278,7 +282,7 @@ describe('express sentry-trace', () => {
});

test('should preserve baggage property values with equal signs (W3C spec compliance)', async () => {
const runner = createRunner().start();
const runner = getRunner();

// W3C spec example: https://www.w3.org/TR/baggage/#example
const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express-property-values', {
Expand All @@ -299,7 +303,7 @@ describe('express sentry-trace', () => {
});

test('Includes transaction in baggage if the transaction name is parameterized', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express');

Expand All @@ -313,7 +317,7 @@ describe('express sentry-trace', () => {
});

test('Should assign `sentry-trace` header which sets parent trace id of an outgoing request.', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express', {
headers: {
Expand All @@ -333,7 +337,7 @@ describe('express sentry-trace', () => {
});

test('should attach a `sentry-trace` header to an outgoing request.', async () => {
const runner = createRunner().start();
const runner = getRunner();

const response = await runner.makeRequest<TestAPIResponse>('get', '/test/express');

Expand Down
Loading
Loading