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.

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.0Supported 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
UseClickHouseInstrumentation 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 Information
Span Information
- Span name:
clickhouse.query,clickhouse.insert, etc. - Operation type:
db.operationattribute (SELECT, INSERT, CREATE TABLE, etc.) - SQL query text: Full query statement captured in
db.statement(configurable) - Database system:
db.systemattribute (clickhouse) - Database name:
db.nameattribute - Server address:
server.addressandserver.portattributes
Performance Metrics
Performance Metrics
- Query execution time
- Number of rows read
- Number of bytes processed
- Network latency
Error Tracking
Error Tracking
- 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:| Attribute | Description | Example |
|---|---|---|
db.operation | SQL operation type | SELECT |
db.statement | Full SQL query | SELECT * FROM events... |
db.system | Database system | clickhouse |
db.name | Database name | analytics |
server.address | Server hostname | clickhouse.example.com |
server.port | Server port | 8123 |
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
Use Connection Pooling
Use Connection Pooling
ClickHouse client handles connection pooling automatically:
Configure Query Timeouts
Configure Query Timeouts
Set appropriate timeouts for your queries:
Use Batch Inserts
Use Batch Inserts
For high-throughput scenarios, batch your inserts:
Monitor Query Performance
Monitor Query Performance
Use traces to identify slow queries and optimize them with appropriate indexes and table engines.
Performance Considerations
Minimal Overhead
Minimal Overhead
The instrumentation adds minimal overhead (~1-2ms per query) for tracing operations.
Sampling
Sampling
Use OpenTelemetry sampling to reduce data volume in high-traffic applications:
Troubleshooting
Spans Not Appearing
Spans Not Appearing
Ensure OpenTelemetry is initialized before making ClickHouse queries:
SQL Not Captured
SQL Not Captured
Check that
captureQueryText is enabled:Connection Errors
Connection Errors
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!