Skip to content
Open
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
22 changes: 11 additions & 11 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ module.exports = [
name: 'CDN Bundle (incl. Tracing)',
path: createCDNPath('bundle.tracing.min.js'),
gzip: true,
limit: '46.5 KB',
limit: '47 KB',
},
{
name: 'CDN Bundle (incl. Logs, Metrics)',
Expand All @@ -203,7 +203,7 @@ module.exports = [
name: 'CDN Bundle (incl. Tracing, Logs, Metrics)',
path: createCDNPath('bundle.tracing.logs.metrics.min.js'),
gzip: true,
limit: '47.5 KB',
limit: '48 KB',
},
{
name: 'CDN Bundle (incl. Replay, Logs, Metrics)',
Expand All @@ -215,19 +215,19 @@ module.exports = [
name: 'CDN Bundle (incl. Tracing, Replay)',
path: createCDNPath('bundle.tracing.replay.min.js'),
gzip: true,
limit: '83.5 KB',
limit: '84 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Logs, Metrics)',
path: createCDNPath('bundle.tracing.replay.logs.metrics.min.js'),
gzip: true,
limit: '84.5 KB',
limit: '85 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Feedback)',
path: createCDNPath('bundle.tracing.replay.feedback.min.js'),
gzip: true,
limit: '89 KB',
limit: '89.5 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)',
Expand All @@ -248,7 +248,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.min.js'),
gzip: false,
brotli: false,
limit: '138 KB',
limit: '140 KB',
},
{
name: 'CDN Bundle (incl. Logs, Metrics) - uncompressed',
Expand All @@ -262,7 +262,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '141.5 KB',
limit: '143 KB',
},
{
name: 'CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed',
Expand All @@ -276,28 +276,28 @@ module.exports = [
path: createCDNPath('bundle.tracing.replay.min.js'),
gzip: false,
brotli: false,
limit: '255.5 KB',
limit: '257 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed',
path: createCDNPath('bundle.tracing.replay.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '259 KB',
limit: '260.5 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed',
path: createCDNPath('bundle.tracing.replay.feedback.min.js'),
gzip: false,
brotli: false,
limit: '269 KB',
limit: '270 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed',
path: createCDNPath('bundle.tracing.replay.feedback.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '273 KB',
limit: '273.5 KB',
},
// Next.js SDK (ESM)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
traceLifecycle: 'stream',
transport: loggingTransport,
});

async function run(): Promise<void> {
await Sentry.startSpan({ name: 'test_transaction' }, async () => {
await fetch(`${process.env.SERVER_URL}/api/v0`);
});

await Sentry.flush();
}

void run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createTestServer } from '@sentry-internal/test-utils';
import { expect, test } from 'vitest';
import { createRunner } from '../../../../utils/runner';

test('infers sentry.op for streamed outgoing fetch spans', async () => {
expect.assertions(2);

const [SERVER_URL, closeTestServer] = await createTestServer()
.get('/api/v0', () => {
expect(true).toBe(true);
})
.start();

await createRunner(__dirname, 'scenario.ts')
.withEnv({ SERVER_URL })
.expect({
span: container => {
const httpClientSpan = container.items.find(
item =>
item.attributes?.['sentry.op']?.type === 'string' && item.attributes['sentry.op'].value === 'http.client',
);

expect(httpClientSpan).toBeDefined();
},
})
.start()
.completed();
closeTestServer();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
traceLifecycle: 'stream',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Sentry from '@sentry/node';
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
import cors from 'cors';
import express from 'express';

const app = express();

app.use(cors());

app.get('/test', (_req, res) => {
res.send({ response: 'ok' });
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';

describe('httpIntegration-streamed', () => {
afterAll(() => {
cleanupChildProcesses();
});

createEsmAndCjsTests(__dirname, 'server.mjs', 'instrument.mjs', (createRunner, test) => {
test('infers sentry.op, name, and source for streamed server spans', async () => {
const runner = createRunner()
.expect({
span: container => {
const serverSpan = container.items.find(
item =>
item.attributes?.['sentry.op']?.type === 'string' &&
item.attributes['sentry.op'].value === 'http.server',
);

expect(serverSpan).toBeDefined();
expect(serverSpan?.is_segment).toBe(true);
expect(serverSpan?.name).toBe('GET /test');
expect(serverSpan?.attributes?.['sentry.source']).toEqual({ type: 'string', value: 'route' });
expect(serverSpan?.attributes?.['sentry.span.source']).toEqual({ type: 'string', value: 'route' });
},
})
.start();

await runner.makeRequest('get', '/test');

await runner.completed();
});
});
});
120 changes: 120 additions & 0 deletions packages/core/src/tracing/spans/captureSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { RawAttributes } from '../../attributes';
import type { Client } from '../../client';
import type { ScopeData } from '../../scope';
import {
SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_RELEASE,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION,
Expand Down Expand Up @@ -58,6 +60,14 @@ export function captureSpan(span: Span, client: Client): SerializedStreamedSpanW
client.emit('processSegmentSpan', spanJSON);
}

// Backfill span data from OTel semantic conventions when not explicitly set.
// OTel-originated spans don't have sentry.op, description, etc. — the non-streamed path
// infers these in the SentrySpanExporter, but streamed spans skip the exporter entirely.
// Access `kind` via duck-typing — OTel span objects have this property but it's not on Sentry's Span type.
// This must run before hooks and beforeSendSpan so that user callbacks can see and override inferred values.
const spanKind = (span as { kind?: number }).kind;
inferSpanDataFromOtelAttributes(spanJSON, spanKind);
Comment thread
chargome marked this conversation as resolved.

// This allows hook subscribers to mutate the span JSON
// This also invokes the `processSpan` hook of all integrations
client.emit('processSpan', spanJSON);
Expand Down Expand Up @@ -150,3 +160,113 @@ export function safeSetSpanJSONAttributes(
}
});
}

// OTel SpanKind values (numeric to avoid importing from @opentelemetry/api)
const SPAN_KIND_SERVER = 1;
const SPAN_KIND_CLIENT = 2;

/**
* Infer and backfill span data from OTel semantic conventions.
* This mirrors what the `SentrySpanExporter` does for non-streamed spans via `getSpanData`/`inferSpanData`.
* Streamed spans skip the exporter, so we do the inference here during capture.
*
* Backfills: `sentry.op`, `sentry.source`, and `name` (description).
* Uses `safeSetSpanJSONAttributes` so explicitly set attributes are never overwritten.
*/
function inferSpanDataFromOtelAttributes(spanJSON: StreamedSpanJSON, spanKind?: number): void {
const attributes = spanJSON.attributes;
if (!attributes) {
return;
}

const httpMethod = attributes['http.request.method'] || attributes['http.method'];
if (httpMethod) {
inferHttpSpanData(spanJSON, attributes, spanKind, httpMethod);
return;
}

const dbSystem = attributes['db.system.name'] || attributes['db.system'];
const opIsCache =
typeof attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'string' &&
`${attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]}`.startsWith('cache.');
if (dbSystem && !opIsCache) {
inferDbSpanData(spanJSON, attributes);
return;
}
Comment thread
cursor[bot] marked this conversation as resolved.

if (attributes['rpc.service']) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'rpc' });
return;
}

if (attributes['messaging.system']) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'message' });
return;
}

const faasTrigger = attributes['faas.trigger'];
if (faasTrigger) {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${faasTrigger}` });
}
}

function inferHttpSpanData(
spanJSON: StreamedSpanJSON,
attributes: RawAttributes<Record<string, unknown>>,
spanKind: number | undefined,
httpMethod: unknown,
): void {
// Infer op: http.client, http.server, or just http
const opParts = ['http'];
if (spanKind === SPAN_KIND_CLIENT) {
opParts.push('client');
} else if (spanKind === SPAN_KIND_SERVER) {
opParts.push('server');
}
if (attributes['sentry.http.prefetch']) {
opParts.push('prefetch');
}
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: opParts.join('.') });

// If the user set a custom span name via updateSpanName(), apply it — OTel instrumentation
// may have overwritten span.name after the user set it, so we restore from the attribute.
const customName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
if (typeof customName === 'string') {
spanJSON.name = customName;
return;
}

if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
return;
}
Comment thread
cursor[bot] marked this conversation as resolved.

// Only overwrite the span name when we have an explicit http.route — it's more specific than
// what OTel instrumentation sets as the span name. For all other cases (url.full, http.target),
// the OTel-set name is already good enough and we'd risk producing a worse name (e.g. full URL).
const httpRoute = attributes['http.route'];
if (typeof httpRoute === 'string') {
spanJSON.name = `${httpMethod} ${httpRoute}`;
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' });
}
}

function inferDbSpanData(spanJSON: StreamedSpanJSON, attributes: RawAttributes<Record<string, unknown>>): void {
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db' });

// If the user set a custom span name via updateSpanName(), apply it.
const customName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
if (typeof customName === 'string') {
spanJSON.name = customName;
return;
}

if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom') {
return;
}

const statement = attributes['db.statement'];
if (statement) {
spanJSON.name = `${statement}`;
Comment thread
sentry[bot] marked this conversation as resolved.
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task' });
}
}
Loading