ClickHouse
Semantic Logger sends log entries and metrics as OpenTelemetry (OTLP), which the OpenTelemetry
Collector writes to ClickHouse with its clickhouse exporter. No OpenTelemetry gems are needed in the
application: appender: :otlp and metric: :otlp write OTLP themselves. Grafana, with the ClickHouse
plugin, shows the logs and graphs the metrics.
application --OTLP/HTTP--> OpenTelemetry Collector --> ClickHouse <-- Grafana
Quick start
The quick start runs ClickHouse, the Collector, and Grafana in Docker, for development or to try it out. It needs Docker with Compose, and a checkout of Semantic Logger for its examples/clickhouse directory.
1. Start the stack
docker compose -f examples/clickhouse/compose.yml up -d --wait
compose.yml starts:
| Service | Image | On this host |
|---|---|---|
| ClickHouse | clickhouse/clickhouse-server:26.8.46.6 |
SQL over HTTP on localhost:8123, user and password otel |
| OpenTelemetry Collector | otel/opentelemetry-collector-contrib:0.166.0 |
OTLP/HTTP on localhost:4318 |
| Grafana | grafana/grafana:13.3.2, with grafana-clickhouse-datasource 4.24.0 |
localhost:3000, user and password admin |
When another stack has these ports, set CLICKHOUSE_PORT, OTLP_PORT, or GRAFANA_PORT before
up, and point the application at the Collector with OTEL_EXPORTER_OTLP_ENDPOINT. ClickHouse needs
about 1 GB of memory: give Docker more, such as with colima start --memory 4, when it stops with
memory limit exceeded.
2. The Collector
otel-collector.yaml
receives OTLP/HTTP and writes logs and metrics to the otel database, creating its tables. In
short, without its timeout and retry settings:
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
exporters:
clickhouse:
endpoint: tcp://clickhouse:9000?dial_timeout=10s
database: otel
username: otel
password: otel
ttl: 72h
create_schema: true
sending_queue:
batch:
flush_timeout: 1s
service:
pipelines:
logs:
receivers: [otlp]
exporters: [clickhouse]
metrics:
receivers: [otlp]
exporters: [clickhouse]
ttl is how long rows are kept: raise it for production. up --wait returns once the Collector has
started, which may be a moment before it accepts connections.
3. The application
Send log entries and metrics to http://localhost:4318, the default of both appenders:
SemanticLogger.application = "checkout"
SemanticLogger.environment = "development"
SemanticLogger.add_appender(appender: :otlp)
SemanticLogger.add_appender(metric: :otlp, interval: 10)
logger = SemanticLogger["Orders"]
logger.info("Created order", order_id: 42)
logger.increment("orders.created", dimensions: {plan: "pro"})
logger.time("orders.charge", dimensions: {gateway: "stripe"}) { charge(order) }
app.rb sends
a few of each: bundle exec ruby examples/clickhouse/app.rb.
In Rails, with Rails Semantic Logger 6.0 or later, bin/rails generate rails_semantic_logger:observability
adds the same stack, with dashboards, in compose.observability.yml, with Grafana on
http://localhost:3001. It first runs rails_semantic_logger:install, which writes an explicit
config.rails_semantic_logger.appenders block in each environment file, unless the app already
declares its appenders, then adds the OTLP appenders to the development one. In a new Rails app:
# config/environments/development.rb
config.rails_semantic_logger.appenders do |appenders|
appenders.add(file_name: RailsSemanticLogger.default_log_file_name(config), formatter: {color: {ap: {multiline: false}}})
appenders.add_server(io: $stdout, formatter: :color)
appenders.add_console(io: $stderr, formatter: :color)
# Send the logs and metrics to the local observability stack in compose.observability.yml:
# the OpenTelemetry Collector on localhost:4318, ClickHouse, and Grafana on http://localhost:3001.
# Added by `bin/rails generate rails_semantic_logger:observability`.
appenders.add(appender: :otlp)
appenders.add(metric: :otlp, interval: 10)
end
# The service.name of the logs and metrics sent to the observability stack.
config.semantic_logger.application = "my_shop"
It adds only an OTLP appender, for logs or for metrics, that the app does not already have. When the
app has one that applies only some of the time, such as add_server or inside a condition, it leaves
it as it is and says so.
See Configuring where logs go.
The OTLP appender and OTLP metrics pages describe what each sends and their options.
4. Check that the data arrived
Query ClickHouse over HTTP, or open Grafana’s Explore view on the ClickHouse data source:
curl -u otel:otel 'http://localhost:8123/?database=otel' --data-binary @- <<'SQL'
SELECT Timestamp, SeverityText, ScopeName, Body, LogAttributes
FROM otel_logs
WHERE ServiceName = 'checkout'
ORDER BY Timestamp DESC
LIMIT 20
FORMAT Vertical
SQL
The latest errors, with their exception:
SELECT Timestamp, ScopeName, Body, LogAttributes['exception.type'] AS type,
LogAttributes['exception.stacktrace'] AS stacktrace
FROM otel_logs
WHERE ServiceName = 'checkout' AND SeverityNumber >= 17
ORDER BY Timestamp DESC
LIMIT 20
Every entry logged in a trace, by its trace id:
SELECT Timestamp, ScopeName, Body
FROM otel_logs
WHERE TraceId = '5b8efff798038103d269b633813fc60c'
ORDER BY Timestamp
A counter, increment, per minute:
SELECT toStartOfMinute(TimeUnix) AS minute, Attributes['plan'] AS plan, sum(Value) AS created
FROM otel_metrics_sum
WHERE MetricName = 'orders.created'
GROUP BY minute, plan
ORDER BY minute
Each duration, of time, or of measure_* given metric:, arrives as a delta base-2 exponential
histogram, in seconds.
Its 95th percentile weights each positive bucket by its count, at the bucket’s middle,
2(PositiveOffset + i + 0.5) × 2-Scale, which is within one bucket of the
durations recorded:
SELECT quantileExactWeighted(0.95)(
pow(2, (PositiveOffset + index - 1 + 0.5) * pow(2, -Scale)), count
) AS p95_seconds
FROM otel_metrics_exponential_histogram
ARRAY JOIN PositiveBucketCounts AS count, arrayEnumerate(PositiveBucketCounts) AS index
WHERE MetricName = 'orders.charge' AND TimeUnix > now() - INTERVAL 1 HOUR
examples/clickhouse/test.rb runs this quick start, on ports of its own, and checks the rows with these queries.
Stop the stack with docker compose -f examples/clickhouse/compose.yml down, and add -v to delete
its data.
What ClickHouse holds
| Table | Holds | From Semantic Logger |
|---|---|---|
otel_logs |
One row for each log entry | Body the message, or an exception logged without one, as ArgumentError: the message; SeverityText and SeverityNumber the level; ScopeName the logger name; ServiceName the application; LogAttributes the payload, named tags, exception, and call site; TraceId and SpanId the trace |
otel_metrics_sum |
Counters, as deltas | increment, and a log call given metric: without a duration |
otel_metrics_exponential_histogram |
Histograms, as deltas: durations in seconds, and other values | time, measure_* given metric:, and record |
Every row also has the resource of the process: ServiceName, and ResourceAttributes with
host.name, deployment.environment.name, and process.pid.
In production
In production, have Semantic Logger write only to its own host: a local file, or an OpenTelemetry
Collector on localhost. The agent then owns the network, its retries, and a buffer on disk, so a
ClickHouse that is down or slow never reaches the application. Run the Collector beside each
application, as a sidecar or one per host, with the clickhouse exporter of the quick start pointed
at your ClickHouse, and a persistent queue:
extensions:
# A directory that the Collector's user can write to, on a persistent volume.
file_storage/queue:
directory: /var/lib/otelcol/queue
create_directory: true
exporters:
clickhouse:
# endpoint, database, username, password, ttl as above
sending_queue:
storage: file_storage/queue
service:
extensions: [file_storage/queue]
Then choose how the application hands its entries to the Collector:
- A log file (preferred): write
formatter: :jsonto a file, and have the Collector read it with thefilelogreceiver of examples/filelog/filelog.yaml, or writeformatter: :otlp_json_linesand read it with theotlpjsonfilereceiver (OTLP JSON Lines). Logs pile up in the file while the Collector is down, and it carries on from where it was. Metrics still go over OTLP. -
OTLP to
localhost: keepappender: :otlpandmetric: :otlpat their default,http://localhost:4318. Add the log appender with a bounded queue that drops rather than blocks, so that a Collector that is down never stalls the application:SemanticLogger.add_appender(appender: :otlp, non_blocking: true)Its queue holds 10,000 entries by default (
max_queue_size:); once it is full, the entries dropped are counted, and reported on the internal logger. Afterasync_max_retriesfailures in a row, 100 by default, the appender stops for good, until the process restarts: giveasync_max_retries: -1to keep trying through a long outage of the Collector.