Skip to content

Commit a592ce4

Browse files
committed
chore: refine debug info
1 parent f8fed50 commit a592ce4

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

source/constructs/lib/ecs-image-handler.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import * as ecs from '@aws-cdk/aws-ecs';
77
import * as ecsPatterns from '@aws-cdk/aws-ecs-patterns';
88
import * as iam from '@aws-cdk/aws-iam';
99
import * as s3 from '@aws-cdk/aws-s3';
10-
import * as ssm from '@aws-cdk/aws-ssm';
1110
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
11+
import * as ssm from '@aws-cdk/aws-ssm';
1212
import { Aws, CfnOutput, Construct, Duration, Stack } from '@aws-cdk/core';
1313

1414
const GB = 1024;
@@ -184,14 +184,9 @@ function getOrCreateVpc(scope: Construct): ec2.IVpc {
184184
}
185185

186186
function getTaskSubnets(scope: Construct, vpc: ec2.IVpc): ec2.ISubnet[] {
187-
const subnetIds: string[] = scope.node.tryGetContext('subnet_ids');
188-
// TODO: use filter subnets from vpc
189-
let subnets: ec2.ISubnet[] = [];
190-
if (subnetIds) {
191-
subnetIds.forEach((subnetId, index) => {
192-
subnets.push(ec2.Subnet.fromSubnetId(scope, 'subnet' + index, subnetId));
193-
});
194-
return subnets;
187+
const subnetIds: string[] = scope.node.tryGetContext('subnet_ids') || [];
188+
if (subnetIds.length) {
189+
return subnetIds.map((subnetId, index) => ec2.Subnet.fromSubnetId(scope, 'subnet' + index, subnetId));
195190
} else {
196191
return vpc.privateSubnets;
197192
}

source/new-image-handler/src/debug.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,39 @@ export interface IDebugInfo {
4343
cpus: number;
4444
loadavg: number[];
4545
};
46-
memoryStats: string;
47-
memoryUsage: NodeJS.MemoryUsage;
48-
cache?: {
46+
memory: {
47+
stats: string;
48+
free: number;
49+
total: number;
50+
usage: NodeJS.MemoryUsage;
51+
};
52+
resource: {
53+
usage: NodeJS.ResourceUsage;
54+
};
55+
lruCache?: {
4956
keys: number;
5057
sizeMB: number;
5158
ttlSec: number;
5259
};
53-
resourceUsage: NodeJS.ResourceUsage;
5460
sharp: ISharpInfo;
5561
}
5662

57-
export default function debug(cache?: LRUCache<string, CacheObject>): IDebugInfo {
63+
export default function debug(lruCache?: LRUCache<string, CacheObject>): IDebugInfo {
5864
const ret: IDebugInfo = {
5965
os: {
6066
arch: os.arch(),
6167
cpus: os.cpus().length,
6268
loadavg: os.loadavg(),
6369
},
64-
memoryStats: `free: ${fmtmb(os.freemem())}, total: ${fmtmb(os.totalmem())}, usage ${Math.round(100 * (os.totalmem() - os.freemem()) / os.totalmem()) / 100} %`,
65-
memoryUsage: process.memoryUsage(),
66-
resourceUsage: process.resourceUsage(),
70+
memory: {
71+
stats: `free: ${formatBytes(os.freemem())}, total: ${formatBytes(os.totalmem())}, usage ${((os.totalmem() - os.freemem()) / os.totalmem() * 100).toFixed(2)} %`,
72+
free: os.freemem(),
73+
total: os.totalmem(),
74+
usage: process.memoryUsage(),
75+
},
76+
resource: {
77+
usage: process.resourceUsage(),
78+
},
6779
sharp: {
6880
cache: sharp.cache(),
6981
simd: sharp.simd(),
@@ -72,17 +84,25 @@ export default function debug(cache?: LRUCache<string, CacheObject>): IDebugInfo
7284
versions: sharp.versions,
7385
},
7486
};
75-
if (cache) {
76-
ret.cache = {
77-
keys: cache.size,
78-
sizeMB: Math.round(cache.calculatedSize / 1048576 * 100) / 100,
79-
ttlSec: Math.round(cache.ttl / 1000),
87+
if (lruCache) {
88+
ret.lruCache = {
89+
keys: lruCache.size,
90+
sizeMB: Math.round(b2mb(lruCache.calculatedSize) * 100) / 100,
91+
ttlSec: Math.round(lruCache.ttl / 1000),
8092
};
8193
}
8294
return ret;
8395
}
8496

85-
function fmtmb(v: number) {
86-
return `${Math.round(v / 1048576 * 100) / 100} MB`;
97+
function b2mb(v: number) {
98+
return v / 1048576;
8799
}
88100

101+
function formatBytes(bytes: number) {
102+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
103+
let i = 0;
104+
for (; bytes >= 1024 && i < units.length - 1; i++) {
105+
bytes /= 1024;
106+
}
107+
return `${bytes.toFixed(2)} ${units[i]}`;
108+
};

source/new-image-handler/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { bufferStore, getProcessor, parseRequest, setMaxGifSizeMB, setMaxGifPage
1515
import * as is from './is';
1616
import { IHttpHeaders, InvalidArgument } from './processor';
1717

18-
const MB = 1024 * 1024;
18+
const MB = 1048576;
1919

2020
const ssm = new SSM({ region: config.region });
2121
const smclient = new SecretsManager({ region: config.region });

0 commit comments

Comments
 (0)