Skip to content
45 changes: 26 additions & 19 deletions MIGRATION.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,44 @@ test.describe('server - orchestrion db instrumentation', () => {

const childSpans = spans.filter(span => !span.is_segment);

// Under span streaming a redis span is named after the operation and the connection — the key
// it acts on is unbounded, so it stays on `db.query.text`. The route builds its client without
// a host or port, so ioredis' own `localhost:6379` defaults are what the name reports.
expect(childSpans).toContainEqual(
expect.objectContaining({
name: 'set test-key [1 other arguments]',
name: 'set localhost:6379',
status: 'ok',
attributes: expect.objectContaining({
'sentry.op': { value: 'db.query', type: 'string' },
'sentry.origin': { value: 'auto.db.redis', type: 'string' },
'db.system.name': { value: 'redis', type: 'string' },
'db.operation.name': { value: 'set', type: 'string' },
'db.query.text': { value: 'set test-key [1 other arguments]', type: 'string' },
'server.address': { value: 'localhost', type: 'string' },
'server.port': { value: 6379, type: 'integer' },
}),
}),
);
expect(childSpans).toContainEqual(
expect.objectContaining({
name: 'get test-key',
name: 'get localhost:6379',
status: 'ok',
attributes: expect.objectContaining({
'sentry.op': { value: 'db.query', type: 'string' },
'sentry.origin': { value: 'auto.db.redis', type: 'string' },
'db.system.name': { value: 'redis', type: 'string' },
'db.operation.name': { value: 'get', type: 'string' },
'db.query.text': { value: 'get test-key', type: 'string' },
'server.address': { value: 'localhost', type: 'string' },
'server.port': { value: 6379, type: 'integer' },
}),
}),
);

// Each command maps to exactly one span (no offline-queue duplicate).
const setSpans = spans.filter(span => span.name === 'set test-key [1 other arguments]');
const setSpans = spans.filter(
span => span.attributes['db.query.text']?.value === 'set test-key [1 other arguments]',
);
expect(setSpans).toHaveLength(1);

// Every db span nests under the native instrumentation-API http.server segment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
traceLifecycle: 'static',
traceLifecycle: process.env.STREAMED === 'true' ? 'stream' : 'static',
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { afterAll, expect } from 'vitest';
import { SENTRY_TRACE_LIFECYCLE } from '@sentry/conventions/attributes';
import type { SerializedStreamedSpanContainer } from '@sentry/core';
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests, describeWithDockerCompose } from '../../../utils/runner';

describeWithDockerCompose(
Expand Down Expand Up @@ -103,5 +105,134 @@ describeWithDockerCompose(
.completed();
});
});

// The same commands as above, asserted on the streamed span container. With span streaming the
// db spans are named `{db.operation.name} {server.address}:{server.port}` instead of
// `redis-{command}`. ioredis reports its commands lowercase, so the name follows suit.
describe('streamed', () => {
const ORIGIN = 'auto.db.redis.diagnostic_channel';
const SEGMENT_NAME = 'Test Span IORedis 5.11 DC';
const HOST = '127.0.0.1';
const PORT = 6382;

const streamAttribute = (value: unknown): { type: string; value: unknown } => ({
type: Array.isArray(value) ? 'array' : Number.isInteger(value) ? 'integer' : typeof value,
value,
});

// Streamed spans carry `{ type, value }` attribute pairs; the expectations below are written
// as plain values and wrapped here.
const streamAttributes = (values: Record<string, unknown>): Record<string, unknown> =>
Object.fromEntries(Object.entries(values).map(([key, value]) => [key, streamAttribute(value)]));

function streamedSpan(name: string, op: string, attributes: Record<string, unknown>): unknown {
return {
name,
attributes: {
...streamAttributes({
'db.system.name': 'redis',
'sentry.environment': 'production',
'sentry.op': op,
'sentry.origin': ORIGIN,
'sentry.release': '1.0',
'sentry.sdk.name': 'sentry.javascript.node',
'sentry.segment.name': SEGMENT_NAME,
'server.address': HOST,
'server.port': PORT,
[SENTRY_TRACE_LIFECYCLE]: 'stream',
...attributes,
}),
'sentry.sdk.version': { type: 'string', value: expect.any(String) },
'sentry.segment.id': { type: 'string', value: expect.stringMatching(/^[\da-f]{16}$/) },
},
end_timestamp: expect.any(Number),
is_segment: false,
parent_span_id: expect.stringMatching(/^[\da-f]{16}$/),
span_id: expect.stringMatching(/^[\da-f]{16}$/),
start_timestamp: expect.any(Number),
status: 'ok',
trace_id: expect.stringMatching(/^[\da-f]{32}$/),
};
}

const PEER = { 'network.peer.address': HOST, 'network.peer.port': PORT };

// A cache span is a db span the cache hook took over: it is renamed to its cache operation
// and reports the connection it inherited as peer attributes too.
const cacheSpan = (
op: 'cache.get' | 'cache.put' | 'cache.remove',
attributes: Record<string, unknown>,
): unknown => streamedSpan(op, op, { ...PEER, 'cache.operation': op.slice('cache.'.length), ...attributes });

createEsmAndCjsTests(__dirname, 'scenario-ioredis-5-11.mjs', 'instrument.mjs', (createTestRunner, test) => {
test(
'creates streamed spans for ioredis v5.11 commands via diagnostics_channel',
{ timeout: 75_000 },
async () => {
await createTestRunner()
.withEnv({ STREAMED: 'true' })
.expect({
span: (container: SerializedStreamedSpanContainer) => {
// The connect span opens its own segment but shares the trace with the test span,
// so both segments arrive in the same container.
expect(container.items.filter(item => item.is_segment).map(item => item.name)).toEqual([
'redis-connect',
SEGMENT_NAME,
]);

// ioredis' own handshake commands (`client SETINFO`, `info`) are emitted on the
// channel too, but belong to the connect segment — the test span's children are the
// commands the scenario issues.
const spans = container.items.filter(
item => !item.is_segment && item.attributes['sentry.segment.name']?.value === SEGMENT_NAME,
);

expect(spans).toEqual([
streamedSpan(`set ${HOST}:${PORT}`, 'db.query', {
'db.operation.name': 'set',
'db.query.text': 'set dc-test-key ?',
}),
cacheSpan('cache.put', {
'db.operation.name': 'set',
'db.query.text': 'set dc-cache:test-key ?',
'cache.key': ['dc-cache:test-key'],
'cache.item_size': 2,
}),
cacheSpan('cache.put', {
'db.operation.name': 'set',
'db.query.text': 'set dc-cache:test-key-ex ? ? ?',
'cache.key': ['dc-cache:test-key-ex'],
'cache.item_size': 2,
}),
streamedSpan(`get ${HOST}:${PORT}`, 'db.query', {
'db.operation.name': 'get',
'db.query.text': 'get dc-test-key',
}),
cacheSpan('cache.get', {
'db.operation.name': 'get',
'db.query.text': 'get dc-cache:test-key',
'cache.key': ['dc-cache:test-key'],
'cache.hit': true,
'cache.item_size': 10,
}),
cacheSpan('cache.get', {
'db.operation.name': 'get',
'db.query.text': 'get dc-cache:unavailable-data',
'cache.key': ['dc-cache:unavailable-data'],
'cache.hit': false,
}),
streamedSpan(`mget ${HOST}:${PORT}`, 'db.query', {
'db.operation.name': 'mget',
'db.query.text': 'mget ? ? ?',
}),
]);
},
})
.start()
.completed();
},
);
});
});
},
);
Loading
Loading