Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -22,3 +22,31 @@ test('App router transactions should be attached to the pageload request span',
expect(pageloadTraceId).toBeTruthy();
expect(serverTransaction.contexts?.trace?.trace_id).toBe(pageloadTraceId);
});

test('extracts HTTP request headers as span attributes', async ({ baseURL }) => {
const serverTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === 'GET /pageload-tracing';
});

await fetch(`${baseURL}/pageload-tracing`, {
headers: {
'User-Agent': 'Custom-NextJS-Agent/15.0',
'Content-Type': 'text/html',
'X-NextJS-Test': 'nextjs-header-value',
Accept: 'text/html, application/xhtml+xml',
'X-Framework': 'Next.js',
},
});

const serverTransaction = await serverTransactionPromise;

expect(serverTransaction.contexts?.trace?.data).toEqual(
expect.objectContaining({
'http.request.header.user_agent': ['Custom-NextJS-Agent/15.0'],
'http.request.header.content_type': ['text/html'],
'http.request.header.x_nextjs_test': ['nextjs-header-value'],
'http.request.header.accept': ['text/html, application/xhtml+xml'],
'http.request.header.x_framework': ['Next.js'],
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
const transactionEvent = await pageloadTransactionEventPromise;

expect(transactionEvent.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -38,7 +38,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-transaction',
},
}),
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
Expand Down Expand Up @@ -208,3 +208,34 @@ test('Sends an API route transaction for an errored route', async ({ baseURL })
measurements: {},
});
});

test('Extracts HTTP request headers as span attributes', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-express', transactionEvent => {
return (
transactionEvent?.contexts?.trace?.op === 'http.server' &&
transactionEvent?.transaction === 'GET /test-transaction'
);
});

await fetch(`${baseURL}/test-transaction`, {
headers: {
'User-Agent': 'Custom-Agent/1.0 (Test)',
'Content-Type': 'application/json',
'X-Custom-Header': 'test-value',
Accept: 'application/json, text/plain',
Authorization: 'Bearer test-token-123',
},
});

const transactionEvent = await transactionEventPromise;

expect(transactionEvent.contexts?.trace?.data).toEqual(
expect.objectContaining({
'http.request.header.user_agent': ['Custom-Agent/1.0 (Test)'],
'http.request.header.content_type': ['application/json'],
'http.request.header.x_custom_header': ['test-value'],
'http.request.header.accept': ['application/json, text/plain'],
'http.request.header.authorization': ['Bearer test-token-123'],
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
);

expect(outboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -75,7 +75,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-outgoing-http/:id',
},
}),
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
Expand All @@ -84,7 +84,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
});

expect(inboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -106,7 +106,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-inbound-headers/:id',
},
}),
op: 'http.server',
parent_span_id: outgoingHttpSpanId,
span_id: expect.stringMatching(/[a-f0-9]{16}/),
Expand Down Expand Up @@ -165,7 +165,7 @@ test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
);

expect(outboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -189,7 +189,7 @@ test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-outgoing-fetch/:id',
},
}),
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
);

expect(outboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -74,7 +74,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-outgoing-http/:id',
},
}),
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
Expand All @@ -83,7 +83,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
});

expect(inboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -105,7 +105,7 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-inbound-headers/:id',
},
}),
op: 'http.server',
parent_span_id: outgoingHttpSpanId,
span_id: expect.stringMatching(/[a-f0-9]{16}/),
Expand Down Expand Up @@ -164,7 +164,7 @@ test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
);

expect(outboundTransaction.contexts?.trace).toEqual({
data: {
data: expect.objectContaining({
'sentry.source': 'route',
'sentry.origin': 'auto.http.otel.http',
'sentry.op': 'http.server',
Expand All @@ -188,7 +188,7 @@ test('Propagates trace for outgoing fetch requests', async ({ baseURL }) => {
'http.status_code': 200,
'http.status_text': 'OK',
'http.route': '/test-outgoing-fetch/:id',
},
}),
op: 'http.server',
span_id: expect.stringMatching(/[a-f0-9]{16}/),
status: 'ok',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,31 @@ test('does not send transactions for build asset folder "_nuxt"', async ({ page

expect(transactionEvent.transaction).toBe('GET /test-param/:param()');
});

test('extracts HTTP request headers as span attributes', async ({ baseURL }) => {
const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => {
return transactionEvent.transaction.includes('GET /api/test-param/');
});

await fetch(`${baseURL}/api/test-param/headers-test`, {
headers: {
'User-Agent': 'Custom-Nuxt-Agent/3.0',
'Content-Type': 'application/json',
'X-Nuxt-Test': 'nuxt-header-value',
Accept: 'application/json, text/html',
'X-Framework': 'Nuxt',
},
});

const transaction = await transactionPromise;

expect(transaction.contexts?.trace?.data).toEqual(
expect.objectContaining({
'http.request.header.user_agent': ['Custom-Nuxt-Agent/3.0'],
'http.request.header.content_type': ['application/json'],
'http.request.header.x_nuxt_test': ['nuxt-header-value'],
'http.request.header.accept': ['application/json, text/html'],
'http.request.header.x_framework': ['Nuxt'],
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ test('server pageload request span has nested request span for sub request', asy
]),
);
});

test('extracts HTTP request headers as span attributes', async ({ page, baseURL }) => {
const serverTxnEventPromise = waitForTransaction('sveltekit-2', txnEvent => {
return txnEvent?.transaction === 'GET /api/users';
});

await fetch(`${baseURL}/api/users`, {
headers: {
'User-Agent': 'Custom-SvelteKit-Agent/1.0',
'Content-Type': 'application/json',
'X-Test-Header': 'sveltekit-test-value',
Accept: 'application/json',
'X-Framework': 'SvelteKit',
},
});

const serverTxnEvent = await serverTxnEventPromise;

expect(serverTxnEvent.contexts?.trace?.data).toEqual(
expect.objectContaining({
'http.request.header.user_agent': ['Custom-SvelteKit-Agent/1.0'],
'http.request.header.content_type': ['application/json'],
'http.request.header.x_test_header': ['sveltekit-test-value'],
'http.request.header.accept': ['application/json'],
'http.request.header.x_framework': ['SvelteKit'],
}),
);
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran prettier fix and that's why this and the following three files are included in the diff.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ Sentry.init({
tracesSampleRate: 1.0,
sendDefaultPii: true,
transport: loggingTransport,
integrations: [
Sentry.anthropicAIIntegration(),
nodeContextIntegration(),
],
integrations: [Sentry.anthropicAIIntegration(), nodeContextIntegration()],
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ Sentry.init({
sendDefaultPii: false,
transport: loggingTransport,
// Force include the integration
integrations: [
Sentry.anthropicAIIntegration(),
nodeContextIntegration(),
],
integrations: [Sentry.anthropicAIIntegration(), nodeContextIntegration()],
});
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,3 @@ async function run() {
}

run();


Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MockAnthropic {
// Create messages object with create and countTokens methods
this.messages = {
create: this._messagesCreate.bind(this),
countTokens: this._messagesCountTokens.bind(this)
countTokens: this._messagesCountTokens.bind(this),
};

this.models = {
Expand Down Expand Up @@ -56,8 +56,8 @@ class MockAnthropic {

// For countTokens, just return input_tokens
return {
input_tokens: 15
}
input_tokens: 15,
};
}

async _modelsRetrieve(modelId) {
Expand All @@ -69,7 +69,7 @@ class MockAnthropic {
id: modelId,
name: modelId,
created_at: 1715145600,
model: modelId, // Add model field to match the check in addResponseAttributes
model: modelId, // Add model field to match the check in addResponseAttributes
};
}
}
Expand All @@ -86,9 +86,7 @@ async function run() {
await client.messages.create({
model: 'claude-3-haiku-20240307',
system: 'You are a helpful assistant.',
messages: [
{ role: 'user', content: 'What is the capital of France?' },
],
messages: [{ role: 'user', content: 'What is the capital of France?' }],
temperature: 0.7,
max_tokens: 100,
});
Expand All @@ -106,9 +104,7 @@ async function run() {
// Third test: count tokens with cached tokens
await client.messages.countTokens({
model: 'claude-3-haiku-20240307',
messages: [
{ role: 'user', content: 'What is the capital of France?' },
],
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});

// Fourth test: models.retrieve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { afterAll, describe, expect, test } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests, createRunner } from '../../../utils/runner';
import { createTestServer } from '../../../utils/server';

function getCommonHttpRequestHeaders(): Record<string, unknown> {
return {
'http.request.header.accept': ['*/*'],
'http.request.header.accept_encoding': ['gzip, deflate'],
'http.request.header.accept_language': ['*'],
'http.request.header.connection': ['keep-alive'],
'http.request.header.host': [expect.any(String)],
'http.request.header.sec_fetch_mode': ['cors'],
'http.request.header.user_agent': ['node'],
};
}

describe('httpIntegration', () => {
afterAll(() => {
cleanupChildProcesses();
Expand Down Expand Up @@ -86,6 +98,7 @@ describe('httpIntegration', () => {
'sentry.sample_rate': 1,
'sentry.source': 'route',
url: `http://localhost:${port}/test`,
...getCommonHttpRequestHeaders(),
});
},
})
Expand Down Expand Up @@ -127,6 +140,9 @@ describe('httpIntegration', () => {
'sentry.sample_rate': 1,
'sentry.source': 'route',
url: `http://localhost:${port}/test`,
'http.request.header.content_length': ['9'],
'http.request.header.content_type': ['text/plain;charset=UTF-8'],
...getCommonHttpRequestHeaders(),
});
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export {
getSpanStatusFromHttpCode,
getTraceData,
getTraceMetaTags,
httpHeadersToSpanAttributes,
winterCGHeadersToDict,
graphqlIntegration,
hapiIntegration,
httpIntegration,
Expand Down
Loading
Loading