Skip to main content

Overview

@kubiks/otel-clickhouse provides comprehensive OpenTelemetry instrumentation for ClickHouse. Add distributed tracing to your ClickHouse database queries with a single line of code—perfect for analytics workloads and OLAP queries.
ClickHouse Trace Visualization
Visualize your ClickHouse queries with detailed span information including query text, execution time, and performance metrics.

Installation

Peer Dependencies: @opentelemetry/api >= 1.9.0, @clickhouse/client >= 0.2.0

Supported Frameworks

Works with any TypeScript framework and Node.js runtime:

Next.js

App Router & Pages Router

Fastify

High-performance server

NestJS

Enterprise framework

Express

Classic Node.js server

Remix

Full-stack framework

SvelteKit

Modern web framework

Supported Platforms

Works with any observability platform that supports OpenTelemetry:

Quick Start

Use ClickHouseInstrumentation to add tracing to your ClickHouse client:
This is the simplest approach—just add the instrumentation and all ClickHouse queries are automatically traced!

Configuration Options

By default, SQL queries are captured in spans. You can disable this by setting captureQueryText: false for sensitive environments.

What You Get

Each ClickHouse query automatically creates a span with rich telemetry data:
  • Span name: clickhouse.query, clickhouse.insert, etc.
  • Operation type: db.operation attribute (SELECT, INSERT, CREATE TABLE, etc.)
  • SQL query text: Full query statement captured in db.statement (configurable)
  • Database system: db.system attribute (clickhouse)
  • Database name: db.name attribute
  • Server address: server.address and server.port attributes
  • Query execution time
  • Number of rows read
  • Number of bytes processed
  • Network latency
  • Exceptions are recorded with stack traces
  • Proper span status (OK, ERROR)
  • Error messages and ClickHouse error codes

Span Attributes

The instrumentation adds the following attributes to each span following OpenTelemetry semantic conventions:

Usage Examples

Basic Queries

Streaming Queries

Parameterized Queries

Complete Integration Example

Here’s a complete example of ClickHouse with OpenTelemetry in a Next.js application:
lib/clickhouse.ts
instrumentation.ts
app/api/analytics/route.ts

Best Practices

ClickHouse client handles connection pooling automatically:
Set appropriate timeouts for your queries:
For high-throughput scenarios, batch your inserts:
Use traces to identify slow queries and optimize them with appropriate indexes and table engines.

Performance Considerations

The instrumentation adds minimal overhead (~1-2ms per query) for tracing operations.
Use OpenTelemetry sampling to reduce data volume in high-traffic applications:

Troubleshooting

Ensure OpenTelemetry is initialized before making ClickHouse queries:
Check that captureQueryText is enabled:
Verify your ClickHouse connection settings:

Resources

ClickHouse Documentation

Learn more about ClickHouse

GitHub Repository

View source code and examples

npm Package

View package on npm

Report Issues

Found a bug? Let us know!

License

MIT