OTel to ClickHouse analytics bridge
Analytics Pipeline — OTel Collector → ClickHouse¶
Architecture Overview¶
Browser
↓
Backend Service (inside cluster)
↓ HTTP POST /v1/logs
↓ scope.name = "playtelly.analytics"
OTel Collector (monitoring namespace)
↓ filter: only playtelly.analytics
↓ batch: 5000 rows / 5s
ClickHouse — analytics.otel_logs (landing table, TTL 30 days)
↓ Materialized View (automatic, zero latency)
ClickHouse — analytics.events (query target, kept forever)
↑
Grafana (ch_grafana, read only)
Files¶
| File | Purpose | Namespace |
|---|---|---|
00-otel-collector-config-original.yaml |
Backup of original config | monitoring |
01-otel-bridge-migration.yaml |
Creates otel_logs table + MV |
analytic |
02-otel-collector-config.yaml |
Updated OTel Collector config | monitoring |
Tables Created¶
analytics.otel_logs — Landing table¶
Receives data from OTel Collector exporter. Schema matches OTel exporter expectations.
CREATE TABLE analytics.otel_logs
(
Timestamp DateTime64(9),
TraceId String,
SpanId String,
TraceFlags UInt32,
SeverityText LowCardinality(String),
SeverityNumber Int32,
ServiceName LowCardinality(String),
Body String,
ResourceSchemaUrl String,
ResourceAttributes Map(LowCardinality(String), String),
ScopeSchemaUrl String,
ScopeName String,
ScopeVersion String,
ScopeAttributes Map(LowCardinality(String), String),
LogAttributes Map(LowCardinality(String), String)
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(Timestamp)
ORDER BY (ServiceName, Timestamp)
TTL toDateTime(Timestamp) + INTERVAL 30 DAY
SETTINGS index_granularity = 8192;
TTL 30 days — raw OTel data auto-deleted after 30 days. Processed data lives in analytics.events forever.
analytics.mv_otel_to_events — Materialized View (bridge)¶
Fires automatically on every INSERT into otel_logs. Transforms OTel fields into analytics.events schema.
CREATE MATERIALIZED VIEW analytics.mv_otel_to_events
TO analytics.events
AS
SELECT
toDateTime(Timestamp) AS timestamp,
Body AS event,
LogAttributes['user_id'] AS user_id,
LogAttributes['session_id'] AS session_id,
LogAttributes['device_type'] AS device_type,
toJSONString(
mapFilter(
(k, v) -> k NOT IN ('user_id', 'session_id', 'device_type'),
LogAttributes
)
) AS properties
FROM analytics.otel_logs
WHERE Body != '';
Field mapping:
| OTel Field | analytics.events Column |
|---|---|
Body |
event |
LogAttributes['user_id'] |
user_id |
LogAttributes['session_id'] |
session_id |
LogAttributes['device_type'] |
device_type |
remaining LogAttributes |
properties (JSON) |
OTel Collector Changes¶
Image upgrade¶
Why upgrade was needed¶
spanmetrics was removed from processors in newer versions and moved to connectors. Version 0.91.0 did not support the new filter syntax either.
What changed in config¶
| Component | Before (0.91.0) | After (0.104.0) |
|---|---|---|
spanmetrics |
under processors |
under connectors |
| traces pipeline | processors: [spanmetrics] |
exporters: [spanmetrics] |
| metrics pipeline | no spanmetrics receiver | receivers: [spanmetrics] |
| clickhouse exporter | not present | added |
| filter/analytics | not present | added |
| logs/analytics pipeline | not present | added |
New components added¶
processors:
batch/clickhouse: # larger batches for ClickHouse (5000 rows)
timeout: 5s
send_batch_size: 5000
filter/analytics: # only pass playtelly.analytics scope
error_mode: ignore
logs:
log_record:
- 'instrumentation_scope.name != "playtelly.analytics"'
connectors:
spanmetrics: # moved from processors
histogram:
explicit:
buckets: [...]
exporters:
clickhouse: # new — sends to ClickHouse
endpoint: tcp://clickhouse-svc.analytic.svc.cluster.local:9000
database: analytics
username: ch_app
logs_table_name: otel_logs
create_schema: false # we own the schema
pipelines:
logs/analytics: # new pipeline
receivers: [otlp]
processors: [filter/analytics, batch/clickhouse]
exporters: [clickhouse]
OTLP Payload Format¶
Your app must send logs in this format for the pipeline to work:
POST /v1/logs
{
"resourceLogs": [{
"resource": { "attributes": [] },
"scopeLogs": [{
"scope": { "name": "playtelly.analytics" },
"logRecords": [{
"timeUnixNano": "1781781697000000000",
"severityNumber": 9,
"severityText": "INFO",
"body": { "stringValue": "page_view" },
"attributes": [
{ "key": "user_id", "value": { "stringValue": "uuid-here" } },
{ "key": "session_id", "value": { "stringValue": "uuid-here" } },
{ "key": "device_type", "value": { "stringValue": "Desktop" } }
]
}]
}]
}]
}
Key requirement: scope.name must be "playtelly.analytics" — the filter drops everything else.
Endpoints¶
| Use Case | Endpoint |
|---|---|
| Backend inside cluster | http://otel-collector-service.monitoring.svc.cluster.local:4318/v1/logs |
| External / browser | https://collector.johor.gov.my/v1/logs |
Important: Backend services running inside the cluster must use the internal DNS. Using the external domain from inside the cluster fails due to hairpin NAT — the server cannot reach its own public IP.
Deployment Order (fresh install)¶
# 1. Create bridge tables in ClickHouse
kubectl apply -f 01-otel-bridge-migration.yaml
kubectl logs -n analytic -l app=clickhouse-migration -f
# Expected: otel_logs, mv_otel_to_events created
# 2. Update OTel Collector config + image
kubectl apply -f 02-otel-collector-config.yaml
kubectl set image deployment/otel-collector \
otel-collector=otel/opentelemetry-collector-contrib:0.104.0 \
-n monitoring
# 3. Verify collector is running
kubectl get pods -n monitoring | grep otel
kubectl logs deployment/otel-collector -n monitoring | grep -i "error" | tail -5
Verify Data is Flowing¶
# Send test event
kubectl run test-otel --image=curlimages/curl --rm -it --restart=Never -n monitoring -- \
curl -X POST http://otel-collector-service.monitoring.svc.cluster.local:4318/v1/logs \
-H "Content-Type: application/json" \
-d '{"resourceLogs":[{"resource":{"attributes":[]},"scopeLogs":[{"scope":{"name":"playtelly.analytics"},"logRecords":[{"timeUnixNano":"1781781697000000000","severityNumber":9,"body":{"stringValue":"test_event"},"attributes":[{"key":"user_id","value":{"stringValue":"test-123"}},{"key":"session_id","value":{"stringValue":"sess-456"}},{"key":"device_type","value":{"stringValue":"Desktop"}}]}]}]}]}'
# <CH_ADMIN_PASSWORD> is the admin_password key of the ch-credentials Secret (namespace analytic)
# Check otel_logs (landing)
kubectl exec -it clickhouse-0 -n analytic -- \
clickhouse-client --user ch_admin --password <CH_ADMIN_PASSWORD> \
--query "SELECT count() FROM analytics.otel_logs"
# Check events (after MV)
kubectl exec -it clickhouse-0 -n analytic -- \
clickhouse-client --user ch_admin --password <CH_ADMIN_PASSWORD> \
--query "SELECT event, count() FROM analytics.events GROUP BY event"
Querying Analytics Data¶
Always query analytics.events — never otel_logs directly.
-- Event counts by type
SELECT event, count()
FROM analytics.events
GROUP BY event
ORDER BY count() DESC;
-- Daily active users
SELECT toDate(timestamp) AS date, uniq(user_id) AS dau
FROM analytics.events
GROUP BY date
ORDER BY date DESC;
-- Page views last 7 days
SELECT toDate(timestamp) AS date, count() AS views
FROM analytics.events
WHERE event = 'page_view'
AND timestamp >= now() - INTERVAL 7 DAY
GROUP BY date
ORDER BY date;
-- Device breakdown
SELECT device_type, count()
FROM analytics.events
GROUP BY device_type
ORDER BY count() DESC;
Troubleshooting¶
| Symptom | Check |
|---|---|
| Collector crashing | kubectl logs deployment/otel-collector -n monitoring \| tail -20 |
create database error |
Add create_schema: false to clickhouse exporter |
spanmetrics unknown processor |
Wrong image version — upgrade to 0.104.0 |
otel_logs has data but events empty |
Check MV: SHOW CREATE TABLE analytics.mv_otel_to_events |
| Filter dropping all logs | Verify scope.name = "playtelly.analytics" in payload |
| Backend can't reach external collector | Use internal DNS instead of external domain (hairpin NAT) |
| 0 rows after sending | Wait for batch timeout (5s) then recheck |
Known Issues & Lessons Learned¶
| Issue | Root Cause | Fix |
|---|---|---|
spanmetrics unknown processor |
Removed in 0.96.0+, moved to connectors | Use connectors: section, add to traces exporters and metrics receivers |
instrumentation_libraries invalid key |
Old filter syntax, changed across versions | Use OTTL: log_record: - 'instrumentation_scope.name != "..."' |
create_schema invalid key |
Not supported in 0.91.0 | Remove it; add back as create_schema: false in 0.104.0 |
ch_app not enough privileges |
Exporter tried to CREATE DATABASE | Set create_schema: false — exporter only INSERTs |
| TTL on DateTime64 fails | TTL requires DateTime not DateTime64 | Wrap with toDateTime(Timestamp) |
| Backend can't reach public URL | Hairpin NAT — server can't reach own public IP | Use internal cluster DNS |
Future Work¶
- Add more event types beyond
page_viewandheartbeat - Build Grafana dashboards on
analytics.events - Add
access_logspipeline from Kong/APISIX - Consider adding retention policy for
analytics.events