Skip to content

Commit 9fa3681

Browse files
blachaWentao-Kuang
andauthored
fix(lambda-tiler): correctly log fetch requests (#3359)
### Motivation Requests tracing has been broken for a while (not entire sure when it broke) It is very very helpful to have statistics on the number of s3 requests required to make a tile ### Modifications - `requestCount` is reserved for the number of lambda invocations so move to `fetchCount` for number of fetches - include the fetchIds to trace hot fetches - increase the percent of requests with tracing enabled - Use async local storage to gain access to the logger for the request that triggers the log message, this fixes a bug where the request may be in `trace` but the fetch is using the base logger which is set to `info` so no logs are generated. ### Verification Ran locally and verified logs are now appearing. <!-- TODO: Say how you tested your changes. --> --------- Co-authored-by: Wentao Kuang <wkuang@linz.govt.nz>
1 parent a61b652 commit 9fa3681

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

packages/lambda-tiler/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FsaCache, FsaLog, LogConfig } from '@basemaps/shared';
1+
import { FsaCache, FsaLog, LogConfig, LogStorage } from '@basemaps/shared';
22
import { LambdaHttpRequest, LambdaHttpResponse, lf } from '@linzjs/lambda';
33

44
import { tileAttributionGet } from './routes/attribution.js';
@@ -34,13 +34,14 @@ function randomTrace(req: LambdaHttpRequest): void {
3434
const rand = Math.random();
3535
// 1% trace
3636
if (rand < 0.01) req.log.level = 'trace';
37-
// 5% debug
38-
else if (rand < 0.04) req.log.level = 'debug';
37+
// 25% debug
38+
else if (rand < 0.25) req.log.level = 'debug';
3939
// everything else info
4040
else req.log.level = 'info';
4141
}
4242

4343
handler.router.hook('request', (req) => {
44+
LogStorage.enterWith({ log: req.log });
4445
FsaLog.reset();
4546

4647
randomTrace(req);
@@ -49,7 +50,8 @@ handler.router.hook('request', (req) => {
4950
});
5051

5152
handler.router.hook('response', (req, res) => {
52-
req.set('requestCount', FsaLog.requests.length);
53+
req.set('fetchCount', FsaLog.count);
54+
req.set('fetches', FsaLog.requests);
5355
req.set('cacheSize', FsaCache.size);
5456
// Force access-control-allow-origin to everything
5557
res.header('access-control-allow-origin', '*');

packages/shared/src/file.system.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ applyS3MiddleWare(s3Fs);
4545
const credentials = new AwsS3CredentialProvider();
4646

4747
credentials.onFileSystemCreated = (acc: AwsCredentialConfig, fs: FileSystem): void => {
48-
LogConfig.get().debug({ prefix: acc.prefix, roleArn: acc.roleArn }, 'FileSystem:Register');
48+
LogConfig.get().info({ prefix: acc.prefix, roleArn: acc.roleArn }, 'FileSystem:Register');
4949
applyS3MiddleWare(fs as FsAwsS3);
5050
fsa.register(acc.prefix, fs);
5151
};
@@ -81,9 +81,10 @@ export const FsaLog = {
8181
this.requests.push(requestId);
8282
const startTime = performance.now();
8383
const res = await next(req);
84-
LogConfig.get().trace(
84+
LogConfig.get().debug(
8585
{
8686
source: req.source.url.href,
87+
sourceHost: req.source.url.hostname,
8788
offset: req.offset,
8889
length: req.length,
8990
requestId,

packages/shared/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { ConfigProviderDynamo } from './dynamo/dynamo.config.js';
88
export { Fsa as fsa, FsaCache, FsaChunk, FsaLog, stringToUrlFolder, urlToString } from './file.system.js';
99
export { Fqdn } from './file.system.middleware.js';
1010
export { getImageryCenterZoom, getPreviewQuery, getPreviewUrl, PreviewSize } from './imagery.url.js';
11-
export { LogConfig, LogType } from './log.js';
11+
export { LogConfig, LogStorage, LogType } from './log.js';
1212
export { LoggerFatalError } from './logger.fatal.error.js';
1313
export { toQueryString } from './url.js';
1414
export * from './util.js';

packages/shared/src/log.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AsyncLocalStorage } from 'node:async_hooks';
2+
13
import pino from 'pino';
24
import { PrettyTransform } from 'pretty-json-log';
35

@@ -29,6 +31,11 @@ const defaultOpts = { level: 'debug' };
2931
export const LogConfig = {
3032
/** Get the currently configured logger */
3133
get(): LogType {
34+
// If this .get() is called inside a async function eg a HTTP request
35+
// use the logger from the async context if it has one
36+
const localStorage = LogStorage.getStore()?.log;
37+
if (localStorage) return localStorage;
38+
3239
if (currentLog == null) {
3340
currentLog = process.stdout.isTTY
3441
? pino.default(defaultOpts, PrettyTransform.stream())
@@ -46,3 +53,5 @@ export const LogConfig = {
4653
LogConfig.get().level = 'silent';
4754
},
4855
};
56+
57+
export const LogStorage = new AsyncLocalStorage<{ log: LogType }>();

0 commit comments

Comments
 (0)