Update uploads.js

This commit is contained in:
Luke Berndt
2024-08-21 15:02:41 -04:00
parent fc77bd2934
commit 7352c8af82

View File

@@ -17,6 +17,7 @@ const agent = new https.Agent({
}); });
var s3_endpoint = process.env['S3_ENDPOINT'] != null ? process.env['S3_ENDPOINT'] : 'https://s3.us-west-1.wasabisys.com'; var s3_endpoint = process.env['S3_ENDPOINT'] != null ? process.env['S3_ENDPOINT'] : 'https://s3.us-west-1.wasabisys.com';
var s3_region = process.env['S3_REGION'] != null ? process.env['S3_REGION'] : 'us-west-1'; var s3_region = process.env['S3_REGION'] != null ? process.env['S3_REGION'] : 'us-west-1';
var s3_bucket = process.env['S3_BUCKET'] != null ? process.env['S3_BUCKET'] : 'openmhz-west'; var s3_bucket = process.env['S3_BUCKET'] != null ? process.env['S3_BUCKET'] : 'openmhz-west';
@@ -35,8 +36,9 @@ const client = new S3Client({
}); });
exports.upload = async function (req, res, next) { exports.upload = async function (req, res, next) {
const tracer = trace.getTracer("upload-service")
const parentSpan = trace.getActiveSpan(); const parentSpan = trace.getActiveSpan();
await trace.getTracer('upload-service').startActiveSpan('upload_handler', { parent: parentSpan }, async (span) => { await tracer.startActiveSpan('upload_handler', { parent: parentSpan }, async (span) => {
try { try {
span.setAttribute('call.shortName', req.params.shortName.toLowerCase()); span.setAttribute('call.shortName', req.params.shortName.toLowerCase());
span.setAttribute('call.talkgroup_num', req.body.talkgroup_num); span.setAttribute('call.talkgroup_num', req.body.talkgroup_num);
@@ -95,7 +97,8 @@ exports.upload = async function (req, res, next) {
console.log("Finished parsing request data"); console.log("Finished parsing request data");
// Validate that the system exists and the API key is correct // Validate that the system exists and the API key is correct
await trace.getTracer('upload-service').startActiveSpan('validate_system', { parent: span }, async (validateSpan) => { const validateSpan = tracer.startSpan("validate_system");
try { try {
item = await System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]); item = await System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]);
@@ -134,12 +137,11 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
validateSpan.end(); validateSpan.end();
} }
});
console.log("Finished validating system API key"); console.log("Finished validating system API key");
// Blocking sensitive talkgroups // Blocking sensitive talkgroups
await trace.getTracer('upload-service').startActiveSpan('check_sensitive_talkgroups', { parent: span }, async (sensitiveSpan) => { const sensitiveSpan = tracer.startSpan("check_sensitive_talkgroups");
if ((shortName == "hennearmer") && ((talkgroupNum == 3421) || (talkgroupNum == 3423))) { if ((shortName == "hennearmer") && ((talkgroupNum == 3421) || (talkgroupNum == 3423))) {
sensitiveSpan.setStatus({ sensitiveSpan.setStatus({
code: opentelemetry.SpanStatusCode.ERROR, code: opentelemetry.SpanStatusCode.ERROR,
@@ -149,13 +151,12 @@ exports.upload = async function (req, res, next) {
return; return;
} }
sensitiveSpan.end(); sensitiveSpan.end();
});
console.log("Finished checking sensitive talkgroups"); console.log("Finished checking sensitive talkgroups");
if (item.ignoreUnknownTalkgroup == true) { if (item.ignoreUnknownTalkgroup == true) {
await trace.getTracer('upload-service').startActiveSpan('check_talkgroup_exists', { parent: span }, async (talkgroupSpan) => { const talkgroupSpan = tracer.startSpan("check_talkgroup_exists");
talkgroupExists = await Talkgroup.exists({ talkgroupExists = await Talkgroup.exists({
'shortName': shortName, 'shortName': shortName,
'num': talkgroupNum 'num': talkgroupNum
@@ -177,7 +178,7 @@ exports.upload = async function (req, res, next) {
return; return;
} }
talkgroupSpan.end(); talkgroupSpan.end();
});
} }
console.log("Finished checking talkgroup exists"); console.log("Finished checking talkgroup exists");
@@ -187,7 +188,7 @@ exports.upload = async function (req, res, next) {
// Prepare call object // Prepare call object
let call; let call;
await trace.getTracer('upload-service').startActiveSpan('prepare_call_object', { parent: span }, async (prepareSpan) => { const prepareSpan = tracer.startSpan('prepare_call_object');
var local_path = "/" + shortName + "/" + time.getFullYear() + "/" + (time.getMonth() + 1) + "/" + time.getDate() + "/"; var local_path = "/" + shortName + "/" + time.getFullYear() + "/" + (time.getMonth() + 1) + "/" + time.getDate() + "/";
var object_key = "media/" + shortName + "-" + talkgroupNum + "-" + startTime + path.extname(req.file.originalname); var object_key = "media/" + shortName + "-" + talkgroupNum + "-" + startTime + path.extname(req.file.originalname);
@@ -227,13 +228,12 @@ exports.upload = async function (req, res, next) {
prepareSpan.setAttribute('call.object_key', object_key); prepareSpan.setAttribute('call.object_key', object_key);
prepareSpan.setAttribute('call.url', url); prepareSpan.setAttribute('call.url', url);
prepareSpan.end(); prepareSpan.end();
});
console.log("Finished preparing call object"); console.log("Finished preparing call object");
let fileContent; let fileContent;
// Upload file to S3 // Upload file to S3
await trace.getTracer('upload-service').startActiveSpan('upload_to_s3',{ parent: span }, async (uploadSpan) => { const uploadSpan = tracer.startSpan('upload_to_s3');
try { try {
fileContent = fs.readFileSync(req.file.path); fileContent = fs.readFileSync(req.file.path);
//s3Src = fs.createReadStream(req.file.path); //s3Src = fs.createReadStream(req.file.path);
@@ -244,11 +244,8 @@ exports.upload = async function (req, res, next) {
Body: fileContent, Body: fileContent,
ACL: 'public-read' ACL: 'public-read'
}); });
await client.send(command); await client.send(command);
} catch (err) { } catch (err) {
uploadSpan.recordException(err); uploadSpan.recordException(err);
uploadSpan.setStatus({ uploadSpan.setStatus({
@@ -259,11 +256,12 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
uploadSpan.end(); uploadSpan.end();
} }
});
console.log("Finished uploading to S3"); console.log("Finished uploading to S3");
// Save call to database // Save call to database
await trace.getTracer('upload-service').startActiveSpan('save_call',{ parent: span }, async (saveSpan) => { const saveSpan = tracer.startSpan('save_call');
try { try {
await call.save(); await call.save();
sysStats.addCall(call.toObject()); sysStats.addCall(call.toObject());
@@ -282,11 +280,12 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
saveSpan.end(); saveSpan.end();
} }
});
console.log("Finished saving call to database"); console.log("Finished saving call to database");
// Clean up temporary file // Clean up temporary file
await trace.getTracer('upload-service').startActiveSpan('cleanup_temp_file',{ parent: span }, async (cleanupSpan) => { const cleanupSpan = tracer.startSpan('cleanup_temp_file');
try { try {
fs.unlinkSync(req.file.path); fs.unlinkSync(req.file.path);
cleanupSpan.setAttribute('file.deleted', true); cleanupSpan.setAttribute('file.deleted', true);
@@ -296,7 +295,7 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
cleanupSpan.end(); cleanupSpan.end();
} }
});
} catch (error) { } catch (error) {
span.recordException(error); span.recordException(error);