This commit is contained in:
gr
2026-10-22 16:43:49 +08:00
17 changed files with 1179 additions and 54 deletions
@@ -14,10 +14,10 @@ final class AssistantSseTiming
private const NUMBER_FIELDS = [
'duration_ms', 'aggregate_ms', 'compaction_ms', 'retry_ms',
'source_bytes', 'query_bytes', 'attachment_count',
'delta_bytes', 'delta_count', 'first_delta_ms',
'delta_bytes', 'delta_count', 'first_delta_ms', 'inputs_bytes',
];
private const BOOLEAN_FIELDS = ['compaction_attempted', 'retry_attempted', 'first_delta_observed'];
private const BOOLEAN_FIELDS = ['compaction_attempted', 'retry_attempted', 'first_delta_observed', 'compaction_cache_hit'];
private const ERROR_CODES = [
'AI_ASSISTANT_FAILED', 'UPSTREAM_UNAVAILABLE', 'UPSTREAM_REJECTED',
@@ -29,10 +29,12 @@ final class AssistantSseTiming
];
private string $traceId;
private int $startedAt;
public function __construct()
{
$this->traceId = bin2hex(random_bytes(16));
$this->startedAt = hrtime(true);
}
public function id(): string
@@ -57,6 +59,8 @@ final class AssistantSseTiming
$event = [
'trace_id' => $this->traceId,
'phase' => in_array($phase, self::PHASES, true) ? $phase : 'upstream_done',
// Includes authorization, aggregation and compaction before SSE starts.
'request_elapsed_ms' => self::elapsedMs($this->startedAt),
];
foreach (self::NUMBER_FIELDS as $field) {
if (isset($metrics[$field]) && is_int($metrics[$field])) {
@@ -81,7 +85,8 @@ final class AssistantSseTiming
public function log(string $phase, array $metrics = []): void
{
try {
Log::info('diagnosis ai assistant timing ' . json_encode($this->event($phase, $metrics)));
// This endpoint exits after SSE and bypasses normal HTTP-end log flushing.
Log::write('diagnosis ai assistant timing ' . json_encode($this->event($phase, $metrics)), 'info');
} catch (\Throwable $ignored) {
// Telemetry must never interrupt the clinical request.
}