Skip to main content
Version: Next

Statistics <StateExtension, PersistedStateExtension>

The statistics class provides an interface to collecting and logging run statistics for requests.

All statistic information is saved on key value store under the key CRAWLEE_CRAWLER_STATISTICS_*, persists between migrations and abort/resurrect

Custom fields are tracked by passing stateExtension - the extra fields are then part of state, persisted and restored along with the built-in ones.

Implements

Index

Constructors

constructor

  • new Statistics<StateExtension, PersistedStateExtension>(options): Statistics<StateExtension, PersistedStateExtension>
  • Construct a statistics instance to pass to a crawler via its statistics option, e.g. to preconfigure persistence or error snapshots, share it across sequential runs, or track extra fields via state.


    Parameters

    Returns Statistics<StateExtension, PersistedStateExtension>

Properties

readonlyerrorTracker

errorTracker: ErrorTracker

An error tracker for final retry errors.

readonlyerrorTrackerRetry

errorTrackerRetry: ErrorTracker

An error tracker for retry errors prior to the final retry.

readonlyid

id: string

Statistic instance id.

Accessors

requestRetryHistogram

  • get requestRetryHistogram(): number[]
  • Contains the current retries histogram. Index 0 means 0 retries, index 2, 2 retries, and so on


    Returns number[]

state

Methods

calculate

persistState

  • persistState(): Promise<void>
  • Persist internal state to the key value store.

    Statistics are bookkeeping - a store that refuses the write is worth a warning, not a failed crawl. The crawler calls this from its migration handler, where a rejection would go unhandled.


    Returns Promise<void>

registerStatusCode

  • registerStatusCode(code): void
  • Increments the status code counter.


    Parameters

    • code: number

    Returns void

reset

  • reset(): void
  • Set the current statistic instance to pristine values.

    The persisted record is left alone - use Statistics.resetStore to clear that as well.


    Returns void

resetStore

  • resetStore(): Promise<void>
  • Clear the persisted statistics record, leaving the in-memory state alone.

    Throws while capturing - the next PERSIST_STATE event would write the record straight back.


    Returns Promise<void>

startCapturing

  • startCapturing(): Promise<void>
  • Initializes the key value store for persisting the statistics, displaying the current state in predefined intervals


    Returns Promise<void>

stopCapturing

  • stopCapturing(): Promise<void>
  • Stops logging and remove event listeners, then persist


    Returns Promise<void>

toJSON

  • Make this class serializable when called with JSON.stringify(statsInstance) directly or through keyValueStore.setValue('KEY', statsInstance)


    Returns StatisticPersistedState & PersistedStateExtension