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
16 changes: 8 additions & 8 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ module.exports = [
name: 'CDN Bundle (incl. Tracing)',
path: createCDNPath('bundle.tracing.min.js'),
gzip: true,
limit: '46.5 KB',
limit: '47 KB',
Comment thread
chargome marked this conversation as resolved.
disablePlugins: ['@size-limit/esbuild'],
},
{
Expand All @@ -226,7 +226,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',
disablePlugins: ['@size-limit/esbuild'],
},
{
Expand All @@ -240,14 +240,14 @@ module.exports = [
name: 'CDN Bundle (incl. Tracing, Replay)',
path: createCDNPath('bundle.tracing.replay.min.js'),
gzip: true,
limit: '83.5 KB',
limit: '84 KB',
Comment thread
cursor[bot] marked this conversation as resolved.
disablePlugins: ['@size-limit/esbuild'],
},
{
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',
disablePlugins: ['@size-limit/esbuild'],
},
{
Expand Down Expand Up @@ -278,7 +278,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.min.js'),
gzip: false,
brotli: false,
limit: '139 KB',
limit: '140 KB',
disablePlugins: ['@size-limit/esbuild'],
},
{
Expand All @@ -294,7 +294,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '142 KB',
limit: '143 KB',
disablePlugins: ['@size-limit/esbuild'],
},
{
Expand All @@ -310,15 +310,15 @@ module.exports = [
path: createCDNPath('bundle.tracing.replay.min.js'),
gzip: false,
brotli: false,
limit: '256 KB',
limit: '257 KB',
disablePlugins: ['@size-limit/esbuild'],
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed',
path: createCDNPath('bundle.tracing.replay.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '260 KB',
limit: '260.5 KB',
disablePlugins: ['@size-limit/esbuild'],
},
{
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();
});
});
});
126 changes: 126 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 @@ -51,6 +53,14 @@ export function captureSpan(span: Span, client: Client): SerializedStreamedSpanW

applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, finalScopeData);

// 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 all 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.
Comment thread
chargome marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

if (spanJSON.is_segment) {
applyScopeToSegmentSpan(spanJSON, finalScopeData);
// Allow hook subscribers to mutate the segment span JSON
Expand Down Expand Up @@ -150,3 +160,119 @@ export function safeSetSpanJSONAttributes(
}
});
}

// OTel SpanKind values (numeric to avoid importing from @opentelemetry/api)
const SPAN_KIND_SERVER = 1;
Comment thread
chargome marked this conversation as resolved.
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.
*/
/** Exported only for tests. */
export 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}` });
}
Comment thread
sentry[bot] marked this conversation as resolved.
}

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;
Comment thread
sentry[bot] marked this conversation as resolved.
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' });
} else {
// Fallback: set source to 'url' for HTTP spans without a route.
// The spec requires sentry.span.source on segment spans, and the non-streamed exporter
// always sets this — so we need to ensure it's present for streamed spans too.
safeSetSpanJSONAttributes(spanJSON, { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' });
}
Comment thread
sentry[bot] marked this conversation as resolved.
}

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
Loading