Skip to content

Commit

Permalink
fix: instrument the number of open outgoing connections (#155)
Browse files Browse the repository at this point in the history
* fix: instrument the number of open outgoing connections

* fix: add `finally` block to trigger instrumentation on promise settle
  • Loading branch information
wa0x6e authored Aug 14, 2023
1 parent e447595 commit 8d2e215
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export const ipfsGatewaysReturnCount = new client.Counter({
labelNames: ['name']
});

export const countOpenProvidersRequest = new client.Gauge({
name: 'providers_open_connections_count',
help: 'Number of open connections to providers',
labelNames: ['name']
});

export const countOpenGatewaysRequest = new client.Gauge({
name: 'ipfs_gateways_open_connections_count',
help: 'Number of open connections to gateways',
labelNames: ['name']
});

export const providersInstrumentation = (req, res, next) => {
const oldJson = res.json;
res.json = body => {
Expand Down
20 changes: 14 additions & 6 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { timeProvidersUpload, providersUploadSize, countOpenProvidersRequest } from '../metrics';
import * as fleek from './fleek';
import * as infura from './infura';
import * as pinata from './pinata';
import * as web3Storage from './web3storage';
import * as fourEverland from './4everland';
import { timeProvidersUpload, providersUploadSize } from '../metrics';

// List of providers used for pinning images
export const IMAGE_PROVIDERS = ['fleek', 'infura', 'pinata', '4everland'];
Expand All @@ -21,12 +21,20 @@ export default function uploadToProviders(providers: string[], params: any) {
return Promise.any(
providers.map(async name => {
const end = timeProvidersUpload.startTimer({ name });
const result = await providersMap[name].set(params);
end();

const size = (params instanceof Buffer ? params : Buffer.from(JSON.stringify(params))).length;
providersUploadSize.inc({ name }, size);
return result;
try {
countOpenProvidersRequest.inc({ name });

const result = await providersMap[name].set(params);
const size = (params instanceof Buffer ? params : Buffer.from(JSON.stringify(params)))
.length;
providersUploadSize.inc({ name }, size);

return result;
} finally {
end();
countOpenProvidersRequest.dec({ name });
}
})
);
}
22 changes: 17 additions & 5 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { capture } from '@snapshot-labs/snapshot-sentry';
import gateways from './gateways.json';
import { set, get } from './aws';
import { MAX, sha256 } from './utils';
import { ipfsGatewaysReturnCount, timeIpfsGatewaysResponse } from './metrics';
import {
ipfsGatewaysReturnCount,
timeIpfsGatewaysResponse,
countOpenGatewaysRequest
} from './metrics';

const router = express.Router();

Expand All @@ -18,10 +22,18 @@ router.get('/ipfs/*', async (req, res) => {
const result = await Promise.any(
gateways.map(async gateway => {
const end = timeIpfsGatewaysResponse.startTimer({ name: gateway });
const url = `https://${gateway}${req.originalUrl}`;
const response = await fetch(url);
end();
return { gateway, json: await response.json() };

try {
countOpenGatewaysRequest.inc({ name: gateway });

const url = `https://${gateway}${req.originalUrl}`;
const response = await fetch(url);

return { gateway, json: await response.json() };
} finally {
end();
countOpenGatewaysRequest.dec({ name: gateway });
}
})
);
ipfsGatewaysReturnCount.inc({ name: result.gateway });
Expand Down

0 comments on commit 8d2e215

Please sign in to comment.