Skip to content

Commit

Permalink
Prevent crash if docker stats api call fails
Browse files Browse the repository at this point in the history
Ensure that performance packets sent to the extension are always valid
  • Loading branch information
davidreis97 committed Mar 18, 2020
1 parent 58f05c9 commit cbaa725
Showing 1 changed file with 47 additions and 31 deletions.
78 changes: 47 additions & 31 deletions dockerfile-utils/src/dynamicAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export class DynamicAnalysis {
this.addDiagnostic(DiagnosticSeverity.Error, this.dockerfile.getENTRYPOINTs()[0].getRange(), "Container Exited with code (" + data.StatusCode + ") - See Logs");
this.publishDiagnostics();
}
this.destroy();
});
});
});
Expand Down Expand Up @@ -677,53 +678,68 @@ export class DynamicAnalysis {

//Based on https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175-L188
private calculateCPUPercent(stats) {
let cpuPercent = 0;
let cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage;
let systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage;
let cpuCount = stats.cpu_stats.cpu_usage.percpu_usage.length;
try{
let cpuPercent = 0;
let cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage;
let systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage;
let cpuCount = stats.cpu_stats.cpu_usage.percpu_usage.length;

if (systemDelta && cpuDelta) {
cpuPercent = (cpuDelta / systemDelta) * cpuCount * 100;
}
if (systemDelta && cpuDelta) {
cpuPercent = (cpuDelta / systemDelta) * cpuCount * 100;
}

return cpuPercent;
return !isNaN(cpuPercent) ? cpuPercent : 0;
}catch(e){
return 0;
}
}

private calculateNetworks(stats) {
let rawNetworks = stats.networks;
let finalNetworks = {};

for (let key of Object.keys(rawNetworks)) {
finalNetworks[key] = {
input: rawNetworks[key].rx_bytes,
output: rawNetworks[key].tx_bytes
};
}

return finalNetworks;
try{
for (let key of Object.keys(rawNetworks)) {
finalNetworks[key] = {
input: rawNetworks[key].rx_bytes,
output: rawNetworks[key].tx_bytes
};
}

return finalNetworks;
}catch(e){
return {};
}
}

//Based on https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L106-L125
private calculateStorage(stats) { //! TODO - Test a container which actually uses storage
let readBytes = 0;
let writeBytes = 0;

if (process.platform === "win32") {
readBytes = stats.storage_stats.read_size_bytes || 0;
writeBytes = stats.storage_stats.write_size_bytes || 0;
} else {
for (let entry of stats.blkio_stats.io_service_bytes_recursive) {
if (entry.op == "read") {
readBytes += entry.value;
} else if (entry.op == "write") {
writeBytes += entry.value;
try{
if (process.platform === "win32") {
readBytes = stats.storage_stats.read_size_bytes || 0;
writeBytes = stats.storage_stats.write_size_bytes || 0;
} else {
for (let entry of stats.blkio_stats.io_service_bytes_recursive) {
if (entry.op == "read") {
readBytes += entry.value;
} else if (entry.op == "write") {
writeBytes += entry.value;
}
}
}
}

return {
readBytes: readBytes,
writeBytes: writeBytes
return {
readBytes: !isNaN(readBytes) ? readBytes : 0,
writeBytes: !isNaN(writeBytes) ? writeBytes : 0
}
}catch(e){
return {
readBytes: 0,
writeBytes: 0
}
}
}

Expand Down Expand Up @@ -754,8 +770,8 @@ export class DynamicAnalysis {
percentage: this.calculateCPUPercent(parsedData)
},
memory: {
usage: parsedData.memory_stats.usage,
limit: parsedData.memory_stats.limit
usage: parsedData.memory_stats.usage || 0,
limit: parsedData.memory_stats.limit || 0
},
networks: this.calculateNetworks(parsedData),
storage: this.calculateStorage(parsedData)
Expand Down

0 comments on commit cbaa725

Please sign in to comment.