def fetch_metric_paths()

in otava/graphite.py [0:0]


    def fetch_metric_paths(self, prefix: str, paths: Optional[List[str]] = None) -> List[str]:
        """
        Provided a valid Graphite metric prefix, this method will retrieve all corresponding metric paths
        Reference:
        - https://graphite-api.readthedocs.io/en/latest/api.html
        """
        if paths is None:
            paths = []
        try:
            url = f"{self.__url}metrics/find?query={prefix}"
            data_str = urllib.request.urlopen(url).read()
            data_as_json = json.loads(data_str)
            for result in data_as_json:
                curr_path = result["id"]
                if result["leaf"]:
                    paths.append(curr_path)
                else:
                    paths = self.fetch_metric_paths(f"{curr_path}.*", paths)
            return sorted(paths)
        except IOError as err:
            raise GraphiteError(f"Failed to fetch metric path from Graphite: {str(err)}")