-
Notifications
You must be signed in to change notification settings - Fork 0
DT-61: Implement ClickHouse client and create necessary tables and views #62
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
7 commits
Select commit
Hold shift + click to select a range
edf4dbb
feat(clickhouse): Implement ClickHouse client and create necessary ta…
abdotop 27d7232
feat(logging): Enhance ClickHouse logging functionality and add log i…
abdotop 014ad5e
feat(deployment): Add deployment management routes and enhance user s…
abdotop 770c06c
feat(schema): Update log schema to include context and remove severit…
abdotop b3e0b67
feat(logging): Add getLogs endpoint to retrieve logs from ClickHouse …
abdotop 54ee31c
feat(schema): Enhance log schema with additional fields and improve d…
abdotop a0e50df
feat(logging): Refactor number conversion functions for log trace and…
abdotop 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
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,166 @@ | ||
| import { createClient } from 'npm:@clickhouse/client' | ||
| import { | ||
| CLICKHOUSE_HOST, | ||
| CLICKHOUSE_PASSWORD, | ||
| CLICKHOUSE_USER, | ||
| } from './lib/env.ts' | ||
| import { respond } from './lib/response.ts' | ||
| import { log } from './lib/log.ts' | ||
| import { ARR, NUM, OBJ, optional, STR, UNION } from './lib/validator.ts' | ||
| import { Asserted } from './lib/router.ts' | ||
|
|
||
| const LogSchema = OBJ({ | ||
| timestamp: NUM('The timestamp of the log event'), | ||
| trace_id: NUM('A float64 representation of the trace ID'), | ||
| span_id: optional(NUM('A float64 representation of the span ID')), | ||
| severity_number: NUM('The severity number of the log event'), | ||
| attributes: optional(OBJ({}, 'A map of attributes')), | ||
| event_name: STR('The name of the event'), | ||
| service_version: optional(STR('Service version')), | ||
| service_instance_id: optional(STR('Service instance ID')), | ||
| }, 'A log event') | ||
| const LogsInputSchema = UNION( | ||
| LogSchema, | ||
| ARR(LogSchema, 'An array of log events'), | ||
| ) | ||
|
|
||
| type Log = Asserted<typeof LogSchema> | ||
| type LogsInput = Asserted<typeof LogsInputSchema> | ||
|
|
||
| const client = createClient({ | ||
| url: CLICKHOUSE_HOST, | ||
| username: CLICKHOUSE_USER, | ||
| password: CLICKHOUSE_PASSWORD, | ||
| compression: { | ||
| request: true, | ||
| response: true, | ||
| }, | ||
| clickhouse_settings: { | ||
| date_time_input_format: 'best_effort', | ||
| }, | ||
| }) | ||
|
|
||
| const numberToHex128 = (() => { | ||
| const alphabet = new TextEncoder().encode('0123456789abcdef') | ||
| const output = new Uint8Array(16) | ||
| const view = new DataView(new Uint8Array(8).buffer) | ||
| const dec = new TextDecoder() | ||
| return (id: number) => { | ||
| view.setFloat64(0, id, false) | ||
| let i = -1 | ||
| while (++i < 8) { | ||
| const x = view.getUint8(i) | ||
| output[i * 2] = alphabet[x >> 4] | ||
| output[i * 2 + 1] = alphabet[x & 0xF] | ||
| } | ||
| return dec.decode(output) | ||
| } | ||
| })() | ||
|
|
||
| async function insertLogs( | ||
| service_name: string, | ||
| data: LogsInput, | ||
| ) { | ||
| const logsToInsert = Array.isArray(data) ? data : [data] | ||
| if (logsToInsert.length === 0) throw respond.NoContent() | ||
|
|
||
| const rows = logsToInsert.map((log) => { | ||
| const traceHex = numberToHex128(log.trace_id) | ||
| const spanHex = numberToHex128(log.span_id ?? log.trace_id) | ||
| return { | ||
| ...log, | ||
| timestamp: new Date(log.timestamp), | ||
| attributes: log.attributes ?? {}, | ||
| service_name: service_name, | ||
| trace_id: traceHex, | ||
| span_id: spanHex, | ||
| } | ||
| }) | ||
|
|
||
| log.debug('Inserting logs into ClickHouse', { rows }) | ||
|
|
||
| try { | ||
| await client.insert({ table: 'logs', values: rows, format: 'JSONEachRow' }) | ||
| return respond.OK() | ||
| } catch (error) { | ||
| log.error('Error inserting logs into ClickHouse:', { error }) | ||
| throw respond.InternalServerError() | ||
|
Member
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. same here, shouldn't we just return in that case ? |
||
| } | ||
| } | ||
|
|
||
| async function getLogs({ | ||
| resource, | ||
| severity_number, | ||
| start_date, | ||
| end_date, | ||
| sort_by, | ||
| sort_order, | ||
| search, | ||
| }: { | ||
| resource: string | ||
| severity_number?: string | ||
| start_date?: string | ||
| end_date?: string | ||
| sort_by?: string | ||
| sort_order?: 'ASC' | 'DESC' | ||
| search?: Record<string, string> | ||
| }) { | ||
| const queryParts: string[] = [] | ||
| const queryParams: Record<string, unknown> = { service_name: resource } | ||
|
|
||
| queryParts.push('service_name = {service_name:String}') | ||
| queryParams.service_name = resource | ||
|
|
||
| if (severity_number) { | ||
| queryParts.push('severity_number = {severity_number:UInt8}') | ||
| queryParams.severity_number = severity_number | ||
| } | ||
|
|
||
| if (start_date) { | ||
| queryParts.push('timestamp >= {start_date:DateTime}') | ||
| queryParams.start_date = new Date(start_date) | ||
| } | ||
|
|
||
| if (end_date) { | ||
| queryParts.push('timestamp <= {end_date:DateTime}') | ||
| queryParams.end_date = new Date(end_date) | ||
| } | ||
|
|
||
| if (search) { | ||
| if (search.trace_id) { | ||
| queryParts.push('trace_id = {trace_id:String}') | ||
| queryParams.trace_id = search.trace_id | ||
| } | ||
| if (search.span_id) { | ||
| queryParts.push('span_id = {span_id:String}') | ||
| queryParams.span_id = search.span_id | ||
| } | ||
| if (search.event_name) { | ||
| queryParts.push('event_name = {event_name:String}') | ||
| queryParams.event_name = search.event_name | ||
| } | ||
| } | ||
|
|
||
| const query = ` | ||
| SELECT * | ||
| FROM logs | ||
| WHERE ${queryParts.join(' AND ')} | ||
| ${sort_by ? `ORDER BY ${sort_by} ${sort_order || 'DESC'}` : ''} | ||
| LIMIT 1000 | ||
| ` | ||
|
|
||
| try { | ||
| const resultSet = await client.query({ | ||
| query, | ||
| query_params: queryParams, | ||
| format: 'JSON', | ||
| }) | ||
|
|
||
| return (await resultSet.json<Log>()).data | ||
| } catch (error) { | ||
| log.error('Error querying logs from ClickHouse:', { error }) | ||
| throw respond.InternalServerError() | ||
| } | ||
| } | ||
|
|
||
| export { client, getLogs, insertLogs, LogSchema, LogsInputSchema } | ||
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
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
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.
is it ok to throw a response ?