code: def _collect_metrics(
      repo: "Repo",
      targets: Optional[List[str]] = None,
      stages: Optional[List[str]] = None,
      outs_only: bool = False,
  ) -> List[str]:
      metrics: List[str] = []
  
      if targets:
          # target is a repo-relative path
          metrics.extend(targets)
  
      if not targets or outs_only:
          outs = (
              metrics_from_target(repo, stages)
              if stages
              else repo.index.metrics
          )
          relpath = repo.fs.relpath
          metrics.extend(
              relpath(out.fs_path, repo.root_dir) for out in outs
          )
  
      if not targets and not outs_only and not stages:
          # _collect_top_level_metrics returns repo-relative paths
          metrics.extend(_collect_top_level_metrics(repo))
  
      fs = repo.dvcfs
  
      # convert to posixpath for DVCFileSystem
      paths = (fs.from_os_path(metric) for metric in metrics)
      # make paths absolute for DVCFileSystem
      repo_paths = (f"{fs.root_marker}{path}" for path in paths)
      return ldistinct(expand_paths(fs, repo_paths))
  
imports: import os
  from itertools import chain
  from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional, Tuple, TypedDict, Union
  from funcy import ldistinct
  from scmrepo.exceptions import SCMError
  from dvc.log import logger
  from dvc.scm import NoSCMError
  from dvc.utils import as_posix, expand_paths
  from dvc.utils.collections import ensure_list
  from dvc.utils.serialize import load_path
label_desc: Use image node_id 6 for calling a global function with example usage: _collect_metrics(repo, targets, stages, outs_only) and returns: ldistinct