Skip to content

Commit

Permalink
fix: fix finally block not executed (#170)
Browse files Browse the repository at this point in the history
* fix: fix finally block not being run

* chore: add status label to `providers_upload_duration_seconds` metrics

* chore: add status to `ipfs_gateways_response_duration_seconds` metrics

* fix: capture formatted provider error as contexts
  • Loading branch information
wa0x6e authored Sep 6, 2023
1 parent f645d2c commit 531ca44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ providersImageCount.set(IMAGE_PROVIDERS.filter(p => providersMap[p].isConfigured
export const timeProvidersUpload = new client.Histogram({
name: 'providers_upload_duration_seconds',
help: "Duration in seconds of provider's upload requests.",
labelNames: ['name', 'type'],
labelNames: ['name', 'type', 'status'],
buckets: [0.5, 1, 2, 5, 10, 15]
});

Expand All @@ -61,7 +61,7 @@ const providersReturnCount = new client.Counter({
export const timeIpfsGatewaysResponse = new client.Histogram({
name: 'ipfs_gateways_response_duration_seconds',
help: "Duration in seconds of each IPFS gateway's reponse.",
labelNames: ['name'],
labelNames: ['name', 'status'],
buckets: [0.5, 1, 2, 5, 10, 15]
});

Expand Down
12 changes: 9 additions & 3 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function uploadToProviders(providers: string[], type: ProviderTyp
configuredProviders.map(async name => {
const type: ProviderType = params instanceof Buffer ? 'image' : 'json';
const end = timeProvidersUpload.startTimer({ name, type });
let status = 0;

try {
countOpenProvidersRequest.inc({ name, type });
Expand All @@ -18,13 +19,18 @@ export default function uploadToProviders(providers: string[], type: ProviderTyp
const size = (params instanceof Buffer ? params : Buffer.from(JSON.stringify(params)))
.length;
providersUploadSize.inc({ name, type }, size);
status = 1;

return result;
} catch (e: any) {
capture(e, { name });
throw e;
if (e instanceof Error) {
capture(e, { name });
} else {
capture('Error from provider', { contexts: { input: { name }, provider_response: e } });
}
return Promise.reject(e);
} finally {
end();
end({ status });
countOpenProvidersRequest.dec({ name, type });
}
})
Expand Down
4 changes: 3 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ router.get('/ipfs/*', async (req, res) => {
const result = await Promise.any(
gateways.map(async gateway => {
const end = timeIpfsGatewaysResponse.startTimer({ name: gateway });
let status = 0;

try {
countOpenGatewaysRequest.inc({ name: gateway });
Expand All @@ -31,10 +32,11 @@ router.get('/ipfs/*', async (req, res) => {
if (!response.ok) {
return Promise.reject(response.status);
}
status = 1;

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

0 comments on commit 531ca44

Please sign in to comment.