Skip to main content
Metrics allow you to collect and aggregate data while running codemods. This is useful for:
  • Codebase analysis: Count occurrences of patterns, prop usages, component instances
  • Migration tracking: Track what needs to be migrated before making changes
  • Read-only codemods: Gather insights without modifying code

Basic Usage

Import useMetricAtom from codemod:metrics to create a metric tracker:

Cardinality

Cardinality dimensions are key-value pairs that let you group and filter metrics. When you call increment(), you pass an object with string keys and values:
This creates a unique metric entry for each combination of cardinality values. You can later group by any dimension (e.g., all entries where component=Button).
Use cardinality to capture context about what you’re counting. For example, when counting prop usages, include both the prop name and the component it belongs to.

Example: Counting Prop Usage

This read-only codemod counts how many times each prop is used on Button components:
Running this codemod produces output like:

Insights

Insights binds each dashboard query to one workflow step and loads that step’s js-ast-grep script from the published package. Metric names must be statically visible in that script (useMetricAtom + increment)—not only in a different package file or helper import. This is the selected step’s script, not necessarily the package entry (for example scripts/codemod.ts). For scale (transforms or mining), see Performant codemods.