Repository navigation
feat(api): record ClickHouse version on proxy spans #3843
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| feat: record the ClickHouse server version on ClickHouse proxy spans, as | ||
| `hyperdx.clickhouse_proxy.clickhouse.version`, so query errors and latency can | ||
| be compared across ClickHouse upgrades. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { | ||
| Metadata, | ||
| MetadataCache, | ||
| } from '@hyperdx/common-utils/dist/core/metadata'; | ||
| import type { Connection } from '@hyperdx/common-utils/dist/types'; | ||
|
|
||
| import { ClickhouseClient } from '@/clickhouse'; | ||
|
|
||
| // Short, because the point is to see a server change version after an upgrade. | ||
| const VERSION_TTL_MS = 60_000; | ||
| const VERSION_TIMEOUT_MS = 5_000; | ||
|
|
||
| const versionCache = new MetadataCache(); | ||
|
|
||
| type VersionedConnection = Pick< | ||
| Connection, | ||
| 'id' | 'host' | 'username' | 'password' | ||
| >; | ||
|
|
||
| // '' marks a failed read, so an unreachable server is not asked on every request. | ||
| async function readVersion(connection: VersionedConnection): Promise<string> { | ||
| let client: ClickhouseClient | undefined; | ||
| try { | ||
| client = new ClickhouseClient({ | ||
| host: connection.host, | ||
| username: connection.username, | ||
| password: connection.password, | ||
| requestTimeout: VERSION_TIMEOUT_MS, | ||
| }); | ||
| const version = await new Metadata( | ||
| client, | ||
| new MetadataCache(), | ||
| ).getServerVersion({ connectionId: connection.id }); | ||
| return version?.join('.') ?? ''; | ||
| } catch { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 P2 — Recorded version is the parsed and padded tuple, not what
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| return ''; | ||
| } finally { | ||
| void client?.close(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The connection's ClickHouse version if it is cached, starting a lookup | ||
| * otherwise so a later call has it. Never waits on ClickHouse. | ||
| */ | ||
| export function cachedClickHouseVersion( | ||
| connection: VersionedConnection, | ||
| ): string | undefined { | ||
| // Keyed on the host as well, so editing a connection's host drops the answer. | ||
| const key = `${connection.id}|${connection.host}`; | ||
| const version = versionCache.get<string>(key); | ||
| if (version === undefined) { | ||
| void versionCache.getOrFetch(key, () => readVersion(connection), { | ||
| ttlMs: VERSION_TTL_MS, | ||
| }); | ||
| } | ||
| return version || undefined; | ||
| } | ||
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.
🟡 P2 — Module-level version cache never evicts entries for connections that stop being used
MetadataCache(packages/common-utils/src/core/metadata.ts:331) only deletes an expired entry whenget()is called again with the same key. Every${id}|${host}pair ever proxied therefore stays inversionCachefor the life of the pod, including deleted connections and every old host left behind by an edit (the key includes the host on purpose). On a long-lived multi-tenant API pod this grows without bound. Fix: use a bounded LRU/TTL map, or sweep expired entries when a new one is written.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond