Skip to content
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

Add Logger to the ResultSet in the Node.js implementation #265

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

## Bug fixes

- (Node.js only) The `command` method now drains the response stream properly, as the previous implementation could cause the Keep-Alive socket to close after each request.
- The `command` method now drains the response stream properly, as the previous implementation could cause the `Keep-Alive` socket to close after each request.
- Removed an unnecessary error log in the `ResultSet.stream` method if the request was aborted or the result set was closed ([#263](https://github.com/ClickHouse/clickhouse-js/issues/263)).

## Improvements

- `ResultSet.stream` logs an error via the `Logger` instance, if the stream emits an error event instead of a simple `console.error` call.
- Minor adjustments to the `DefaultLogger` log messages formatting.
- Added missing `rows_before_limit_at_least` to the ResponseJSON type ([@0237h](https://github.com/0237h), [#267](https://github.com/ClickHouse/clickhouse-js/issues/267)).

# 1.0.1 (Common, Node.js, Web)
Expand Down
16 changes: 15 additions & 1 deletion packages/client-common/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Connection,
ConnExecResult,
IsSame,
LogWriter,
MakeResultSet,
WithClickHouseSummary,
} from '@clickhouse/client-common'
Expand Down Expand Up @@ -116,6 +117,7 @@ export class ClickHouseClient<Stream = unknown> {
private readonly makeResultSet: MakeResultSet<Stream>
private readonly valuesEncoder: ValuesEncoder<Stream>
private readonly sessionId?: string
private readonly logWriter: LogWriter

constructor(
config: BaseClickHouseClientConfigOptions & ImplementationDetails<Stream>,
Expand All @@ -129,6 +131,7 @@ export class ClickHouseClient<Stream = unknown> {
config.impl.handle_specific_url_params ?? null,
)
const connectionParams = getConnectionParams(configWithURL, logger)
this.logWriter = connectionParams.log_writer
this.clientClickHouseSettings = connectionParams.clickhouse_settings
this.sessionId = config.session_id
this.connection = config.impl.make_connection(
Expand All @@ -155,7 +158,18 @@ export class ClickHouseClient<Stream = unknown> {
query,
...this.withClientQueryParams(params),
})
return this.makeResultSet(stream, format, query_id)
return this.makeResultSet(stream, format, query_id, (err) => {
this.logWriter.error({
err,
module: 'Client',
message: 'Error while processing the ResultSet.',
args: {
session_id: this.sessionId,
query,
query_id,
},
})
})
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/client-common/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type MakeResultSet<Stream> = <
stream: Stream,
format: Format,
query_id: string,
log_error: (err: Error) => void,
) => ResultSet

export interface ValuesEncoder<Stream> {
Expand Down
1 change: 1 addition & 0 deletions packages/client-common/src/error/parse_error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const errorRe =
/(Code|Error): (?<code>\d+).*Exception: (?<message>.+)\((?<type>(?=.+[A-Z]{3})[A-Z0-9_]+?)\)/s

interface ParsedClickHouseError {
message: string
code: string
Expand Down
14 changes: 7 additions & 7 deletions packages/client-common/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DefaultLogger implements Logger {
formatMessage({ module, message, level: 'TRACE' }),
]
if (args) {
params.push('Arguments:', args)
params.push('\nArguments:', args)
}
console.debug(...params)
}
Expand All @@ -30,7 +30,7 @@ export class DefaultLogger implements Logger {
formatMessage({ module, message, level: 'DEBUG' }),
]
if (args) {
params.push('Arguments:', args)
params.push('\nArguments:', args)
}
console.debug(...params)
}
Expand All @@ -40,7 +40,7 @@ export class DefaultLogger implements Logger {
formatMessage({ module, message, level: 'INFO' }),
]
if (args) {
params.push('Arguments:', args)
params.push('\nArguments:', args)
}
console.info(...params)
}
Expand All @@ -50,10 +50,10 @@ export class DefaultLogger implements Logger {
formatMessage({ module, message, level: 'WARN' }),
]
if (args) {
params.push('Arguments:', args)
params.push('\nArguments:', args)
}
if (err) {
params.push('Caused by:', err)
params.push('\nCaused by:', err)
}
console.warn(...params)
}
Expand All @@ -63,9 +63,9 @@ export class DefaultLogger implements Logger {
formatMessage({ module, message, level: 'ERROR' }),
]
if (args) {
params.push('Arguments:', args)
params.push('\nArguments:', args)
}
params.push('Caused by:', err)
params.push('\nCaused by:', err)
console.error(...params)
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/client-node/__tests__/integration/node_exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ describe('[Node.js] exec result streaming', () => {
default_format: format,
},
})
const rs = new ResultSet(stream, format, query_id)
const rs = ResultSet.instance({
stream,
format,
query_id,
log_error: (err) => {
console.error(err)
},
})
expect(await rs.json()).toEqual([{ number: '0' }])
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
import {

Check warning on line 1 in packages/client-node/__tests__/integration/node_logger_support.test.ts

View workflow job for this annotation

GitHub Actions / node-unit-tests (18)

Imports "ClickHouseClient", "ErrorLogParams", "Logger" and "LogParams" are only used as type

Check warning on line 1 in packages/client-node/__tests__/integration/node_logger_support.test.ts

View workflow job for this annotation

GitHub Actions / node-unit-tests (20)

Imports "ClickHouseClient", "ErrorLogParams", "Logger" and "LogParams" are only used as type

Check warning on line 1 in packages/client-node/__tests__/integration/node_logger_support.test.ts

View workflow job for this annotation

GitHub Actions / node-unit-tests (21)

Imports "ClickHouseClient", "ErrorLogParams", "Logger" and "LogParams" are only used as type
ClickHouseClient,
ClickHouseLogLevel,
ErrorLogParams,
Logger,
LogParams,
Expand All @@ -20,29 +21,25 @@
})

describe('Logger support', () => {
const logLevelKey = 'CLICKHOUSE_LOG_LEVEL'
let defaultLogLevel: string | undefined
beforeEach(() => {
defaultLogLevel = process.env[logLevelKey]
})
afterEach(() => {
if (defaultLogLevel === undefined) {
delete process.env[logLevelKey]
} else {
process.env[logLevelKey] = defaultLogLevel
}
})

it('should use the default logger implementation', async () => {
process.env[logLevelKey] = 'DEBUG'
client = createTestClient()
const consoleSpy = spyOn(console, 'log')
const infoSpy = spyOn(console, 'info')
client = createTestClient({
log: {
level: ClickHouseLogLevel.DEBUG,
},
})
expect(infoSpy).toHaveBeenCalledOnceWith(
jasmine.stringContaining('Log level is set to DEBUG'),
)

const debugSpy = spyOn(console, 'debug')
await client.ping()
// logs[0] are about current log level
expect(consoleSpy).toHaveBeenCalledOnceWith(
jasmine.stringContaining('Got a response from ClickHouse'),
expect(debugSpy).toHaveBeenCalledOnceWith(
jasmine.stringContaining('Ping: got a response from ClickHouse'),
jasmine.stringContaining('\nArguments:'),
jasmine.objectContaining({
request_headers: {
connection: jasmine.stringMatching(/Keep-Alive/i),
'user-agent': jasmine.any(String),
},
request_method: 'GET',
Expand All @@ -59,17 +56,17 @@
})

it('should provide a custom logger implementation', async () => {
process.env[logLevelKey] = 'DEBUG'
client = createTestClient({
log: {
level: ClickHouseLogLevel.DEBUG,
LoggerClass: TestLogger,
},
})
await client.ping()
// logs[0] are about current log level
expect(logs[1]).toEqual(
jasmine.objectContaining({
message: 'Got a response from ClickHouse',
message: 'Ping: got a response from ClickHouse',
args: jasmine.objectContaining({
request_path: '/ping',
request_method: 'GET',
Expand All @@ -79,10 +76,9 @@
})

it('should provide a custom logger implementation (but logs are disabled)', async () => {
process.env[logLevelKey] = 'OFF'
client = createTestClient({
log: {
// enable: false,
// the default level is OFF
LoggerClass: TestLogger,
},
})
Expand Down
Loading
Loading