-
Notifications
You must be signed in to change notification settings - Fork 323
perf: Improve getKeyValues query performance for JSON keys #1284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5c77da
perf: Improve getKeyValues query performance for JSON keys
pulpdrew 23f37db
test: Add integration tests for common-utils/metadata
pulpdrew d417159
fix: Close client in metadata integration test
pulpdrew 2dc77ff
fix: Improve getKeyValues comments
pulpdrew 4945803
Merge branch 'main' into drew/optimize-filter-sampling
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@hyperdx/common-utils": patch | ||
| --- | ||
|
|
||
| perf: Improve getKeyValues query performance for JSON keys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| CLICKHOUSE_HOST=http://localhost:8123 | ||
| CLICKHOUSE_PASSWORD= | ||
| CLICKHOUSE_USER=default | ||
| NODE_ENV=test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
| module.exports = { | ||
| setupFiles: ['dotenv/config'], | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| verbose: true, | ||
| rootDir: './src', | ||
| testMatch: ['**/__tests__/*.int.test.ts?(x)'], | ||
| testTimeout: 30000, | ||
| moduleNameMapper: { | ||
| '@/(.*)$': '<rootDir>/$1', | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
packages/common-utils/src/__tests__/metadata.int.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { createClient } from '@clickhouse/client'; | ||
| import { ClickHouseClient } from '@clickhouse/client-common'; | ||
|
|
||
| import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node'; | ||
| import { Metadata, MetadataCache } from '@/metadata'; | ||
| import { ChartConfigWithDateRange } from '@/types'; | ||
|
|
||
| describe('Metadata Integration Tests', () => { | ||
| let client: ClickHouseClient; | ||
| let hdxClient: HdxClickhouseClient; | ||
|
|
||
| beforeAll(() => { | ||
| const host = process.env.CLICKHOUSE_HOST || 'http://localhost:8123'; | ||
| const username = process.env.CLICKHOUSE_USER || 'default'; | ||
| const password = process.env.CLICKHOUSE_PASSWORD || ''; | ||
|
|
||
| client = createClient({ | ||
| url: host, | ||
| username, | ||
| password, | ||
| }); | ||
|
|
||
| hdxClient = new HdxClickhouseClient({ | ||
| host, | ||
| username, | ||
| password, | ||
| }); | ||
| }); | ||
|
|
||
| describe('getKeyValues', () => { | ||
| let metadata: Metadata; | ||
| const chartConfig: ChartConfigWithDateRange = { | ||
| connection: 'test_connection', | ||
| from: { | ||
| databaseName: 'default', | ||
| tableName: 'test_table', | ||
| }, | ||
| dateRange: [new Date('2023-01-01'), new Date('2025-01-01')], | ||
| select: 'col1, col2', | ||
| timestampValueExpression: 'Timestamp', | ||
| where: '', | ||
| }; | ||
|
|
||
| beforeAll(async () => { | ||
| await client.command({ | ||
| query: `CREATE OR REPLACE TABLE default.test_table ( | ||
| Timestamp DateTime64(9) CODEC(Delta(8), ZSTD(1)), | ||
| SeverityText LowCardinality(String) CODEC(ZSTD(1)), | ||
| TraceId String, | ||
| LogAttributes JSON CODEC(ZSTD(1)), | ||
| ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)), | ||
| \`__hdx_materialized_k8s.pod.name\` String MATERIALIZED ResourceAttributes['k8s.pod.name'] CODEC(ZSTD(1)), | ||
| ) | ||
| ENGINE = MergeTree() | ||
| ORDER BY (Timestamp) | ||
| `, | ||
| }); | ||
|
|
||
| await client.command({ | ||
| query: `INSERT INTO default.test_table (Timestamp, SeverityText, TraceId, ResourceAttributes, LogAttributes) VALUES | ||
| ('2023-06-01 12:00:00', 'info', '1o2udn120d8n', { 'k8s.pod.name': 'pod1', 'env': 'prod' },'{"action":"ping"}'), | ||
| ('2024-06-01 12:00:00', 'error', '67890-09098', { 'k8s.pod.name': 'pod2', 'env': 'prod' },'{}'), | ||
| ('2024-06-01 12:00:00', 'info', '11h9238re1h92', { 'env': 'staging' },'{"user":"john"}'), | ||
| ('2024-06-01 12:00:00', 'warning', '1o2udn120d8n', { 'k8s.pod.name': 'pod1', 'env': 'prod' }, '{"user":"jack","action":"login"}'), | ||
| ('2024-06-01 12:00:00', '', '1o2udn120d8n', { 'env': 'prod' }, '{"user":"jane","action":"login"}') | ||
| `, | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| metadata = new Metadata(hdxClient, new MetadataCache()); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await client.command({ | ||
| query: 'DROP TABLE IF EXISTS default.test_table', | ||
| }); | ||
|
|
||
| await client.close(); | ||
| }); | ||
|
|
||
| describe.each([true, false])('with disableRowLimit=%s', disableRowLimit => { | ||
| it('should return key-value pairs for a given metadata key', async () => { | ||
| const resultSeverityText = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['SeverityText'], | ||
| disableRowLimit, | ||
| }); | ||
|
|
||
| expect(resultSeverityText).toHaveLength(1); | ||
| expect(resultSeverityText[0].key).toBe('SeverityText'); | ||
| expect(resultSeverityText[0].value).toHaveLength(3); | ||
| expect(resultSeverityText[0].value).toEqual( | ||
| expect.arrayContaining(['info', 'error', 'warning']), | ||
| ); | ||
|
|
||
| const resultTraceId = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['TraceId'], | ||
| disableRowLimit, | ||
| }); | ||
|
|
||
| expect(resultTraceId).toHaveLength(1); | ||
| expect(resultTraceId[0].key).toBe('TraceId'); | ||
| expect(resultTraceId[0].value).toHaveLength(3); | ||
| expect(resultTraceId[0].value).toEqual( | ||
| expect.arrayContaining([ | ||
| '1o2udn120d8n', | ||
| '67890-09098', | ||
| '11h9238re1h92', | ||
| ]), | ||
| ); | ||
|
|
||
| const resultBoth = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['TraceId', 'SeverityText'], | ||
| disableRowLimit, | ||
| }); | ||
|
|
||
| expect(resultBoth).toEqual([ | ||
| { | ||
| key: 'TraceId', | ||
| value: expect.arrayContaining([ | ||
| '1o2udn120d8n', | ||
| '67890-09098', | ||
| '11h9238re1h92', | ||
| ]), | ||
| }, | ||
| { | ||
| key: 'SeverityText', | ||
| value: expect.arrayContaining(['info', 'error', 'warning']), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle materialized columns correctly', async () => { | ||
| const resultPodName = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['__hdx_materialized_k8s.pod.name'], | ||
| disableRowLimit, | ||
| }); | ||
|
|
||
| expect(resultPodName).toHaveLength(1); | ||
| expect(resultPodName[0].key).toBe('__hdx_materialized_k8s.pod.name'); | ||
| expect(resultPodName[0].value).toHaveLength(2); | ||
| expect(resultPodName[0].value).toEqual( | ||
| expect.arrayContaining(['pod1', 'pod2']), | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle JSON columns correctly', async () => { | ||
| const resultLogAttributes = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['LogAttributes.user'], | ||
| disableRowLimit, | ||
| }); | ||
|
|
||
| expect(resultLogAttributes).toHaveLength(1); | ||
| expect(resultLogAttributes[0].key).toBe('LogAttributes.user'); | ||
| expect(resultLogAttributes[0].value).toHaveLength(3); | ||
| expect(resultLogAttributes[0].value).toEqual( | ||
| expect.arrayContaining(['john', 'jack', 'jane']), | ||
| ); | ||
| }); | ||
|
|
||
| it('should return an empty list when no keys are provided', async () => { | ||
| const resultEmpty = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: [], | ||
| }); | ||
|
|
||
| expect(resultEmpty).toEqual([]); | ||
| }); | ||
|
|
||
| it('should correctly limit the number of returned values', async () => { | ||
| const resultLimited = await metadata.getKeyValues({ | ||
| chartConfig, | ||
| keys: ['SeverityText'], | ||
| limit: 2, | ||
| }); | ||
|
|
||
| expect(resultLimited).toHaveLength(1); | ||
| expect(resultLimited[0].key).toBe('SeverityText'); | ||
| expect(resultLimited[0].value).toHaveLength(2); | ||
| expect( | ||
| resultLimited[0].value.every(v => | ||
| ['info', 'error', 'warning'].includes(v), | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@teeohhem You mentioned our filter queries are fragile - this PR adds integration tests so that we can actually test the queries against real ClickHouse data. Hopefully that will help reduce some of the fragility. I think it would be great if we could extend these tests to cover more of our query generation code from common-utils in the future (eg. all of
renderChartConfig).