Warning
This plugin works only with @clickhouse/client and @clickhouse/client-web below 1.23.0.
The plugin patches @clickhouse/client-common. Starting with 1.23.0, both clients bundle their own copy of that code and never load @clickhouse/client-common. On 1.23.0 and later, the plugin creates no spans and reports no error.
On 1.24.0 and later, you don't need this plugin. The client traces itself with OpenTelemetry. See Migrate to native tracing.
If you use Monocle, use the @monocle.sh/instrumentation-clickhouse integration instead.
This module provides automatic instrumentation and tracing for ClickHouse in Node.js applications using the official @clickhouse/client package
Compatible with OpenTelemetry JS API and SDK 1.0+.
Pick the setup that matches your @clickhouse/client version:
@clickhouse/client version |
What to use |
|---|---|
<1.23.0 |
This plugin |
1.23.x |
The native tracer option. Spans don't include the SQL text. |
>=1.24.0 |
The native tracer option, with dangerously_log_query_text if needed |
Pass an OpenTelemetry tracer to createClient. The client accepts it as is, with no adapter:
import { trace } from '@opentelemetry/api'
import { createClient } from '@clickhouse/client'
const client = createClient({
url: 'http://localhost:8123',
tracer: trace.getTracer('@clickhouse/client'),
dangerously_log_query_text: true,
})Remove ClickHouseInstrumentation from your registerInstrumentations call once the client gets a tracer.
On 1.23.x, the tracer option exists but dangerously_log_query_text doesn't. Spans never carry the SQL text. Upgrade to 1.24.0 to get db.query.text.
dangerously_log_query_text defaults to false. When you set it to true, the client copies the full SQL statement into the db.query.text span attribute. It also writes the statement to the error-level logs of failed requests.
Any value written inline in the SQL reaches your tracing backend. A query such as SELECT * FROM users WHERE email = 'jane@example.com' sends that email address. Values bound through query_params and credentials are never sent, whatever the option says.
This plugin captures the query text by default, up to maxQueryLength characters. Turning the option on keeps the exposure you already had. To keep sensitive values out of your traces, pass them through query_params instead of writing them in the SQL.
- Span names. The client uses fixed names:
clickhouse.query,clickhouse.query.stream,clickhouse.command,clickhouse.exec,clickhouse.insert, andclickhouse.ping. This plugin appends the table name, as inclickhouse.query events. Update any dashboard or alert that filters on span names. db.operation.nameanddb.collection.name. The client sets them only onclickhouse.insertspans, toINSERTand to thetableparameter. Spans forquery,command, andexechave neither attribute. This plugin parses both out of the SQL for every operation.- Deprecated attributes. The client doesn't set
db.system,db.statement,db.operation, ordb.name. Usedb.system.name,db.query.text,db.operation.name, anddb.namespace. - New attributes. The client adds
clickhouse.summary.*counters from theX-ClickHouse-Summaryheader,clickhouse.request.query_id, anddb.response.returned_rows. From 1.24.0, you can add your own attributes to a single request with thespan_attributesparameter. requireParentSpanandsuppressInternalInstrumentation. The client has no option for either. The ClickHouse tracing guide shows how to wrap the tracer to get the same behavior.
See the @clickhouse/client changelog for the details of each version.
pnpm add @julr/otel-instrumentation-clickhouse@clickhouse/clientversions>=1.0.0 <1.23.0@clickhouse/client-webversions>=1.0.0 <1.23.0
const { ClickHouseInstrumentation } = require('@julr/otel-instrumentation-clickhouse');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new ClickHouseInstrumentation({
// optional params
// maxQueryLength: 2048,
// requireParentSpan: false,
// suppressInternalInstrumentation: true,
}),
],
});| Option | Type | Default | Description |
|---|---|---|---|
maxQueryLength |
number | 2048 | Maximum length of the query to include in the span. Queries longer than this will be truncated. Set to 0 to disable query capture. |
requireParentSpan |
boolean | false | Whether to require a parent span to create child spans. When true, ClickHouse spans will only be created if there is an active parent span. |
suppressInternalInstrumentation |
boolean | true | Whether to suppress internal HTTP instrumentation. When true, HTTP spans for ClickHouse requests will not be created by @opentelemetry/instrumentation-http. |
The following ClickHouse client methods are automatically instrumented:
query()- Execute SELECT queriesinsert()- Insert data into tablescommand()- Execute DDL commands (CREATE, ALTER, etc.)exec()- Execute raw queries
This instrumentation sets the following span attributes following OpenTelemetry Semantic Conventions for Database:
| Attribute | Description |
|---|---|
db.system.name |
Always set to clickhouse |
db.operation.name |
The operation type (query, insert, etc.) |
db.query.text |
The SQL query (truncated if exceeds max length) |
db.namespace |
The database name |
db.collection.name |
The table name (when available) |
server.address |
The ClickHouse server hostname |
server.port |
The ClickHouse server port |
- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
Apache 2.0 - See LICENSE for more information.