Add route identifiers for easy metric reporting (#173)

Pre-requisite for https://github.com/matrix-org/matrix-public-archive/issues/162 and https://github.com/matrix-org/matrix-public-archive/issues/148
This commit is contained in:
Eric Eastwood 2023-04-19 15:09:51 -05:00 committed by GitHub
parent 27afaea8ca
commit 78ee88e094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 7 deletions

View File

@ -0,0 +1,11 @@
'use strict';
// via https://gitlab.com/gitterHQ/env/-/blob/2a184afea5dc5f4db478d651b3597001474ee932/lib/middlewares/identify-route.js
function identifyRouteMiddleware(routeName) {
return function identifyRoute(req, res, next) {
req.routeIdentifier = routeName;
next();
};
}
module.exports = identifyRouteMiddleware;

View File

@ -7,10 +7,11 @@ const asyncHandler = require('../lib/express-async-handler');
const { handleTracingMiddleware } = require('../tracing/tracing-middleware');
const getVersionTags = require('../lib/get-version-tags');
const preventClickjackingMiddleware = require('./prevent-clickjacking-middleware');
const contentSecurityPolicyMiddleware = require('./content-security-policy-middleware');
const preventClickjackingMiddleware = require('../middleware/prevent-clickjacking-middleware');
const contentSecurityPolicyMiddleware = require('../middleware/content-security-policy-middleware');
const identifyRoute = require('../middleware/identify-route-middleware');
const clientSideRoomAliasHashRedirectRoute = require('./client-side-room-alias-hash-redirect-route');
const redirectToCorrectArchiveUrlIfBadSigil = require('./redirect-to-correct-archive-url-if-bad-sigil-middleware');
const redirectToCorrectArchiveUrlIfBadSigil = require('../middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware');
function installRoutes(app) {
app.use(handleTracingMiddleware);
@ -21,6 +22,7 @@ function installRoutes(app) {
let healthCheckResponse;
app.get(
'/health-check',
identifyRoute('health-check'),
asyncHandler(async function (req, res) {
if (!healthCheckResponse) {
const versionTags = await getVersionTags();
@ -71,11 +73,19 @@ function installRoutes(app) {
// Since everything after the hash (`#`) won't make it to the server, let's serve a 404
// page that will potentially redirect them to the correct place if they tried
// `/r/#room-alias:server/date/2022/10/27` -> `/r/room-alias:server/date/2022/10/27`
app.use('/:entityDescriptor(r|roomid)', clientSideRoomAliasHashRedirectRoute);
app.get(
'/:entityDescriptor(r|roomid)',
identifyRoute('client-side-room-alias-hash-redirect'),
clientSideRoomAliasHashRedirectRoute
);
// Correct any honest mistakes: If someone accidentally put the sigil in the URL, then
// redirect them to the correct URL without the sigil to the correct path above.
app.use('/:roomIdOrAliasDirty', redirectToCorrectArchiveUrlIfBadSigil);
app.get(
'/:roomIdOrAliasDirty',
identifyRoute('redirect-to-correct-archive-url-if-bad-sigil'),
redirectToCorrectArchiveUrlIfBadSigil
);
}
module.exports = installRoutes;

View File

@ -6,6 +6,7 @@ const urlJoin = require('url-join');
const express = require('express');
const asyncHandler = require('../lib/express-async-handler');
const identifyRoute = require('../middleware/identify-route-middleware');
const fetchPublicRooms = require('../lib/matrix-utils/fetch-public-rooms');
const renderHydrogenVmRenderScriptToPageHtml = require('../hydrogen-render/render-hydrogen-vm-render-script-to-page-html');
const setHeadersToPreloadAssets = require('../lib/set-headers-to-preload-assets');
@ -29,6 +30,7 @@ const router = express.Router({
router.get(
'/',
identifyRoute('app-room-directory-index'),
asyncHandler(async function (req, res) {
const paginationToken = req.query.page;
const searchTerm = req.query.search;

View File

@ -7,8 +7,9 @@ const express = require('express');
const asyncHandler = require('../lib/express-async-handler');
const StatusError = require('../lib/status-error');
const timeoutMiddleware = require('./timeout-middleware');
const redirectToCorrectArchiveUrlIfBadSigil = require('./redirect-to-correct-archive-url-if-bad-sigil-middleware');
const timeoutMiddleware = require('../middleware/timeout-middleware');
const redirectToCorrectArchiveUrlIfBadSigil = require('../middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware');
const identifyRoute = require('../middleware/identify-route-middleware');
const { HTTPResponseError } = require('../lib/fetch-endpoint');
const parseViaServersFromUserInput = require('../lib/parse-via-servers-from-user-input');
@ -167,6 +168,7 @@ router.use(redirectToCorrectArchiveUrlIfBadSigil);
router.get(
'/',
identifyRoute('app-archive-room-index'),
asyncHandler(async function (req, res) {
const roomIdOrAlias = getRoomIdOrAliasFromReq(req);
@ -208,6 +210,7 @@ router.get(
router.get(
'/event/:eventId',
identifyRoute('app-archive-room-event'),
asyncHandler(async function (req, res) {
// TODO: Fetch event to get `origin_server_ts` and redirect to
// /!roomId/2022/01/01?at=$eventId
@ -217,6 +220,7 @@ router.get(
router.get(
'/jump',
identifyRoute('app-archive-room-jump'),
// eslint-disable-next-line max-statements, complexity
asyncHandler(async function (req, res) {
const roomIdOrAlias = getRoomIdOrAliasFromReq(req);
@ -726,6 +730,7 @@ router.get(
// `path-to-regex` bug where the `?` wasn't attaching to the capture group, see
// https://github.com/pillarjs/path-to-regexp/issues/287
'/date/:yyyy(\\d{4})/:mm(\\d{2})/:dd(\\d{2}):time(T\\d\\d?:\\d\\d?((:\\d\\d?)?))?',
identifyRoute('app-archive-room-date'),
timeoutMiddleware,
// eslint-disable-next-line max-statements, complexity
asyncHandler(async function (req, res) {