From 3ea8624f8a2739a45d60924542c9f875a340b5cd Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:04:45 +0900 Subject: [PATCH] chore(refactor): move metrics instrumentation to middleware --- src/metrics.ts | 19 +++++++++++++++++++ src/rpc.ts | 5 +++-- src/upload.ts | 4 +++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/metrics.ts b/src/metrics.ts index e332a83..1b803f8 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -28,6 +28,12 @@ export const providersUploadSize = new client.Counter({ labelNames: ['name'] }); +const providersReturnCount = new client.Counter({ + name: 'providers_return_count', + help: 'Number of times each provider have been used', + labelNames: ['name'] +}); + export const timeIpfsGatewaysResponse = new client.Histogram({ name: 'ipfs_gateways_response_duration_seconds', help: "Duration in seconds of each IPFS gateway's reponse", @@ -40,3 +46,16 @@ export const ipfsGatewaysReturnCount = new client.Counter({ help: 'Number of times each gateway have been used', labelNames: ['name'] }); + +export const providersInstrumentation = (req, res, next) => { + const oldJson = res.json; + res.json = body => { + if (res.statusCode === 200 && body) { + providersReturnCount.inc({ name: body.result?.provider || body.provider }); + } + + res.locals.body = body; + return oldJson.call(res, body); + }; + next(); +}; diff --git a/src/rpc.ts b/src/rpc.ts index fe88ca3..7b1e355 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -4,17 +4,18 @@ import { capture } from '@snapshot-labs/snapshot-sentry'; import { MAX, rpcError, rpcSuccess } from './utils'; import { set as setAws } from './aws'; import { JSON_PROVIDERS, default as set } from './providers/'; +import { providersInstrumentation } from './metrics'; const router = express.Router(); -router.post('/', async (req, res) => { +router.post('/', providersInstrumentation, async (req, res) => { const { id, params } = req.body; try { const size = Buffer.from(JSON.stringify(params)).length; if (size > MAX) return rpcError(res, 400, 'File too large', id); const result = await Promise.any(set(JSON_PROVIDERS, params)); await setAws(result.cid, params); - console.log('Success', result.provider, 'size', size, 'ms', result.ms); + console.log('Success', result.provider, 'size', size); result.size = size; return rpcSuccess(res, result, id); } catch (e: any) { diff --git a/src/upload.ts b/src/upload.ts index 8e6c540..22057ed 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -6,6 +6,7 @@ import sharp from 'sharp'; import { capture } from '@snapshot-labs/snapshot-sentry'; import { rpcError, rpcSuccess } from './utils'; import { IMAGE_PROVIDERS, default as set } from './providers/'; +import { providersInstrumentation } from './metrics'; const MAX_INPUT_SIZE = 1024 * 1024; const MAX_IMAGE_DIMENSION = 1500; @@ -16,7 +17,7 @@ const upload = multer({ limits: { fileSize: MAX_INPUT_SIZE } }).single('file'); -router.post('/upload', async (req, res) => { +router.post('/upload', providersInstrumentation, async (req, res) => { upload(req, res, async err => { try { if (err) return rpcError(res, 400, err.message); @@ -41,6 +42,7 @@ router.post('/upload', async (req, res) => { provider: result.provider }; console.log('Upload success', result.provider, result.cid); + return rpcSuccess(res, file); } catch (e: any) { if (e.message === 'Input buffer contains unsupported image format') {