|
| 1 | +import { createClient } from '@clickhouse/client'; |
| 2 | +import { ClickHouseClient } from '@clickhouse/client-common'; |
| 3 | + |
| 4 | +import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node'; |
| 5 | +import { Metadata, MetadataCache } from '@/metadata'; |
| 6 | +import { ChartConfigWithDateRange } from '@/types'; |
| 7 | + |
| 8 | +describe('Metadata Integration Tests', () => { |
| 9 | + let client: ClickHouseClient; |
| 10 | + let hdxClient: HdxClickhouseClient; |
| 11 | + |
| 12 | + beforeAll(() => { |
| 13 | + const host = process.env.CLICKHOUSE_HOST || 'http://localhost:8123'; |
| 14 | + const username = process.env.CLICKHOUSE_USER || 'default'; |
| 15 | + const password = process.env.CLICKHOUSE_PASSWORD || ''; |
| 16 | + |
| 17 | + client = createClient({ |
| 18 | + url: host, |
| 19 | + username, |
| 20 | + password, |
| 21 | + }); |
| 22 | + |
| 23 | + hdxClient = new HdxClickhouseClient({ |
| 24 | + host, |
| 25 | + username, |
| 26 | + password, |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('getKeyValues', () => { |
| 31 | + let metadata: Metadata; |
| 32 | + const chartConfig: ChartConfigWithDateRange = { |
| 33 | + connection: 'test_connection', |
| 34 | + from: { |
| 35 | + databaseName: 'default', |
| 36 | + tableName: 'test_table', |
| 37 | + }, |
| 38 | + dateRange: [new Date('2023-01-01'), new Date('2025-01-01')], |
| 39 | + select: 'col1, col2', |
| 40 | + timestampValueExpression: 'Timestamp', |
| 41 | + where: '', |
| 42 | + }; |
| 43 | + |
| 44 | + beforeAll(async () => { |
| 45 | + await client.command({ |
| 46 | + query: `CREATE OR REPLACE TABLE default.test_table ( |
| 47 | + Timestamp DateTime64(9) CODEC(Delta(8), ZSTD(1)), |
| 48 | + SeverityText LowCardinality(String) CODEC(ZSTD(1)), |
| 49 | + TraceId String, |
| 50 | + LogAttributes JSON CODEC(ZSTD(1)), |
| 51 | + ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)), |
| 52 | + \`__hdx_materialized_k8s.pod.name\` String MATERIALIZED ResourceAttributes['k8s.pod.name'] CODEC(ZSTD(1)), |
| 53 | + ) |
| 54 | + ENGINE = MergeTree() |
| 55 | + ORDER BY (Timestamp) |
| 56 | + `, |
| 57 | + }); |
| 58 | + |
| 59 | + await client.command({ |
| 60 | + query: `INSERT INTO default.test_table (Timestamp, SeverityText, TraceId, ResourceAttributes, LogAttributes) VALUES |
| 61 | + ('2023-06-01 12:00:00', 'info', '1o2udn120d8n', { 'k8s.pod.name': 'pod1', 'env': 'prod' },'{"action":"ping"}'), |
| 62 | + ('2024-06-01 12:00:00', 'error', '67890-09098', { 'k8s.pod.name': 'pod2', 'env': 'prod' },'{}'), |
| 63 | + ('2024-06-01 12:00:00', 'info', '11h9238re1h92', { 'env': 'staging' },'{"user":"john"}'), |
| 64 | + ('2024-06-01 12:00:00', 'warning', '1o2udn120d8n', { 'k8s.pod.name': 'pod1', 'env': 'prod' }, '{"user":"jack","action":"login"}'), |
| 65 | + ('2024-06-01 12:00:00', '', '1o2udn120d8n', { 'env': 'prod' }, '{"user":"jane","action":"login"}') |
| 66 | + `, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + beforeEach(async () => { |
| 71 | + metadata = new Metadata(hdxClient, new MetadataCache()); |
| 72 | + }); |
| 73 | + |
| 74 | + afterAll(async () => { |
| 75 | + await client.command({ |
| 76 | + query: 'DROP TABLE IF EXISTS default.test_table', |
| 77 | + }); |
| 78 | + |
| 79 | + await client.close(); |
| 80 | + }); |
| 81 | + |
| 82 | + describe.each([true, false])('with disableRowLimit=%s', disableRowLimit => { |
| 83 | + it('should return key-value pairs for a given metadata key', async () => { |
| 84 | + const resultSeverityText = await metadata.getKeyValues({ |
| 85 | + chartConfig, |
| 86 | + keys: ['SeverityText'], |
| 87 | + disableRowLimit, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(resultSeverityText).toHaveLength(1); |
| 91 | + expect(resultSeverityText[0].key).toBe('SeverityText'); |
| 92 | + expect(resultSeverityText[0].value).toHaveLength(3); |
| 93 | + expect(resultSeverityText[0].value).toEqual( |
| 94 | + expect.arrayContaining(['info', 'error', 'warning']), |
| 95 | + ); |
| 96 | + |
| 97 | + const resultTraceId = await metadata.getKeyValues({ |
| 98 | + chartConfig, |
| 99 | + keys: ['TraceId'], |
| 100 | + disableRowLimit, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(resultTraceId).toHaveLength(1); |
| 104 | + expect(resultTraceId[0].key).toBe('TraceId'); |
| 105 | + expect(resultTraceId[0].value).toHaveLength(3); |
| 106 | + expect(resultTraceId[0].value).toEqual( |
| 107 | + expect.arrayContaining([ |
| 108 | + '1o2udn120d8n', |
| 109 | + '67890-09098', |
| 110 | + '11h9238re1h92', |
| 111 | + ]), |
| 112 | + ); |
| 113 | + |
| 114 | + const resultBoth = await metadata.getKeyValues({ |
| 115 | + chartConfig, |
| 116 | + keys: ['TraceId', 'SeverityText'], |
| 117 | + disableRowLimit, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(resultBoth).toEqual([ |
| 121 | + { |
| 122 | + key: 'TraceId', |
| 123 | + value: expect.arrayContaining([ |
| 124 | + '1o2udn120d8n', |
| 125 | + '67890-09098', |
| 126 | + '11h9238re1h92', |
| 127 | + ]), |
| 128 | + }, |
| 129 | + { |
| 130 | + key: 'SeverityText', |
| 131 | + value: expect.arrayContaining(['info', 'error', 'warning']), |
| 132 | + }, |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should handle materialized columns correctly', async () => { |
| 137 | + const resultPodName = await metadata.getKeyValues({ |
| 138 | + chartConfig, |
| 139 | + keys: ['__hdx_materialized_k8s.pod.name'], |
| 140 | + disableRowLimit, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(resultPodName).toHaveLength(1); |
| 144 | + expect(resultPodName[0].key).toBe('__hdx_materialized_k8s.pod.name'); |
| 145 | + expect(resultPodName[0].value).toHaveLength(2); |
| 146 | + expect(resultPodName[0].value).toEqual( |
| 147 | + expect.arrayContaining(['pod1', 'pod2']), |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should handle JSON columns correctly', async () => { |
| 152 | + const resultLogAttributes = await metadata.getKeyValues({ |
| 153 | + chartConfig, |
| 154 | + keys: ['LogAttributes.user'], |
| 155 | + disableRowLimit, |
| 156 | + }); |
| 157 | + |
| 158 | + expect(resultLogAttributes).toHaveLength(1); |
| 159 | + expect(resultLogAttributes[0].key).toBe('LogAttributes.user'); |
| 160 | + expect(resultLogAttributes[0].value).toHaveLength(3); |
| 161 | + expect(resultLogAttributes[0].value).toEqual( |
| 162 | + expect.arrayContaining(['john', 'jack', 'jane']), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return an empty list when no keys are provided', async () => { |
| 167 | + const resultEmpty = await metadata.getKeyValues({ |
| 168 | + chartConfig, |
| 169 | + keys: [], |
| 170 | + }); |
| 171 | + |
| 172 | + expect(resultEmpty).toEqual([]); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should correctly limit the number of returned values', async () => { |
| 176 | + const resultLimited = await metadata.getKeyValues({ |
| 177 | + chartConfig, |
| 178 | + keys: ['SeverityText'], |
| 179 | + limit: 2, |
| 180 | + }); |
| 181 | + |
| 182 | + expect(resultLimited).toHaveLength(1); |
| 183 | + expect(resultLimited[0].key).toBe('SeverityText'); |
| 184 | + expect(resultLimited[0].value).toHaveLength(2); |
| 185 | + expect( |
| 186 | + resultLimited[0].value.every(v => |
| 187 | + ['info', 'error', 'warning'].includes(v), |
| 188 | + ), |
| 189 | + ).toBeTruthy(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments