Skip to content

Commit

Permalink
chore(refactor): move metrics instrumentation to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Aug 11, 2023
1 parent ccad87f commit 3ea8624
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
};
5 changes: 3 additions & 2 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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') {
Expand Down

0 comments on commit 3ea8624

Please sign in to comment.