Skip to content

Commit

Permalink
fix(lambda-tiler): correctly log fetch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Oct 16, 2024
1 parent a61b652 commit bb78f69
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/lambda-tiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FsaCache, FsaLog, LogConfig } from '@basemaps/shared';
import { FsaCache, FsaLog, LogConfig, LogStorage } from '@basemaps/shared';

Check failure on line 1 in packages/lambda-tiler/src/index.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Module '"@basemaps/shared"' has no exported member 'LogStorage'.

Check failure on line 1 in packages/lambda-tiler/src/index.ts

View workflow job for this annotation

GitHub Actions / build-containers

Module '"@basemaps/shared"' has no exported member 'LogStorage'.

Check failure on line 1 in packages/lambda-tiler/src/index.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Module '"@basemaps/shared"' has no exported member 'LogStorage'.

Check failure on line 1 in packages/lambda-tiler/src/index.ts

View workflow job for this annotation

GitHub Actions / smoke

Module '"@basemaps/shared"' has no exported member 'LogStorage'.

Check failure on line 1 in packages/lambda-tiler/src/index.ts

View workflow job for this annotation

GitHub Actions / screenshot

Module '"@basemaps/shared"' has no exported member 'LogStorage'.
import { LambdaHttpRequest, LambdaHttpResponse, lf } from '@linzjs/lambda';

import { tileAttributionGet } from './routes/attribution.js';
Expand Down Expand Up @@ -35,12 +35,13 @@ function randomTrace(req: LambdaHttpRequest): void {
// 1% trace
if (rand < 0.01) req.log.level = 'trace';
// 5% debug
else if (rand < 0.04) req.log.level = '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);
Expand All @@ -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', '*');
Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/file.system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AsyncLocalStorage } from 'node:async_hooks';

import pino from 'pino';
import { PrettyTransform } from 'pretty-json-log';

Expand Down Expand Up @@ -29,6 +31,9 @@ const defaultOpts = { level: 'debug' };
export const LogConfig = {
/** Get the currently configured logger */
get(): LogType {
const localStorage = LogStorage.getStore()?.log;
if (localStorage) return localStorage;

if (currentLog == null) {
currentLog = process.stdout.isTTY
? pino.default(defaultOpts, PrettyTransform.stream())
Expand All @@ -46,3 +51,5 @@ export const LogConfig = {
LogConfig.get().level = 'silent';
},
};

export const LogStorage = new AsyncLocalStorage<{ log: LogType }>();

0 comments on commit bb78f69

Please sign in to comment.