Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

OpenTelemetry ClickHouse Instrumentation

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+.

Migrate to native tracing

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

Use the tracer option

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.

Understand the risk of dangerously_log_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.

What changes compared to this plugin

  • Span names. The client uses fixed names: clickhouse.query, clickhouse.query.stream, clickhouse.command, clickhouse.exec, clickhouse.insert, and clickhouse.ping. This plugin appends the table name, as in clickhouse.query events. Update any dashboard or alert that filters on span names.
  • db.operation.name and db.collection.name. The client sets them only on clickhouse.insert spans, to INSERT and to the table parameter. Spans for query, command, and exec have 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, or db.name. Use db.system.name, db.query.text, db.operation.name, and db.namespace.
  • New attributes. The client adds clickhouse.summary.* counters from the X-ClickHouse-Summary header, clickhouse.request.query_id, and db.response.returned_rows. From 1.24.0, you can add your own attributes to a single request with the span_attributes parameter.
  • requireParentSpan and suppressInternalInstrumentation. 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.

Installation

pnpm add @julr/otel-instrumentation-clickhouse

Supported Versions

Usage

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,
    }),
  ],
});

Configuration Options

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.

Instrumented Methods

The following ClickHouse client methods are automatically instrumented:

  • query() - Execute SELECT queries
  • insert() - Insert data into tables
  • command() - Execute DDL commands (CREATE, ALTER, etc.)
  • exec() - Execute raw queries

Span Attributes

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

Useful Links

License

Apache 2.0 - See LICENSE for more information.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages