Skip to content

Commit 5236502

Browse files
committed
feat: add health check method to ClickHouse adapter for connection status and server info
1 parent 37d519e commit 5236502

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,56 @@ public function getName(): string
149149
return 'ClickHouse';
150150
}
151151

152+
/**
153+
* Check ClickHouse connection health and get server information.
154+
*
155+
* @return array{healthy: bool, host: string, port: int, database: string, secure: bool, version?: string, uptime?: int, error?: string, response_time?: float}
156+
*/
157+
public function healthCheck(): array
158+
{
159+
$startTime = microtime(true);
160+
$result = [
161+
'healthy' => false,
162+
'host' => $this->host,
163+
'port' => $this->port,
164+
'database' => $this->database,
165+
'secure' => $this->secure,
166+
];
167+
168+
try {
169+
// Simple connectivity test
170+
$response = $this->query('SELECT 1 as ping FORMAT JSON');
171+
$json = json_decode($response, true);
172+
173+
if (!is_array($json) || !isset($json['data'][0]['ping'])) {
174+
$result['error'] = 'Invalid response format';
175+
return $result;
176+
}
177+
178+
// Get server version and uptime
179+
try {
180+
$versionResponse = $this->query('SELECT version() as version, uptime() as uptime FORMAT JSON');
181+
$versionJson = json_decode($versionResponse, true);
182+
183+
if (is_array($versionJson) && isset($versionJson['data'][0])) {
184+
$result['version'] = (string) $versionJson['data'][0]['version'];
185+
$result['uptime'] = (int) $versionJson['data'][0]['uptime'];
186+
}
187+
} catch (Exception $e) {
188+
// Version info is optional, don't fail health check
189+
}
190+
191+
$result['healthy'] = true;
192+
$result['response_time'] = round(microtime(true) - $startTime, 3);
193+
194+
return $result;
195+
} catch (Exception $e) {
196+
$result['error'] = $e->getMessage();
197+
$result['response_time'] = round(microtime(true) - $startTime, 3);
198+
return $result;
199+
}
200+
}
201+
152202
/**
153203
* Validate host parameter.
154204
*

0 commit comments

Comments
 (0)