Skip to content

Commit aea6d32

Browse files
authored
Migrate src/observability console statements to structured logger (#61078)
1 parent 6c7d607 commit aea6d32

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

eslint.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export default [
185185
'src/languages/**/*.{ts,js}',
186186
'src/links/**/*.{ts,js}',
187187
'src/metrics/**/*.{ts,js}',
188-
'src/observability/**/*.{ts,js}',
188+
'src/observability/lib/handle-package-not-found.ts',
189189
'src/rest/**/*.{ts,js}',
190190
'src/search/**/*.{ts,js}',
191191
'src/shielding/**/*.{ts,js}',

src/observability/lib/tracing.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
2626
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
2727
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici'
2828
import { NodeSDK } from '@opentelemetry/sdk-node'
29+
import { createLogger } from '@/observability/logger'
30+
import { toError } from '@/observability/lib/to-error'
31+
32+
const logger = createLogger(import.meta.url)
2933

3034
if (process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
3135
const sdk = new NodeSDK({
@@ -44,7 +48,9 @@ if (process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
4448
try {
4549
sdk.start()
4650
} catch (error) {
47-
console.error('[tracing] failed to start:', error instanceof Error ? error.message : error)
51+
logger.error('[tracing] failed to start', {
52+
error: toError(error),
53+
})
4854
}
4955

5056
// Gracefully shut down the SDK on process exit.
@@ -54,7 +60,7 @@ if (process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
5460
await sdk.shutdown()
5561
} catch (error) {
5662
if (error instanceof Error) {
57-
console.error(`[tracing] shutdown error: ${error.message}`)
63+
logger.error('[tracing] shutdown error', { error })
5864
}
5965
}
6066
})

src/observability/middleware/handle-errors.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { minimumNotFoundHtml } from '@/frame/lib/constants'
66
import { setFastlySurrogateKey, SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key'
77
import { errorCacheControl } from '@/frame/middleware/cache-control'
88
import statsd from '@/observability/lib/statsd'
9+
import { toError } from '@/observability/lib/to-error'
910
import { ExtendedRequest } from '@/types'
1011
import { createLogger } from '@/observability/logger'
1112

@@ -86,7 +87,7 @@ async function handleError(
8687
setFastlySurrogateKey(res, SURROGATE_ENUMS.DEFAULT)
8788
}
8889
} else if (DEBUG_MIDDLEWARE_TESTS) {
89-
console.warn('An error occurred in some middleware handler', error)
90+
logger.warn('An error occurred in some middleware handler', { error })
9091
}
9192

9293
try {
@@ -147,7 +148,9 @@ async function handleError(
147148
await logException(error, req)
148149
}
149150
} catch (handlingError) {
150-
console.error('An error occurred in the error handling middleware!', handlingError)
151+
logger.error('An error occurred in the error handling middleware', {
152+
error: toError(handlingError),
153+
})
151154
next(handlingError)
152155
return
153156
}

0 commit comments

Comments
 (0)