diff --git a/packages/lambda-tiler/src/index.ts b/packages/lambda-tiler/src/index.ts index f14ffbd38..050a21dec 100644 --- a/packages/lambda-tiler/src/index.ts +++ b/packages/lambda-tiler/src/index.ts @@ -1,4 +1,4 @@ -import { FsaCache, FsaLog, LogConfig } from '@basemaps/shared'; +import { FsaCache, FsaLog, LogConfig, LogStorage } from '@basemaps/shared'; import { LambdaHttpRequest, LambdaHttpResponse, lf } from '@linzjs/lambda'; import { tileAttributionGet } from './routes/attribution.js'; @@ -34,13 +34,14 @@ function randomTrace(req: LambdaHttpRequest): void { const rand = Math.random(); // 1% trace if (rand < 0.01) req.log.level = 'trace'; - // 5% debug - else if (rand < 0.04) req.log.level = 'debug'; + // 25% debug + else if (rand < 0.25) req.log.level = 'debug'; // everything else info else req.log.level = 'info'; } handler.router.hook('request', (req) => { + LogStorage.enterWith({ log: req.log }); FsaLog.reset(); randomTrace(req); @@ -49,7 +50,8 @@ handler.router.hook('request', (req) => { }); handler.router.hook('response', (req, res) => { - req.set('requestCount', FsaLog.requests.length); + req.set('fetchCount', FsaLog.count); + req.set('fetches', FsaLog.requests); req.set('cacheSize', FsaCache.size); // Force access-control-allow-origin to everything res.header('access-control-allow-origin', '*'); diff --git a/packages/shared/src/file.system.ts b/packages/shared/src/file.system.ts index 63d49fdc8..c48b9a38c 100644 --- a/packages/shared/src/file.system.ts +++ b/packages/shared/src/file.system.ts @@ -45,7 +45,7 @@ applyS3MiddleWare(s3Fs); const credentials = new AwsS3CredentialProvider(); credentials.onFileSystemCreated = (acc: AwsCredentialConfig, fs: FileSystem): void => { - LogConfig.get().debug({ prefix: acc.prefix, roleArn: acc.roleArn }, 'FileSystem:Register'); + LogConfig.get().info({ prefix: acc.prefix, roleArn: acc.roleArn }, 'FileSystem:Register'); applyS3MiddleWare(fs as FsAwsS3); fsa.register(acc.prefix, fs); }; @@ -81,9 +81,10 @@ export const FsaLog = { this.requests.push(requestId); const startTime = performance.now(); const res = await next(req); - LogConfig.get().trace( + LogConfig.get().debug( { source: req.source.url.href, + sourceHost: req.source.url.hostname, offset: req.offset, length: req.length, requestId, diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 68b3c0ce4..8f7bf6eef 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -8,7 +8,7 @@ export { ConfigProviderDynamo } from './dynamo/dynamo.config.js'; export { Fsa as fsa, FsaCache, FsaChunk, FsaLog, stringToUrlFolder, urlToString } from './file.system.js'; export { Fqdn } from './file.system.middleware.js'; export { getImageryCenterZoom, getPreviewQuery, getPreviewUrl, PreviewSize } from './imagery.url.js'; -export { LogConfig, LogType } from './log.js'; +export { LogConfig, LogStorage, LogType } from './log.js'; export { LoggerFatalError } from './logger.fatal.error.js'; export { toQueryString } from './url.js'; export * from './util.js'; diff --git a/packages/shared/src/log.ts b/packages/shared/src/log.ts index 15b70476f..fecdcaae2 100644 --- a/packages/shared/src/log.ts +++ b/packages/shared/src/log.ts @@ -1,3 +1,5 @@ +import { AsyncLocalStorage } from 'node:async_hooks'; + import pino from 'pino'; import { PrettyTransform } from 'pretty-json-log'; @@ -29,6 +31,11 @@ const defaultOpts = { level: 'debug' }; export const LogConfig = { /** Get the currently configured logger */ get(): LogType { + // If this .get() is called inside a async function eg a HTTP request + // use the logger from the async context if it has one + const localStorage = LogStorage.getStore()?.log; + if (localStorage) return localStorage; + if (currentLog == null) { currentLog = process.stdout.isTTY ? pino.default(defaultOpts, PrettyTransform.stream()) @@ -46,3 +53,5 @@ export const LogConfig = { LogConfig.get().level = 'silent'; }, }; + +export const LogStorage = new AsyncLocalStorage<{ log: LogType }>();