Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WPT queue WST selected region monitoring (DELO-4838) #97

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions instrumentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const opentelemetry = require('@opentelemetry/sdk-node');
const {Resource} = require('@opentelemetry/resources');
const {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION
} = require('@opentelemetry/semantic-conventions');
const {PrometheusExporter} = require('@opentelemetry/exporter-prometheus');
const sdk = new opentelemetry.NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'web-speed-test-server',
[ATTR_SERVICE_VERSION]: require('./package.json').version
}),
metricReader: new PrometheusExporter({
port: 6060,
}),
});
sdk.start();
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
"scripts": {
"test": "mocha",
"start": "node start.js",
"start-with-instrumentation": "node --require ./instrumentation.js start.js",
"postinstall": "patch-package"
},
"dependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-prometheus": "^0.56.0",
"@opentelemetry/resources": "^1.29.0",
"@opentelemetry/sdk-node": "^0.56.0",
"@opentelemetry/semantic-conventions": "^1.28.0",
"async": "^3.2.6",
"async-mutex": "^0.5.0",
"body-parser": "~1.20.3",
Expand Down
8 changes: 7 additions & 1 deletion routes/wpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const locationSelector = require("../wtp/locationSelector");
const logger = require('../logger').logger;
const {LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, LOG_LEVEL_CRITICAL, LOG_LEVEL_DEBUG} = require('../logger');
const path = require('path');
const opentelemetry = require('@opentelemetry/api');

const WstMeter = opentelemetry.metrics.getMeter('default');
const testrunCounter = WstMeter.createCounter('testrun.total');

const routeCallback = (error, result, res, rollBarMsg) => {
if (error) {
Expand Down Expand Up @@ -52,11 +56,11 @@ const wtp = (app) => {
});
});


app.post('/test/run', (req, res) => {
let rollBarMsg = {testId: "N/A", thirdPartyErrorCode: "", file: path.basename((__filename))};
if (!req.body) {
logger.error('Could not run test missing request body', rollBarMsg, req);
testrunCounter.add(1, {"status": "BAD_REQUEST"});
routeCallback({statusCode: 400}, null, res, rollBarMsg);
return;
}
Expand All @@ -65,6 +69,7 @@ const wtp = (app) => {
rollBarMsg.analyzedUrl = testUrl;
if (!testUrl) {
logger.error('Could not run test missing test url',rollBarMsg, req);
testrunCounter.add(1, {"status": "BAD_REQUEST"});
routeCallback({statusCode: 400}, null, res, rollBarMsg);
return;
}
Expand All @@ -75,6 +80,7 @@ const wtp = (app) => {
}*/
logger.info('Started test called from webspeedtest', rollBarMsg, req);
apiCaller.runWtpTest(testUrl, mobile, (error, result, response, rollBarMsg) => {
testrunCounter.add(1, {"status": error ? "FAILURE" : "OK"});
routeCallback(error, result, res, rollBarMsg)
});
});
Expand Down
39 changes: 39 additions & 0 deletions wtp/locationSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@ const config = require('config');
const {Mutex, withTimeout, E_TIMEOUT} = require('async-mutex');
const apiKeys = require('./apiKey');
const path = require("path");
const opentelemetry = require("@opentelemetry/api");
const logger = require('../logger').logger;

const GET_LOCATIONS = 'http://www.webpagetest.org/getLocations.php?f=json';

const WstMeter = opentelemetry.metrics.getMeter('default');
const locationMetrics = {
total: WstMeter.createGauge('location.total'),
lastUpdate: WstMeter.createGauge('location.last_update'),
ratio: WstMeter.createGauge('location.ratio'),
score: WstMeter.createGauge('location.score'),
selected: WstMeter.createGauge('location.selected'),
agent: {
total: WstMeter.createGauge('location.agent.total'),
idle: WstMeter.createGauge('location.agent.idle'),
queued: WstMeter.createGauge('location.agent.queued'),
highprio: WstMeter.createGauge('location.agent.highprio'),
lowprio: WstMeter.createGauge('location.agent.lowprio'),
testing: WstMeter.createGauge('location.agent.testing'),
blocking: WstMeter.createGauge('location.agent.blocking')
},
};

class LocationSelector {
constructor() {
if (!LocationSelector.instance) {
Expand Down Expand Up @@ -111,6 +130,26 @@ class LocationSelector {
this.location = this.getBestLocationId(filtered);
this.cachedAllLocations = filtered;
this.lastUpdated = Date.now();

// telemetry
locationMetrics.total.record(this.cachedAllLocations.length);
locationMetrics.lastUpdate.record(this.lastUpdated);
this.cachedAllLocations.forEach((loc) => {
let labels = {location: loc.location};

locationMetrics.ratio.record(loc.PendingTests.TestAgentRatio, labels);
locationMetrics.score.record(loc.score, labels);

locationMetrics.selected.record(loc.location === this.location ? 1 : 0, labels);

locationMetrics.agent.total.record(loc.PendingTests.Total, labels);
locationMetrics.agent.idle.record(loc.PendingTests.Idle, labels);
locationMetrics.agent.queued.record(loc.PendingTests.Queued, labels);
locationMetrics.agent.highprio.record(loc.PendingTests.HighPriority, labels);
locationMetrics.agent.lowprio.record(loc.PendingTests.LowPriority, labels);
locationMetrics.agent.testing.record(loc.PendingTests.Testing, labels);
locationMetrics.agent.blocking.record(loc.PendingTests.Blocking, labels);
});
};

async getLocation() {
Expand Down
Loading
Loading