Skip to content

Commit 467f82c

Browse files
authored
Migrate src/search console statements to structured logger (#61080)
1 parent 40abe44 commit 467f82c

6 files changed

Lines changed: 38 additions & 21 deletions

File tree

eslint.config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ export default [
161161
},
162162
},
163163

164+
// Client-side files that run in the browser where the server-only logger is unavailable
165+
{
166+
files: [
167+
'src/search/components/hooks/useAISearchAutocomplete.ts',
168+
'src/search/components/hooks/useAISearchLocalStorageCache.ts',
169+
],
170+
rules: {
171+
'custom-rules/use-custom-logger': 'off',
172+
},
173+
},
174+
164175
// Disable custom logger rule for logger implementation itself
165176
{
166177
files: ['src/observability/logger/**/*.{ts,js}'],
@@ -183,7 +194,6 @@ export default [
183194
'src/journeys/**/*.{ts,js}',
184195
'src/metrics/**/*.{ts,js}',
185196
'src/observability/lib/handle-package-not-found.ts',
186-
'src/search/**/*.{ts,js}',
187197
],
188198
rules: {
189199
'custom-rules/use-custom-logger': 'off',

src/search/lib/ai-search-proxy.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Response } from 'express'
2+
import { createLogger } from '@/observability/logger'
23
import statsd from '@/observability/lib/statsd'
34
import { fetchStream } from '@/frame/lib/fetch-utils'
45
import { getHmacWithEpoch } from '@/search/lib/helpers/get-cse-copilot-auth'
56
import { getCSECopilotSource } from '@/search/lib/helpers/cse-copilot-docs-versions'
67
import type { ExtendedRequest } from '@/types'
78
import { 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 {

src/search/lib/helpers/get-client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Client } from '@elastic/elasticsearch'
22
import { safeUrlDisplay } from '@/search/lib/helpers/strings'
3+
import { createLogger } from '@/observability/logger'
4+
5+
const logger = createLogger(import.meta.url)
36

47
const DEFAULT_REQUEST_TIMEOUT = 5000
58

@@ -10,7 +13,7 @@ export function getElasticsearchClient(
1013
): Client {
1114
const node = getElasticsearchURL(overrideURL)
1215
if (verbose) {
13-
console.log('Connecting to Elasticsearch URL:', safeUrlDisplay(node))
16+
logger.info('Connecting to Elasticsearch URL', { url: safeUrlDisplay(node) })
1417
}
1518
const client = new Client({
1619
node,

src/search/middleware/ai-search-local-proxy.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import { Router, Request, Response, NextFunction } from 'express'
44
import { fetchStream } from '@/frame/lib/fetch-utils'
55
import { pipeline, Readable } from 'node:stream'
6+
import { createLogger } from '@/observability/logger'
7+
8+
const logger = createLogger(import.meta.url)
69

710
const router = Router()
811

@@ -84,7 +87,7 @@ router.post('/ai-search/v1', async (req: Request, res: Response, next: NextFunct
8487

8588
pipeline(nodeStream, res, (err) => {
8689
if (err) {
87-
console.error('[ai-search proxy] pipeline error:', err)
90+
logger.error('[ai-search proxy] pipeline error', { error: err })
8891
if (!res.headersSent) res.status(502).end('Bad Gateway')
8992
}
9093
if (reader) {
@@ -93,7 +96,7 @@ router.post('/ai-search/v1', async (req: Request, res: Response, next: NextFunct
9396
}
9497
})
9598
} catch (err) {
96-
console.error('[ai-search proxy] request failed:', err)
99+
logger.error('[ai-search proxy] request failed', { error: err })
97100
next(err)
98101
} finally {
99102
// Ensure reader lock is always released

src/search/middleware/general-search-middleware.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ When a user directly hits our API e.g. /api/search/v1?query=foo, they will hit t
88

99
import { fetchWithRetry } from '@/frame/lib/fetch-utils'
1010
import { Request, Response, NextFunction } from 'express'
11+
import { createLogger } from '@/observability/logger'
1112
import { errors } from '@elastic/elasticsearch'
1213
import statsd, { adaptForTimer } from '@/observability/lib/statsd'
1314

@@ -23,6 +24,8 @@ import type {
2324
SearchValidationErrorEntry,
2425
} from '@/search/types'
2526

27+
const logger = createLogger(import.meta.url)
28+
2629
interface Context<Type extends SearchTypes> {
2730
currentVersion: string
2831
currentLanguage: string
@@ -122,17 +125,12 @@ export default async function contextualizeGeneralSearch(
122125
} catch (error) {
123126
// If the Elasticsearch sends a 4XX we want the user to see a 500
124127
if (error instanceof errors.ResponseError) {
125-
console.error(
126-
'Error calling getSearchResults(%s):',
127-
JSON.stringify({
128-
indexName,
129-
searchParams,
130-
}),
128+
logger.error('Error calling getSearchResults', {
129+
indexName,
130+
searchParams,
131131
error,
132-
)
133-
if (error?.meta?.body) {
134-
console.error(`Meta:`, error.meta.body)
135-
}
132+
meta: error?.meta?.body,
133+
})
136134
throw new Error(error.message)
137135
} else {
138136
throw error
@@ -175,7 +173,7 @@ async function getProxySearch(
175173
}
176174
// Add client_name for external API requests
177175
url.searchParams.set('client_name', 'docs.github.com-client')
178-
console.log(`Proxying search to ${url}`)
176+
logger.info('Proxying search', { url: url.toString() })
179177

180178
const response = await fetchWithRetry(url.toString())
181179
if (!response.ok) {

src/search/middleware/search-routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { aiSearchAutocompleteRoute } from '@/search/lib/routes/ai-search-autocom
1212
import { combinedSearchRoute } from '@/search/lib/routes/combined-search-route'
1313
import { createLogger } from '@/observability/logger'
1414

15-
const logger = createLogger('search:middleware:search-routes')
15+
const logger = createLogger(import.meta.url)
1616
const router = express.Router()
1717

1818
router.get('/legacy', (req: Request, res: Response) => {
@@ -35,7 +35,7 @@ export async function handleGetSearchResultsError(
3535
) {
3636
const errorMessage = error instanceof Error ? error.message : String(error)
3737
if (process.env.NODE_ENV === 'development') {
38-
console.error(`Error calling getSearchResults(${options})`, error)
38+
logger.error('Error calling getSearchResults', { options, error })
3939
} else {
4040
const extra: Record<string, unknown> =
4141
options && typeof options === 'object' && !Array.isArray(options)

0 commit comments

Comments
 (0)