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
1 change: 1 addition & 0 deletions packages/nuxt/src/runtime/utils/instrumentDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function createStartSpanOptions(

return {
name,
onlyIfParent: true,
attributes: {
[DB_QUERY_TEXT]: queryText,
[DB_QUERY_SUMMARY]: querySummary,
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/runtime/utils/instrumentStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function createSpanStartOptions(
return {
// With span streaming, span names have to be low cardinality, so we can't fall back to the cache keys.
name: client && hasSpanStreamingEnabled(client) ? cacheOperation : keys.join(', '),
onlyIfParent: true,
attributes,
};
}
Expand Down
38 changes: 38 additions & 0 deletions packages/nuxt/test/runtime/utils/instrumentDatabase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createTransport, getCurrentScope, spanToJSON, startSpan } from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core/server';
import type { Database } from 'db0';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createDatabasePlugin } from '../../../src/runtime/utils/instrumentDatabase';

describe('createDatabasePlugin', () => {
const startedSpans: string[] = [];

beforeEach(() => {
startedSpans.length = 0;

const client = new ServerRuntimeClient({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, () => Promise.resolve({})),
stackParser: () => [],
tracesSampleRate: 1,
});
getCurrentScope().setClient(client);
client.init();
client.on('spanStart', span => startedSpans.push(spanToJSON(span).name));
});

afterEach(() => {
getCurrentScope().setClient(undefined);
});

it('only creates db spans when there is an active parent span', async () => {
const db = { dialect: 'sqlite', exec: vi.fn(), sql: vi.fn(), prepare: vi.fn() } as unknown as Database;
createDatabasePlugin(() => db, { default: {} });

await db.exec('SELECT * FROM users');
await startSpan({ name: 'parent' }, () => db.exec('SELECT * FROM orders'));

expect(startedSpans).toEqual(['parent', 'SELECT orders']);
});
});
39 changes: 39 additions & 0 deletions packages/nuxt/test/runtime/utils/instrumentStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createTransport, getCurrentScope, spanToJSON, startSpan } from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core/server';
import type { Driver, Storage } from 'unstorage';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createStoragePlugin } from '../../../src/runtime/utils/instrumentStorage';

describe('createStoragePlugin', () => {
const startedSpans: string[] = [];

beforeEach(() => {
startedSpans.length = 0;

const client = new ServerRuntimeClient({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, () => Promise.resolve({})),
stackParser: () => [],
tracesSampleRate: 1,
});
getCurrentScope().setClient(client);
client.init();
client.on('spanStart', span => startedSpans.push(spanToJSON(span).name));
});

afterEach(() => {
getCurrentScope().setClient(undefined);
});

it('only creates cache spans when there is an active parent span', async () => {
const driver = { name: 'memory', getItem: vi.fn() } as unknown as Driver;
const storage = { getMounts: () => [{ base: 'cache:', driver }], mount: vi.fn() } as unknown as Storage;
await createStoragePlugin(() => storage, []);

await driver.getItem('outside', {});
await startSpan({ name: 'parent' }, () => driver.getItem('inside', {}));

expect(startedSpans).toEqual(['parent', 'cache.get']);
});
});
Loading