trying...

This commit is contained in:
Luke Berndt
2024-08-25 09:44:17 -04:00
parent 6d0335c278
commit d9823a6af9
4 changed files with 281 additions and 207 deletions

View File

@@ -1,6 +1,8 @@
'use strict'; 'use strict';
const opentelemetry = require('@opentelemetry/sdk-node'); const opentelemetry = require('@opentelemetry/sdk-node');
const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');
const { TraceIdRatioBasedSampler } = require('@opentelemetry/sdk-trace-node'); const { TraceIdRatioBasedSampler } = require('@opentelemetry/sdk-trace-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
@@ -24,6 +26,9 @@ const sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter(), traceExporter: new OTLPTraceExporter(),
sampler: new TraceIdRatioBasedSampler(samplePercentage), sampler: new TraceIdRatioBasedSampler(samplePercentage),
instrumentations: [ instrumentations: [
new MongoDBInstrumentation({
// see under for available configuration
}),
getNodeAutoInstrumentations({ getNodeAutoInstrumentations({
// we recommend disabling fs autoinstrumentation since it can be noisy // we recommend disabling fs autoinstrumentation since it can be noisy
// and expensive during startup // and expensive during startup

View File

@@ -5,7 +5,7 @@ var sysStats = require("../sys_stats");
var System = require("../models/system"); var System = require("../models/system");
var Talkgroup = require("../models/talkgroup"); var Talkgroup = require("../models/talkgroup");
var { callModel: Call } = require("../models/call"); var { callModel: Call } = require("../models/call");
const { trace } = opentelemetry; const { trace, context } = opentelemetry;
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3"); const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-providers"); const { fromIni } = require("@aws-sdk/credential-providers");
@@ -37,7 +37,8 @@ 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 tracer = trace.getTracer("upload-service")
const parentSpan = trace.getActiveSpan(); return context.with(context.active(), async () => {
const parentSpan = trace.getActiveSpan(context.active());
await tracer.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());
@@ -56,7 +57,7 @@ exports.upload = async function (req, res, next) {
} }
// Extract and parse request data // Extract and parse request data
const parseSpan = tracer.startSpan('parse_request_data');
var shortName = req.params.shortName.toLowerCase(); var shortName = req.params.shortName.toLowerCase();
var apiKey = req.body.api_key; var apiKey = req.body.api_key;
@@ -89,17 +90,27 @@ exports.upload = async function (req, res, next) {
res.send("Error parsing sourcelist " + err); res.send("Error parsing sourcelist " + err);
return; return;
} }
parseSpan.end();
let item = null; let item = null;
// Validate that the system exists and the API key is correct // Validate that the system exists and the API key is correct
const validateSpan = tracer.startSpan("validate_system"); await tracer.startActiveSpan('validate_system', { parent: trace.getActiveSpan(context.active()) }, async (validateSpan) => {
try { try {
item = await System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]); item = await System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]);
validateSpan.setAttribute('system.exists', !!item); validateSpan.setAttribute('system.exists', !!item);
} catch (err) {
validateSpan.recordException(err);
validateSpan.setStatus({
code: opentelemetry.SpanStatusCode.ERROR,
message: "Error validating system: " + err.message,
});
} finally {
validateSpan.end();
}
});
if (!item) { if (!item) {
validateSpan.setStatus({ validateSpan.setStatus({
@@ -122,20 +133,9 @@ exports.upload = async function (req, res, next) {
res.send("API Keys do not match!\n"); res.send("API Keys do not match!\n");
return; return;
} }
} catch (err) {
validateSpan.recordException(err);
validateSpan.setStatus({
code: opentelemetry.SpanStatusCode.ERROR,
message: "Error validating system: " + err.message,
});
res.status(500).send("Error, invalid shortName:\n" + err);
return;
} finally {
validateSpan.end();
}
// Blocking sensitive talkgroups // Blocking sensitive talkgroups
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,
@@ -144,10 +144,9 @@ exports.upload = async function (req, res, next) {
res.status(200).end(); res.status(200).end();
return; return;
} }
sensitiveSpan.end();
if (item.ignoreUnknownTalkgroup == true) { if (item.ignoreUnknownTalkgroup == true) {
const talkgroupSpan = tracer.startSpan("check_talkgroup_exists");
talkgroupExists = await Talkgroup.exists({ talkgroupExists = await Talkgroup.exists({
'shortName': shortName, 'shortName': shortName,
'num': talkgroupNum 'num': talkgroupNum
@@ -164,14 +163,13 @@ exports.upload = async function (req, res, next) {
res.send("Talkgroup does not exist, skipping.\n"); res.send("Talkgroup does not exist, skipping.\n");
return; return;
} }
talkgroupSpan.end();
} }
res.status(200).end(); res.status(200).end();
// Prepare call object // Prepare call object
let call; let call;
const prepareSpan = tracer.startSpan('prepare_call_object'); //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);
@@ -208,13 +206,13 @@ exports.upload = async function (req, res, next) {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
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();
let fileContent; let fileContent;
// Upload file to S3 // Upload file to S3
const uploadSpan = tracer.startSpan('upload_to_s3'); await tracer.startActiveSpan('upload_to_s3', { parent: trace.getActiveSpan(context.active()) }, async (uploadSpan) => {
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);
@@ -237,18 +235,11 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
uploadSpan.end(); uploadSpan.end();
} }
});
// Save call to database // Save call to database
const saveSpan = tracer.startSpan('save_call'); await tracer.startActiveSpan('save_call', { parent: trace.getActiveSpan(context.active()) }, async (saveSpan) => {
try { try {
await call.save(); await call.save();
sysStats.addCall(call.toObject());
// we only want to notify clients if the clip is longer than 1 second.
if (call.len >= 1) {
req.call = call.toObject();
next();
}
} catch (err) { } catch (err) {
saveSpan.recordException(err); saveSpan.recordException(err);
saveSpan.setStatus({ saveSpan.setStatus({
@@ -259,10 +250,19 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
saveSpan.end(); saveSpan.end();
} }
});
// Update system stats
sysStats.addCall(call.toObject());
// we only want to notify clients if the clip is longer than 1 second.
if (call.len >= 1) {
req.call = call.toObject();
next();
}
// Clean up temporary file // Clean up temporary file
const cleanupSpan = tracer.startSpan('cleanup_temp_file'); await tracer.startActiveSpan('cleanup_temp_file', { parent: trace.getActiveSpan(context.active()) }, async (cleanupSpan) => {
try { try {
fs.unlinkSync(req.file.path); fs.unlinkSync(req.file.path);
cleanupSpan.setAttribute('file.deleted', true); cleanupSpan.setAttribute('file.deleted', true);
@@ -272,6 +272,7 @@ exports.upload = async function (req, res, next) {
} finally { } finally {
cleanupSpan.end(); cleanupSpan.end();
} }
});
} catch (error) { } catch (error) {
@@ -285,4 +286,5 @@ exports.upload = async function (req, res, next) {
span.end(); span.end();
} }
}); });
});
}; };

View File

@@ -15,6 +15,7 @@
"@honeycombio/opentelemetry-node": "^0.7.2", "@honeycombio/opentelemetry-node": "^0.7.2",
"@opentelemetry/api": "^1.7.0", "@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.41.1", "@opentelemetry/auto-instrumentations-node": "^0.41.1",
"@opentelemetry/instrumentation-mongodb": "^0.46.0",
"@opentelemetry/resource-detector-docker": "^0.1.2", "@opentelemetry/resource-detector-docker": "^0.1.2",
"@smithy/node-http-handler": "^3.1.4", "@smithy/node-http-handler": "^3.1.4",
"adm-zip": "^0.5.10", "adm-zip": "^0.5.10",
@@ -1743,6 +1744,22 @@
"@opentelemetry/api": "^1.0.0" "@opentelemetry/api": "^1.0.0"
} }
}, },
"node_modules/@opentelemetry/auto-instrumentations-node/node_modules/@opentelemetry/instrumentation-mongodb": {
"version": "0.39.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation-mongodb/-/instrumentation-mongodb-0.39.0.tgz",
"integrity": "sha512-m9dMj39pcCshzlfCEn2lGrlNo7eV5fb9pGBnPyl/Am9Crh7Or8vOqvByCNd26Dgf5J978zTdLGF+6tM8j1WOew==",
"dependencies": {
"@opentelemetry/instrumentation": "^0.48.0",
"@opentelemetry/sdk-metrics": "^1.9.1",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"engines": {
"node": ">=14"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
}
},
"node_modules/@opentelemetry/auto-instrumentations-node/node_modules/@opentelemetry/otlp-exporter-base": { "node_modules/@opentelemetry/auto-instrumentations-node/node_modules/@opentelemetry/otlp-exporter-base": {
"version": "0.48.0", "version": "0.48.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/otlp-exporter-base/-/otlp-exporter-base-0.48.0.tgz", "resolved": "https://registry.npmjs.org/@opentelemetry/otlp-exporter-base/-/otlp-exporter-base-0.48.0.tgz",
@@ -2508,13 +2525,13 @@
} }
}, },
"node_modules/@opentelemetry/instrumentation-mongodb": { "node_modules/@opentelemetry/instrumentation-mongodb": {
"version": "0.39.0", "version": "0.46.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation-mongodb/-/instrumentation-mongodb-0.39.0.tgz", "resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation-mongodb/-/instrumentation-mongodb-0.46.0.tgz",
"integrity": "sha512-m9dMj39pcCshzlfCEn2lGrlNo7eV5fb9pGBnPyl/Am9Crh7Or8vOqvByCNd26Dgf5J978zTdLGF+6tM8j1WOew==", "integrity": "sha512-VF/MicZ5UOBiXrqBslzwxhN7TVqzu1/LN/QDpkskqM0Zm0aZ4CVRbUygL8d7lrjLn15x5kGIe8VsSphMfPJzlA==",
"dependencies": { "dependencies": {
"@opentelemetry/instrumentation": "^0.48.0", "@opentelemetry/instrumentation": "^0.52.0",
"@opentelemetry/sdk-metrics": "^1.9.1", "@opentelemetry/sdk-metrics": "^1.9.1",
"@opentelemetry/semantic-conventions": "^1.0.0" "@opentelemetry/semantic-conventions": "^1.22.0"
}, },
"engines": { "engines": {
"node": ">=14" "node": ">=14"
@@ -2523,6 +2540,47 @@
"@opentelemetry/api": "^1.3.0" "@opentelemetry/api": "^1.3.0"
} }
}, },
"node_modules/@opentelemetry/instrumentation-mongodb/node_modules/@opentelemetry/api-logs": {
"version": "0.52.1",
"resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.52.1.tgz",
"integrity": "sha512-qnSqB2DQ9TPP96dl8cDubDvrUyWc0/sK81xHTK8eSUspzDM3bsewX903qclQFvVhgStjRWdC5bLb3kQqMkfV5A==",
"dependencies": {
"@opentelemetry/api": "^1.0.0"
},
"engines": {
"node": ">=14"
}
},
"node_modules/@opentelemetry/instrumentation-mongodb/node_modules/@opentelemetry/instrumentation": {
"version": "0.52.1",
"resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation/-/instrumentation-0.52.1.tgz",
"integrity": "sha512-uXJbYU/5/MBHjMp1FqrILLRuiJCs3Ofk0MeRDk8g1S1gD47U8X3JnSwcMO1rtRo1x1a7zKaQHaoYu49p/4eSKw==",
"dependencies": {
"@opentelemetry/api-logs": "0.52.1",
"@types/shimmer": "^1.0.2",
"import-in-the-middle": "^1.8.1",
"require-in-the-middle": "^7.1.1",
"semver": "^7.5.2",
"shimmer": "^1.2.1"
},
"engines": {
"node": ">=14"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
}
},
"node_modules/@opentelemetry/instrumentation-mongodb/node_modules/import-in-the-middle": {
"version": "1.11.0",
"resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.11.0.tgz",
"integrity": "sha512-5DimNQGoe0pLUHbR9qK84iWaWjjbsxiqXnw6Qz64+azRgleqv9k2kTt5fw7QsOpmaGYtuxxursnPPsnTKEx10Q==",
"dependencies": {
"acorn": "^8.8.2",
"acorn-import-attributes": "^1.9.5",
"cjs-module-lexer": "^1.2.2",
"module-details-from-path": "^1.0.3"
}
},
"node_modules/@opentelemetry/instrumentation-mongoose": { "node_modules/@opentelemetry/instrumentation-mongoose": {
"version": "0.35.0", "version": "0.35.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation-mongoose/-/instrumentation-mongoose-0.35.0.tgz", "resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation-mongoose/-/instrumentation-mongoose-0.35.0.tgz",
@@ -3584,6 +3642,14 @@
"acorn": "^8" "acorn": "^8"
} }
}, },
"node_modules/acorn-import-attributes": {
"version": "1.9.5",
"resolved": "https://registry.npmjs.org/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz",
"integrity": "sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==",
"peerDependencies": {
"acorn": "^8"
}
},
"node_modules/adm-zip": { "node_modules/adm-zip": {
"version": "0.5.10", "version": "0.5.10",
"resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.10.tgz", "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.10.tgz",

View File

@@ -9,6 +9,7 @@
"@honeycombio/opentelemetry-node": "^0.7.2", "@honeycombio/opentelemetry-node": "^0.7.2",
"@opentelemetry/api": "^1.7.0", "@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.41.1", "@opentelemetry/auto-instrumentations-node": "^0.41.1",
"@opentelemetry/instrumentation-mongodb": "^0.46.0",
"@opentelemetry/resource-detector-docker": "^0.1.2", "@opentelemetry/resource-detector-docker": "^0.1.2",
"@smithy/node-http-handler": "^3.1.4", "@smithy/node-http-handler": "^3.1.4",
"adm-zip": "^0.5.10", "adm-zip": "^0.5.10",