Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add metrics middleware (#1443)
sarayourfriendfd2c7fba20b4
diff --git a/nuxt.config.ts b/nuxt.config.tsindex 482a467f2..599917f0a 100644--- a/nuxt.config.ts+++ b/nuxt.config.ts@@ -1,16 +1,19 @@import path from 'path'import fs from 'fs'+import promBundle from 'express-prom-bundle'+import pkg from './package.json'import locales from './src/locales/scripts/valid-locales.json'+import { searchTypes } from './src/constants/media'import { VIEWPORTS } from './src/constants/screens'import { isProd } from './src/utils/node-env'import { sentryConfig } from './src/utils/sentry-config'import { env } from './src/utils/env'-import type { NuxtConfig } from '@nuxt/types'+import type { NuxtConfig, ServerMiddleware } from '@nuxt/types'import type { LocaleObject } from '@nuxtjs/i18n'/**@@ -226,6 +229,75 @@ const config: NuxtConfig = {*/redirect: [{ from: '^/photos/(.*)$', to: '/image/$1', statusCode: 301 }],sentry: sentryConfig,+ hooks: {+ render: {+ /**+ * When modifying this function in development with automatic rebuilds enabled+ * it _will_ crash your server with an error about duplicate metric name registration.+ * To debug this function's behavior locally you will have to tolerate stopping and+ * starting `pnpm dev` between each change to nuxt.config.ts. To prevent this from+ * being a _general_ problem the metrics middleware is disabled in development+ * mode, otherwise any change to `nuxt.config.ts` would cause this crash. If you+ * need to debug metrics locally, comment out the early return in development.+ */+ setupMiddleware: (app) => {+ if (process.env.NODE_ENV === 'development') return++ const bypassMetricsPathParts = [+ /**+ * Exclude static paths. Remove once we've moved static file+ * hosting out of the SSR server's responsibilities.+ */+ '_nuxt',+ /**+ * Only include these paths in development. They do not exist in production+ * so we can happily skip the extra iterations these path parts introduce.+ */+ ...(process.env.NODE_ENV === 'development'+ ? ['__webpack', 'sse']+ : []),+ ]+ /**+ * Register this here so that it's registered at the absolute top+ * of the middleware stack. Using server-middleware puts it+ * after a whole host of stuff.+ *+ * Note: The middleware only has access to server side navigations,+ * as it is indeed an express middleware, not a Nuxt page middleware.+ * There's no safe way to pipe client side metrics to Prometheus with+ * this set up and if we wanted that anyway we'll want to invest into+ * an actual RUM solution, not a systems monitoring solution like+ * Prometheus. The implication of this is that the metrics will only+ * include SSR'd requests. SPA navigations or anything else that+ * happens exclusively on the client will not be measured. This is the+ * expected behavior!+ *+ * @see {@link https://github.com/nuxt/nuxt.js/blob/dev/packages/server/src/server.js#L70-L138}+ */+ app.use(+ promBundle({+ bypass: (req) =>+ bypassMetricsPathParts.some((p) => req.originalUrl.includes(p)),+ includeMethod: true,+ includePath: true,+ normalizePath: [+ ...searchTypes.map(+ // Normalize single result pages with IDs in the path+ (t) => [`/${t}/.*`, `/${t}/#id`] as [string, string]+ ),+ ],+ /**+ * promBundle creates an Express middleware function, which is type-incompatible+ * with Nuxt's "connect" middleware functions. I guess they _are_ compatible,+ * in actual code, or maybe just in the particular implementation of the prometheus+ * bundle middleware. In any case, this cast is necessary to appeas TypeScript+ * without an ugly ts-ignore.+ */+ }) as unknown as ServerMiddleware+ )+ },+ },+ },build: {templates: [{diff --git a/package.json b/package.jsonindex 3ffd007f5..81f68a720 100644--- a/package.json+++ b/package.json@@ -79,6 +79,7 @@"clipboard": "^2.0.8","cookie-universal-nuxt": "^2.1.5","core-js": "^3.22.1",+ "express-prom-bundle": "^6.4.1","express-useragent": "^1.0.15","focus-trap": "^6.7.1","focus-trap-vue": "1.1.1",@@ -88,6 +89,7 @@"nuxt": "^2.15.4","pinia": "^2.0.13","postcss-focus-visible": "^6.0.4",+ "prom-client": "^14.0.1","reakit-utils": "^0.15.2","rfdc": "^1.3.0","seeded-rand": "^2.0.1",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 98e98dfc6..fc1dc99ef 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -62,6 +62,7 @@ specifiers:eslint-plugin-unicorn: ^42.0.0eslint-plugin-vue: ^8.6.0eslint-plugin-vuejs-accessibility: ^1.1.1+ express-prom-bundle: ^6.4.1express-useragent: ^1.0.15focus-trap: ^6.7.1focus-trap-vue: 1.1.1@@ -79,6 +80,7 @@ specifiers:postcss: ^8.4.12postcss-focus-visible: ^6.0.4prettier: ^2.2.1+ prom-client: ^14.0.1reakit-utils: ^0.15.2rfdc: ^1.3.0rimraf: ^3.0.2@@ -122,6 +124,7 @@ dependencies:clipboard: 2.0.8cookie-universal-nuxt: 2.1.5core-js: 3.22.5+ express-prom-bundle: [email protected]express-useragent: 1.0.15focus-trap: 6.7.1focus-trap-vue: 1.1.1_ww4o2appb4ncfu23ooisleyiie@@ -131,6 +134,7 @@ dependencies:nuxt: 2.15.8_wl2mvxm2ma5s7sz3dfofdm4k7qpinia: 2.0.13_k3kegkzgmpj55t3l5d75ppieg4postcss-focus-visible: [email protected]+ prom-client: 14.0.1reakit-utils: 0.15.2rfdc: 1.3.0seeded-rand: 2.0.1@@ -7710,6 +7714,10 @@ packages:dependencies:file-uri-to-path: 1.0.0+ /bintrees/1.0.1:+ resolution: {integrity: sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=}+ dev: false+/bluebird/3.7.2:resolution: {integrity: sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==}@@ -10537,6 +10545,17 @@ packages:jest-regex-util: 27.4.0dev: true+ /express-prom-bundle/[email protected]:+ resolution: {integrity: sha512-Sg0svLQe/SS5z1tHDTVfZVjNumobiDlXM0jmemt5Dm9K6BX8z9yCwEr93zbko6fNMR4zKav77iPfxUWi6gAjNA==}+ engines: {node: '>=10'}+ peerDependencies:+ prom-client: '>=12.0.0'+ dependencies:+ on-finished: 2.3.0+ prom-client: 14.0.1+ url-value-parser: 2.1.0+ dev: false+/express-useragent/1.0.15:resolution: {integrity: sha512-eq5xMiYCYwFPoekffMjvEIk+NWdlQY9Y38OsTyl13IvA728vKT+q/CSERYWzcw93HGBJcIqMIsZC5CZGARPVdg==}engines: {node: '>=4.5'}@@ -15989,6 +16008,13 @@ packages:resolution: {integrity: sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==}engines: {node: '>=0.4.0'}+ /prom-client/14.0.1:+ resolution: {integrity: sha512-HxTArb6fkOntQHoRGvv4qd/BkorjliiuO2uSWC2KC17MUTKYttWdDoXX/vxOhQdkoECEM9BBH0pj2l8G8kev6w==}+ engines: {node: '>=10'}+ dependencies:+ tdigest: 0.1.1+ dev: false+/promise-inflight/1.0.1:resolution: {integrity: sha1-mEcocL8igTL8vdhoEputEsPAKeM=}peerDependencies:@@ -18197,6 +18223,12 @@ packages:mkdirp: 1.0.4yallist: 4.0.0+ /tdigest/0.1.1:+ resolution: {integrity: sha1-Ljyyw56kSeVdHmzZEReszKRYgCE=}+ dependencies:+ bintrees: 1.0.1+ dev: false+/telejson/5.3.3:resolution: {integrity: sha512-PjqkJZpzEggA9TBpVtJi1LVptP7tYtXB6rEubwlHap76AMjzvOdKX41CxyaW7ahhzDU1aftXnMCx5kAPDZTQBA==}dependencies:@@ -18895,6 +18927,11 @@ packages:unidecode: 0.1.8dev: false+ /url-value-parser/2.1.0:+ resolution: {integrity: sha512-gIYPWXujdUdwd/9TGCHTf5Vvgw6lOxjE5Q/k+7WNByYyS0vW5WX0k+xuVlhvPq6gRNhzXVv/ezC+OfeAet5Kcw==}+ engines: {node: '>=6.0.0'}+ dev: false+/url/0.11.0:resolution: {integrity: sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=}dependencies: