73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
import { createTable, runClickhouseMigrationCommands } from '../src/clickhouse/migration';
|
|
import { getIsCluster, printBoxMessage } from './helpers';
|
|
|
|
export async function up() {
|
|
const replicatedVersion = '1';
|
|
const isClustered = getIsCluster();
|
|
|
|
const sqls: string[] = [];
|
|
|
|
sqls.push(
|
|
...createTable({
|
|
name: 'logs',
|
|
columns: [
|
|
'`id` UUID DEFAULT generateUUIDv4()',
|
|
'`project_id` String CODEC(ZSTD(3))',
|
|
'`device_id` String CODEC(ZSTD(3))',
|
|
'`profile_id` String CODEC(ZSTD(3))',
|
|
'`session_id` String CODEC(LZ4)',
|
|
// OpenTelemetry log fields
|
|
'`timestamp` DateTime64(9) CODEC(DoubleDelta, ZSTD(3))',
|
|
'`observed_at` DateTime64(9) CODEC(DoubleDelta, ZSTD(3))',
|
|
'`severity_number` UInt8',
|
|
'`severity_text` LowCardinality(String)',
|
|
'`body` String CODEC(ZSTD(3))',
|
|
'`trace_id` String CODEC(ZSTD(3))',
|
|
'`span_id` String CODEC(ZSTD(3))',
|
|
'`trace_flags` UInt32 DEFAULT 0',
|
|
'`logger_name` LowCardinality(String)',
|
|
// OTel attributes (log-level key-value pairs)
|
|
'`attributes` Map(String, String) CODEC(ZSTD(3))',
|
|
// OTel resource attributes (device/app metadata)
|
|
'`resource` Map(String, String) CODEC(ZSTD(3))',
|
|
// Server-enriched context
|
|
'`sdk_name` LowCardinality(String)',
|
|
'`sdk_version` LowCardinality(String)',
|
|
'`country` LowCardinality(FixedString(2))',
|
|
'`city` String',
|
|
'`region` LowCardinality(String)',
|
|
'`os` LowCardinality(String)',
|
|
'`os_version` LowCardinality(String)',
|
|
'`browser` LowCardinality(String)',
|
|
'`browser_version` LowCardinality(String)',
|
|
'`device` LowCardinality(String)',
|
|
'`brand` LowCardinality(String)',
|
|
'`model` LowCardinality(String)',
|
|
],
|
|
indices: [
|
|
'INDEX idx_severity_number severity_number TYPE minmax GRANULARITY 1',
|
|
'INDEX idx_body body TYPE tokenbf_v1(32768, 3, 0) GRANULARITY 1',
|
|
'INDEX idx_trace_id trace_id TYPE bloom_filter GRANULARITY 1',
|
|
'INDEX idx_logger_name logger_name TYPE bloom_filter GRANULARITY 1',
|
|
],
|
|
orderBy: ['project_id', 'toDate(timestamp)', 'severity_number', 'device_id'],
|
|
partitionBy: 'toYYYYMM(timestamp)',
|
|
settings: {
|
|
index_granularity: 8192,
|
|
ttl_only_drop_parts: 1,
|
|
},
|
|
distributionHash: 'cityHash64(project_id, toString(toStartOfHour(timestamp)))',
|
|
replicatedVersion,
|
|
isClustered,
|
|
}),
|
|
);
|
|
|
|
printBoxMessage('Running migration: 13-add-logs', [
|
|
'Creates the logs table for OpenTelemetry-compatible device/app log capture.',
|
|
]);
|
|
|
|
if (!process.argv.includes('--dry')) {
|
|
await runClickhouseMigrationCommands(sqls);
|
|
}
|
|
}
|