diff --git a/packages/minting-backend/sdk/src/analytics/index.ts b/packages/minting-backend/sdk/src/analytics/index.ts index 4fc249bdee..7742d79a0d 100644 --- a/packages/minting-backend/sdk/src/analytics/index.ts +++ b/packages/minting-backend/sdk/src/analytics/index.ts @@ -41,3 +41,26 @@ export const trackRecordMint = () => { // ignore } }; + +export const trackError = (error: Error) => { + try { + track(moduleName, 'error', { + name: error.name, + message: error.message + }); + } catch { + // ignore + } +}; + +export const trackUncaughtException = (error: Error, origin: string) => { + try { + track(moduleName, 'error', { + name: error.name, + message: error.message, + origin, + }); + } catch { + // ignore + } +}; diff --git a/packages/minting-backend/sdk/src/index.ts b/packages/minting-backend/sdk/src/index.ts index d734c53f99..9b418f6480 100644 --- a/packages/minting-backend/sdk/src/index.ts +++ b/packages/minting-backend/sdk/src/index.ts @@ -2,6 +2,7 @@ import { ImmutableConfiguration, ModuleConfiguration } from '@imtbl/config'; import { BlockchainData } from '@imtbl/blockchain-data'; import { init } from '@imtbl/webhook'; import { setEnvironment, setPublishableApiKey } from '@imtbl/metrics'; +import { trackUncaughtException } from 'analytics'; import { mintingPersistence as mintingPersistencePg } from './persistence/pg/postgres'; import { mintingPersistence as mintingPersistencePrismaSqlite } from './persistence/prismaSqlite/sqlite'; import { @@ -79,3 +80,5 @@ export class MintingBackendModule { }); } } + +process.on('uncaughtExceptionMonitor', trackUncaughtException); diff --git a/packages/minting-backend/sdk/src/minting.ts b/packages/minting-backend/sdk/src/minting.ts index 4768960990..322974888b 100644 --- a/packages/minting-backend/sdk/src/minting.ts +++ b/packages/minting-backend/sdk/src/minting.ts @@ -5,7 +5,9 @@ import { BlockchainData as Types } from '@imtbl/generated-clients'; import { BlockchainData } from '@imtbl/blockchain-data'; import { CreateMintRequest, MintRequest, MintingPersistence } from './persistence/type'; import { Logger } from './logger/type'; -import { trackProcessMint, trackRecordMint, trackSubmitMintingRequests } from './analytics'; +import { + trackError, trackProcessMint, trackRecordMint, trackSubmitMintingRequests +} from './analytics'; // TODO: expose metrics // - submitting status count, conflicting status count @@ -124,6 +126,7 @@ export const submitMintingRequests = async ( return response; } catch (e: any) { logger.error(e); + trackError(e); if ( e.code === 'CONFLICT_ERROR' @@ -144,6 +147,7 @@ export const submitMintingRequests = async ( ); } catch (e2) { logger.error(e2); + trackError(e); } } else { // separate assets into "need to be retied" and "exceeded max number of tries." diff --git a/samples/minting-backend-with-postgres/index.ts b/samples/minting-backend-with-postgres/index.ts index 143c539b51..242eeb46eb 100644 --- a/samples/minting-backend-with-postgres/index.ts +++ b/samples/minting-backend-with-postgres/index.ts @@ -1,7 +1,7 @@ import Fastify, { FastifyReply, FastifyRequest } from 'fastify' import { mintingBackend, config } from '@imtbl/sdk'; import { Pool } from 'pg'; -import { v4 as uuidv4, stringify, parse } from 'uuid'; +import { v4 as uuidv4, parse } from 'uuid'; const fastify = Fastify({ logger: true