def _init_cdf(self) -> None: """ Initialize the CDF tenant with the necessary time series and asset. """ time_series: List[TimeSeries] = [] if self.asset is not None: # Ensure that asset exist, and retrieve internal ID try: asset = self.cdf_client.assets.create(self.asset) except CogniteDuplicatedError: asset = self.cdf_client.assets.retrieve( external_id=self.asset.external_id) asset_id = asset.id if asset is not None else None else: asset_id = None for metric in REGISTRY.collect(): if type(metric) == Metric and metric.type in ["gauge", "counter"]: external_id = self.external_id_prefix + metric.name time_series.append( TimeSeries( external_id=external_id, name=metric.name, legacy_name=external_id, description=metric.documentation, asset_id=asset_id, )) ensure_time_series(self.cdf_client, time_series)
def _push_to_server(self) -> None: """ Create datapoints an push them to their respective time series """ timestamp = int(arrow.get().float_timestamp * 1000) datapoints: List[Dict[str, Union[str, List[Tuple[float, float]]]]] = [] for metric in REGISTRY.collect(): if type(metric) == Metric and metric.type in ["gauge", "counter"]: if len(metric.samples) == 0: continue external_id = self.external_id_prefix + metric.name datapoints.append({ "externalId": external_id, "datapoints": [(timestamp, metric.samples[0].value)] }) self.cdf_client.datapoints.insert_multiple(datapoints) self.logger.debug("Pushed metrics to CDF tenant '%s'", self._cdf_project)
def collect() -> Iterable[Metric]: for metric in REGISTRY.collect(): if not metric.name.startswith("__"): yield metric
def collect(): for metric in REGISTRY.collect(): if not metric.name.startswith("__"): yield metric
except KeyError: logging.error("Could not retrieve metrics from: " + self.metrics) logging.error("Check argument sonar_metrics") if __name__ == "__main__": parser = configargparse.ArgumentParser() parser.add_argument('--sonar_url', type=str, required=True, env_var='sonar_url') parser.add_argument('--sonar_metrics', type=str, env_var='sonar_metrics', default='ncloc,coverage') parser.add_argument('--sonar_user', type=str, required=True, env_var='sonar_user') parser.add_argument('--sonar_password', type=str, required=True, env_var='sonar_password') parser.add_argument('--run_once', action='store_true') args = parser.parse_args() logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s') requests.packages.urllib3.disable_warnings(InsecureRequestWarning) REGISTRY.register(SonarCollector(args.sonar_url, args.sonar_user, args.sonar_password, args.sonar_metrics)) if args.run_once: for x in REGISTRY.collect(): logging.info(x) for y in x.samples: logging.info(y) sys.exit("runonce") start_http_server(9118) while True: time.sleep(1)