OpenMetrics reporter
-r openmetrics writes the statistics of a run in the OpenMetrics text format: one gauge family per number, with an unlabeled sample for the whole run and one format="..." sample per format. GitLab reads the file as a metrics report and shows in a merge request how each number moved against the target branch; any parser of the Prometheus text format reads it as well. Available since jscpd 5.1.0.
Run it
jscpd . -r openmetrics --output report
OpenMetrics report saved to report/jscpd-metrics.txt
{
"reporters": ["console", "openmetrics"],
"output": "report"
}
The output
Two of the eleven families, from the repository's fixtures/mcp-demo folder:
# HELP jscpd_clones_found Number of duplicated code blocks found.
# TYPE jscpd_clones_found gauge
jscpd_clones_found 3
jscpd_clones_found{format="bash"} 0
jscpd_clones_found{format="javascript"} 3
jscpd_clones_found{format="markdown"} 0
jscpd_clones_found{format="python"} 0
# HELP jscpd_duplicated_lines_percent Percentage of duplicated lines.
# TYPE jscpd_duplicated_lines_percent gauge
# UNIT jscpd_duplicated_lines_percent percent
jscpd_duplicated_lines_percent 17.708333333333336
jscpd_duplicated_lines_percent{format="bash"} 0
jscpd_duplicated_lines_percent{format="javascript"} 49.275362318840585
jscpd_duplicated_lines_percent{format="markdown"} 0
jscpd_duplicated_lines_percent{format="python"} 0
# EOF
| Metric | Meaning |
|---|---|
jscpd_source_files | Files analyzed |
jscpd_lines_analyzed | Lines analyzed |
jscpd_tokens_analyzed | Tokens analyzed |
jscpd_clones_found | Clones |
jscpd_duplicated_lines | Duplicated lines |
jscpd_duplicated_tokens | Duplicated tokens |
jscpd_new_clones | Clones absent from the baseline; 0 without --baseline |
jscpd_new_duplicated_lines | Duplicated lines in those clones; 0 without --baseline |
jscpd_duplicated_lines_percent | Duplication by lines, unit percent |
jscpd_duplicated_tokens_percent | Duplication by tokens, unit percent |
jscpd_detection_duration_seconds | Time spent on detection, unit seconds; the total only |
Format names are escaped as the OpenMetrics grammar requires, and the file ends with # EOF.
Where it shows up
Declare the file as a metrics report artifact. Metrics reports need GitLab Premium or Ultimate.
jscpd:
stage: test
image: node:22
before_script:
- npm install -g jscpd@5
script:
- jscpd . -r console,openmetrics --output report
artifacts:
when: always
reports:
metrics: report/jscpd-metrics.txt
The merge request widget then lists each metric with its value on the target branch and the change.
With gates
--threshold does not change the file. A baseline fills jscpd_new_clones and jscpd_new_duplicated_lines, which stay at 0 otherwise. --kind narrows the counts to the kinds you name.
Options
| Flag | Effect on the file |
|---|---|
-o, --output | The directory of jscpd-metrics.txt |
--baseline, --baseline-from-ref | Values for the two new gauges |
Related
- CodeClimate reporter for the clones themselves in a merge request.
- History for the duplication trend over git commits.
- CI for the GitLab pipeline.