{"version":3,"file":"index.cjs","names":["Registry","Counter","Gauge","Histogram"],"sources":["../../src/metrics/index.ts"],"sourcesContent":["/**\n * Prometheus metrics for monitoring indexer performance and health\n * Exposes metrics for blocks indexed, queue depth, errors, and processing times\n */\n\nimport {\n  collectDefaultMetrics, Counter, Gauge, Histogram, Registry,\n} from \"prom-client\";\n\n/** Prometheus metrics registry */\nexport class IndexerMetrics {\n  /** Prometheus registry instance */\n  public readonly registry: Registry;\n\n  /** Counter for total blocks indexed */\n  public readonly blocksIndexed: Counter;\n\n  /** Gauge for current block height */\n  public readonly currentHeight: Gauge;\n\n  /** Gauge for latest chain block height */\n  public readonly latestHeight: Gauge;\n\n  /** Gauge for blocks behind chain tip */\n  public readonly blocksBehind: Gauge;\n\n  /** Gauge for block queue depth */\n  public readonly queueDepth: Gauge;\n\n  /** Counter for total errors */\n  public readonly errors: Counter;\n\n  /** Counter for RPC errors */\n  public readonly rpcErrors: Counter;\n\n  /** Counter for block processing errors */\n  public readonly processingErrors: Counter;\n\n  /** Counter for database errors */\n  public readonly databaseErrors: Counter;\n\n  /** Histogram for block processing duration */\n  public readonly blockProcessingDuration: Histogram;\n\n  /** Histogram for RPC call duration */\n  public readonly rpcCallDuration: Histogram;\n\n  /** Histogram for database query duration */\n  public readonly databaseQueryDuration: Histogram;\n\n  /** Gauge for retry count */\n  public readonly retryCount: Gauge;\n\n  /** Gauge set to 1 while the indexer is caught up and waiting for the chain to produce a block */\n  public readonly waitingForBlocks: Gauge;\n\n  /** Counter for total transactions processed */\n  public readonly transactionsProcessed: Counter;\n\n  constructor() {\n    this.registry = new Registry();\n\n    // Collect default Node.js metrics (memory, CPU, etc.)\n    collectDefaultMetrics({\n      register: this.registry,\n    });\n\n    // Block indexing metrics\n    this.blocksIndexed = new Counter({\n      name: \"indexer_blocks_indexed_total\",\n      help: \"Total number of blocks indexed\",\n      registers: [this.registry],\n    });\n\n    this.currentHeight = new Gauge({\n      name: \"indexer_current_height\",\n      help: \"Current block height being processed\",\n      registers: [this.registry],\n    });\n\n    this.latestHeight = new Gauge({\n      name: \"indexer_latest_chain_height\",\n      help: \"Latest block height on the chain\",\n      registers: [this.registry],\n    });\n\n    this.blocksBehind = new Gauge({\n      name: \"indexer_blocks_behind\",\n      help: \"Number of blocks behind chain tip\",\n      registers: [this.registry],\n    });\n\n    this.queueDepth = new Gauge({\n      name: \"indexer_queue_depth\",\n      help: \"Current depth of block processing queue\",\n      registers: [this.registry],\n    });\n\n    // Error metrics\n    this.errors = new Counter({\n      name: \"indexer_errors_total\",\n      help: \"Total number of indexer errors\",\n      labelNames: [\"type\"],\n      registers: [this.registry],\n    });\n\n    this.rpcErrors = new Counter({\n      name: \"indexer_rpc_errors_total\",\n      help: \"Total number of RPC errors\",\n      registers: [this.registry],\n    });\n\n    this.processingErrors = new Counter({\n      name: \"indexer_processing_errors_total\",\n      help: \"Total number of Block Processing errors\",\n      registers: [this.registry],\n    });\n\n    this.databaseErrors = new Counter({\n      name: \"indexer_database_errors_total\",\n      help: \"Total number of database errors\",\n      registers: [this.registry],\n    });\n\n    // Performance metrics\n    this.blockProcessingDuration = new Histogram({\n      name: \"indexer_block_processing_duration_seconds\",\n      help: \"Time spent processing a single block\",\n      buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 2, 5, 10],\n      registers: [this.registry],\n    });\n\n    this.rpcCallDuration = new Histogram({\n      name: \"indexer_rpc_call_duration_seconds\",\n      help: \"Duration of RPC calls\",\n      buckets: [0.01, 0.05, 0.1, 0.5, 1, 2, 5, 10, 100],\n      labelNames: [\"method\"],\n      registers: [this.registry],\n    });\n\n    this.databaseQueryDuration = new Histogram({\n      name: \"indexer_database_query_duration_seconds\",\n      help: \"Duration of database queries\",\n      buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5],\n      labelNames: [\"query_type\"],\n      registers: [this.registry],\n    });\n\n    // Operational metrics\n    this.waitingForBlocks = new Gauge({\n      name: \"indexer_waiting_for_blocks\",\n      help: \"1 while the indexer is at the chain tip waiting for the next block, 0 otherwise\",\n      registers: [this.registry],\n    });\n\n    this.retryCount = new Gauge({\n      name: \"indexer_retry_count\",\n      help: \"Current number of retry attempts\",\n      registers: [this.registry],\n    });\n\n    this.transactionsProcessed = new Counter({\n      name: \"indexer_transactions_processed_total\",\n      help: \"Total number of transactions processed\",\n      registers: [this.registry],\n    });\n  }\n\n  /**\n   * Updates block indexing metrics\n   * @param currentHeight - Current height being processed\n   * @param latestHeight - Latest height on chain\n   * @param queueSize - Current queue depth\n   */\n  updateBlockMetrics(currentHeight: number, latestHeight: number, queueSize: number) {\n    this.blocksIndexed.inc();\n    this.currentHeight.set(currentHeight);\n    this.latestHeight.set(latestHeight);\n    this.blocksBehind.set(latestHeight - currentHeight);\n    this.queueDepth.set(queueSize);\n  }\n\n  /**\n   * Flags whether the indexer is idle at the chain tip\n   * @param waiting - true while waiting for the chain to produce a block\n   */\n  setWaiting(waiting: boolean) {\n    this.waitingForBlocks.set(waiting ? 1 : 0);\n  }\n\n  /**\n   * Records an error occurrence\n   * @param type - Error type (rpc, database, processing, etc.)\n   */\n  recordError(type: string) {\n    this.errors.inc({\n      type,\n    });\n\n    if (type === \"rpc\") {\n      this.rpcErrors.inc();\n    }\n    else if (type === \"database\") {\n      this.databaseErrors.inc();\n    }\n    else if (type === \"block\") {\n      this.processingErrors.inc();\n    }\n  }\n\n  /**\n   * Records block processing duration\n   * @param durationSeconds - Duration in seconds\n   */\n  recordBlockProcessing(durationSeconds: number) {\n    this.blockProcessingDuration.observe(durationSeconds);\n  }\n\n  /**\n   * Times a block processing operation\n   * @returns End timer function to call when operation completes\n   */\n  timeBlockProcessing() {\n    return this.blockProcessingDuration.startTimer();\n  }\n\n  /**\n   * Records RPC call duration\n   * @param method - RPC method name\n   * @param durationSeconds - Duration in seconds\n   */\n  recordRpcCall(method: string, durationSeconds: number) {\n    this.rpcCallDuration.observe({\n      method,\n    }, durationSeconds);\n  }\n\n  /**\n   * Times an RPC call operation\n   * @param method - RPC method name\n   * @returns End timer function to call when operation completes\n   */\n  timeRpcCall(method: string) {\n    return this.rpcCallDuration.startTimer({\n      method,\n    });\n  }\n\n  /**\n   * Records database query duration\n   * @param queryType - Type of query (select, insert, update, delete, etc.)\n   * @param durationSeconds - Duration in seconds\n   */\n  recordDatabaseQuery(queryType: string, durationSeconds: number) {\n    this.databaseQueryDuration.observe({\n      query_type: queryType,\n    }, durationSeconds);\n  }\n\n  /**\n   * Times a database query operation\n   * @param queryType - Type of query (select, insert, update, delete, etc.)\n   * @returns End timer function to call when operation completes\n   */\n  timeDatabaseQuery(queryType: string) {\n    return this.databaseQueryDuration.startTimer({\n      query_type: queryType,\n    });\n  }\n\n  /**\n   * Updates the retry count gauge\n   * @param count - Current retry count\n   */\n  updateRetryCount(count: number) {\n    this.retryCount.set(count);\n  }\n\n  /**\n   * Increments the retry count gauge by 1\n   */\n  incrementRetryCount() {\n    this.retryCount.inc();\n  }\n\n  /**\n   * Resets the retry count gauge to 0\n   */\n  resetRetryCount() {\n    this.retryCount.set(0);\n  }\n\n  /**\n   * Records transactions processed\n   * @param count - Number of transactions to record (defaults to 1)\n   */\n  recordTransactions(count: number = 1) {\n    this.transactionsProcessed.inc(count);\n  }\n\n  /**\n   * Gets metrics in Prometheus format\n   * @returns Prometheus metrics string\n   */\n  async getMetrics(): Promise<string> {\n    return this.registry.metrics();\n  }\n}\n"],"mappings":";;;;;;;;;AAUA,IAAa,iBAAb,MAA4B;;CAE1B,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;;CAGhB,AAAgB;CAEhB,cAAc;AACZ,OAAK,WAAW,IAAIA,sBAAU;AAG9B,yCAAsB,EACpB,UAAU,KAAK,UACh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