11import { Response } from 'express'
2+ import { createLogger } from '@/observability/logger'
23import statsd from '@/observability/lib/statsd'
34import { fetchStream } from '@/frame/lib/fetch-utils'
45import { getHmacWithEpoch } from '@/search/lib/helpers/get-cse-copilot-auth'
56import { getCSECopilotSource } from '@/search/lib/helpers/cse-copilot-docs-versions'
67import type { ExtendedRequest } from '@/types'
78import { handleExternalSearchAnalytics } from '@/search/lib/helpers/external-search-analytics'
89
10+ const logger = createLogger ( import . meta. url )
11+
912// Maximum time (ms) to wait for the initial response from the upstream
1013// AI search service. Streaming may take longer once the connection is
1114// established, but the connect + first-byte must complete within this window.
@@ -83,7 +86,7 @@ export const aiSearchProxy = async (req: ExtendedRequest, res: Response) => {
8386
8487 if ( ! response . ok ) {
8588 const errorMessage = `Upstream server responded with status code ${ response . status } `
86- console . error ( errorMessage )
89+ logger . error ( errorMessage , { statusCode : response . status } )
8790 statsd . increment ( 'ai-search.stream_response_error' , 1 , diagnosticTags )
8891 res . status ( response . status ) . json ( {
8992 errors : [ { message : errorMessage } ] ,
@@ -131,7 +134,7 @@ export const aiSearchProxy = async (req: ExtendedRequest, res: Response) => {
131134 statsd . increment ( 'ai-search.success_stream_end' , 1 , diagnosticTags )
132135 res . end ( )
133136 } catch ( streamError ) {
134- console . error ( 'Error streaming from cse-copilot: ' , streamError )
137+ logger . error ( 'Error streaming from cse-copilot' , { error : streamError } )
135138 statsd . increment ( 'ai-search.stream_error' , 1 , diagnosticTags )
136139
137140 if ( ! res . headersSent ) {
@@ -153,11 +156,11 @@ export const aiSearchProxy = async (req: ExtendedRequest, res: Response) => {
153156
154157 if ( isTimeout ) {
155158 statsd . increment ( 'ai-search.timeout' , 1 , diagnosticTags )
156- console . error ( ` AI search request timed out after ${ AI_SEARCH_TIMEOUT_MS } ms` )
159+ logger . error ( ' AI search request timed out' , { timeoutMs : AI_SEARCH_TIMEOUT_MS } )
157160 res . status ( 504 ) . json ( { errors : [ { message : 'Upstream request timed out' } ] } )
158161 } else {
159162 statsd . increment ( 'ai-search.route_error' , 1 , diagnosticTags )
160- console . error ( 'Error posting /answers to cse-copilot: ' , error )
163+ logger . error ( 'Error posting /answers to cse-copilot' , { error } )
161164 res . status ( 500 ) . json ( { errors : [ { message : 'Internal server error' } ] } )
162165 }
163166 } finally {
0 commit comments