Skip to content
Merged
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 dev-packages/bun-integration-tests/node-suites/excludes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const NO_AUTO_INSTRUMENTATION = [
'suites/tracing/anthropic/v0.129/test.ts',
'suites/tracing/anthropic/v1/test.ts',
'suites/tracing/apollo-graphql/**',
'suites/tracing/clickhouse/test.ts',
'suites/tracing/dataloader/test.ts',
'suites/tracing/fastify/test.ts',
'suites/tracing/genericPool-v2/test.ts',
Expand Down
1 change: 1 addition & 0 deletions dev-packages/cloudflare-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@anthropic-ai/sdk": "0.133.0",
"@clickhouse/client-web": "^1.25.0",
"@cloudflare/vite-plugin": "1.66.0",
"@cloudflare/workers-types": "^4.20260426.0",
"@cloudflare/workers-types-v5": "npm:@cloudflare/workers-types@5.20260710.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createClient } from '@clickhouse/client-web';

// The real web client runs in workerd, with its methods instrumented by the Sentry Vite plugin.
// Only the HTTP responses are mocked, so this needs neither a ClickHouse service nor credentials.
export default {
async fetch() {
const queries: string[] = [];
const mockFetch: typeof fetch = async (input, init) => {
const url = new URL(String(input));
const query = url.searchParams.get('query') ?? String(init?.body);
queries.push(query);
if (query.includes('does_not_exist')) {
return new Response('Code: 60. DB::Exception: Table tests.does_not_exist does not exist. (UNKNOWN_TABLE)', {
status: 500,
});
}
return new Response('{"name":"signup"}\n');
};

const client = createClient({
url: 'http://clickhouse.internal:8123',
database: 'tests',
username: 'test',
password: 'test',
fetch: mockFetch,
});

try {
const result = await client.query({
query: "SELECT name FROM events WHERE name = 'signup'",
format: 'JSONEachRow',
});
const rows = await result.json();
await client.command({ query: 'INSERT INTO TABLE events FORMAT CSV\n1,private@example.com' });
const { stream } = await client.exec({ query: 'SELECT count() FROM events FORMAT JSONEachRow' });
await stream.cancel();
await client.insert({ table: 'events', values: [{ id: 1, name: 'signup' }], format: 'JSONEachRow' });
// An empty insert makes no HTTP request and must not create a span.
await client.insert({ table: 'events', values: [], format: 'JSONEachRow' });

let failures = 0;
for (const call of [
() => client.query({ query: 'SELECT * FROM does_not_exist' }),
() => client.command({ query: 'DROP TABLE does_not_exist' }),
() => client.exec({ query: 'SELECT * FROM does_not_exist FORMAT JSONEachRow' }),
() => client.insert({ table: 'does_not_exist', values: [{ id: 1 }], format: 'JSONEachRow' }),
]) {
await call().catch(() => {
failures++;
});
}

return Response.json({ rows, requests: queries.length, failures });
} finally {
await client.close();
}
},
} satisfies ExportedHandler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineCloudflareOptions } from '@sentry/cloudflare';

interface Env {
SENTRY_DSN: string;
}

export default defineCloudflareOptions((env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import {
DB_COLLECTION_NAME,
DB_NAMESPACE,
DB_OPERATION_NAME,
DB_QUERY_SUMMARY,
DB_QUERY_TEXT,
DB_SYSTEM_NAME,
DB_USER,
SENTRY_KIND,
SENTRY_OP,
SENTRY_ORIGIN,
SERVER_ADDRESS,
SERVER_PORT,
} from '@sentry/conventions/attributes';
import { DB } from '@sentry/conventions/op';
import { expect, it } from 'vitest';
import { createRunner } from '../../../runner';
import { getSpansFromEnvelope } from '../../../spanUtils';

it('instruments client-web calls in a bundled Worker and leaves handled errors as spans', async ({ signal }) => {
const runner = createRunner(__dirname).start(signal);
const spansPromise = runner.collectStreamedSpans(
spans =>
spans.some(span => span.is_segment) &&
spans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.db.clickhouse').length === 8,
);

expect(await runner.makeRequest('get', '/')).toEqual({ rows: [{ name: 'signup' }], requests: 8, failures: 4 });
const spans = await spansPromise;
const segment = spans.find(span => span.is_segment);
const dbSpans = spans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.db.clickhouse');

for (const span of dbSpans) {
expect(span.parent_span_id).toBe(segment?.span_id);
expect(span.trace_id).toBe(segment?.trace_id);
expect(span.attributes[SENTRY_OP]).toEqual({ type: 'string', value: DB });
expect(span.attributes[SENTRY_KIND]).toEqual({ type: 'string', value: 'client' });
expect(span.attributes[DB_SYSTEM_NAME]).toEqual({ type: 'string', value: 'clickhouse' });
expect(span.attributes[DB_NAMESPACE]).toEqual({ type: 'string', value: 'tests' });
expect(span.attributes[DB_USER]).toEqual({ type: 'string', value: 'test' });
expect(span.attributes[SERVER_ADDRESS]).toEqual({ type: 'string', value: 'clickhouse.internal' });
expect(span.attributes[SERVER_PORT]).toEqual({ type: 'integer', value: 8123 });
expect(span.attributes[DB_QUERY_SUMMARY]?.value).toBe(span.name);
}

expect(
dbSpans.map(span => ({
name: span.name,
status: span.status,
operation: span.attributes[DB_OPERATION_NAME]?.value,
text: span.attributes[DB_QUERY_TEXT]?.value,
collection: span.attributes[DB_COLLECTION_NAME]?.value,
})),
).toEqual([
{
name: 'SELECT events',
status: 'ok',
operation: 'SELECT',
text: 'SELECT name FROM events WHERE name = ?',
collection: undefined,
},
{
name: 'INSERT events',
status: 'ok',
operation: 'INSERT',
text: 'INSERT INTO TABLE events',
collection: undefined,
},
{
name: 'SELECT events',
status: 'ok',
operation: 'SELECT',
text: 'SELECT count() FROM events FORMAT JSONEachRow',
collection: undefined,
},
{ name: 'INSERT events', status: 'ok', operation: 'INSERT', text: undefined, collection: 'events' },
{
name: 'SELECT does_not_exist',
status: 'error',
operation: 'SELECT',
text: 'SELECT * FROM does_not_exist',
collection: undefined,
},
{
name: 'DROP TABLE does_not_exist',
status: 'error',
operation: 'DROP',
text: 'DROP TABLE does_not_exist',
collection: undefined,
},
{
name: 'SELECT does_not_exist',
status: 'error',
operation: 'SELECT',
text: 'SELECT * FROM does_not_exist FORMAT JSONEachRow',
collection: undefined,
},
{
name: 'INSERT does_not_exist',
status: 'error',
operation: 'INSERT',
text: undefined,
collection: 'does_not_exist',
},
]);

const envelopes = runner.getReceivedEnvelopes();
expect(envelopes.some(envelope => envelope[1].some(item => item[0].type === 'event'))).toBe(false);
expect(envelopes.flatMap(getSpansFromEnvelope).filter(span => span.attributes[SENTRY_OP]?.value === DB)).toHaveLength(
8,
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { cloudflare } from '@cloudflare/vite-plugin';
import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [cloudflare(), sentryCloudflareVitePlugin()],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/wrangler/config-schema.json",
"name": "cloudflare-clickhouse",
"main": "index.ts",
"compatibility_date": "2025-06-17",
"compatibility_flags": ["nodejs_compat"],
}
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@aws-sdk/client-sfn": "^3.1161.0",
"@aws-sdk/client-sns": "^3.1161.0",
"@aws-sdk/client-sqs": "^3.1161.0",
"@clickhouse/client": "^1.25.0",
"@earendil-works/chord": "^1.4.0",
"@earendil-works/pi-ai": "^1.4.0",
"@earendil-works/pi-durable": "^1.4.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
db:
image: clickhouse/clickhouse-server:25.8
restart: always
ports:
- '8123:8123'
environment:
CLICKHOUSE_DB: tests
CLICKHOUSE_USER: test
CLICKHOUSE_PASSWORD: test
ulimits:
nofile:
soft: 262144
hard: 262144
healthcheck:
test: ['CMD-SHELL', 'clickhouse-client --user test --password test --query "SELECT 1"']
interval: 2s
timeout: 3s
retries: 30
start_period: 5s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as Sentry from '@sentry/node';
import { waitForConnection } from '@sentry-internal/node-integration-tests';
import { createClient } from '@clickhouse/client';

const client = createClient({
url: 'http://localhost:8123',
username: 'test',
password: 'test',
database: 'tests',
});

async function run() {
// `ping` isn't instrumented, so the probe adds no spans.
await waitForConnection(async () => {
const result = await client.ping();
if (!result.success) {
throw result.error;
}
});

await Sentry.startSpan({ name: 'Test Span', op: 'test' }, async () => {
try {
await client.command({
query: 'CREATE TABLE IF NOT EXISTS events (id UInt32, name String) ENGINE = MergeTree ORDER BY id',
});

await client.insert({ table: 'events', values: [{ id: 1, name: 'signup' }], format: 'JSONEachRow' });
await client.command({ query: 'INSERT INTO events FORMAT CSV\n2,inline-sensitive@example.com' });
await client.command({ query: 'INSERT INTO TABLE events FORMAT CSV\n3,inline-sensitive@example.com' });

const literal = await client.query({
query: "SELECT name FROM events WHERE name = 'signup'",
format: 'JSONEachRow',
});
await literal.json();

const nestedComment = await client.query({
query: 'SELECT 1 /* outer /* inner */ private@example.com */',
format: 'JSONEachRow',
});
await nestedComment.json();

const parameterized = await client.query({
query: 'SELECT name FROM events WHERE id = {id:UInt32}',
query_params: { id: 1 },
format: 'JSONEachRow',
});
await parameterized.json();

const { stream } = await client.exec({ query: 'SELECT count() FROM events FORMAT JSONEachRow' });
for await (const _chunk of stream) {
// drain the response so the socket is released
}

// Failed calls the caller handles: each should leave an errored span and no error event.
await client.query({ query: 'SELECT * FROM does_not_exist' }).catch(() => {});
await client.command({ query: 'DROP TABLE does_not_exist' }).catch(() => {});
await client.exec({ query: 'SELECT * FROM does_not_exist FORMAT JSONEachRow' }).catch(() => {});
await client.insert({ table: 'does_not_exist', values: [{ id: 1 }], format: 'JSONEachRow' }).catch(() => {});
} finally {
await client.command({ query: 'DROP TABLE IF EXISTS events' });
}
});

await client.close();
}

run();
Loading
Loading