From 3228f0fad32c9301bcb3794fc957712bdea58a2f Mon Sep 17 00:00:00 2001 From: Orsiris de Jong Date: Mon, 29 May 2023 19:33:17 +0200 Subject: [PATCH] Log metric destination --- npbackup/core/runner.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/npbackup/core/runner.py b/npbackup/core/runner.py index bc17652..8017fd8 100644 --- a/npbackup/core/runner.py +++ b/npbackup/core/runner.py @@ -99,21 +99,25 @@ def metric_writer(config_dict: dict, restic_result: bool, result_string: str): ) if errors or not restic_result: logger.error("Restic finished with errors.") - if destination.lower().startswith("file://"): - destination = destination[len("file://") :] - with open(destination, "w") as file_handle: - for metric in metrics: - file_handle.write(metric + "\n") - if destination.lower().startswith("http"): - try: - authentication = ( - config_dict["prometheus"]["http_username"], - config_dict["prometheus"]["http_password"], - ) - except KeyError: - logger.info("No metrics authentication present.") - authentication = None - upload_metrics(destination, authentication, no_cert_verify, metrics) + if destination: + logger.debug("Uploading metrics to {}".format(destination)) + if destination.lower().startswith("http"): + try: + authentication = ( + config_dict["prometheus"]["http_username"], + config_dict["prometheus"]["http_password"], + ) + except KeyError: + logger.info("No metrics authentication present.") + authentication = None + upload_metrics(destination, authentication, no_cert_verify, metrics) + else: + try: + with open(destination, "w") as file_handle: + for metric in metrics: + file_handle.write(metric + "\n") + except OSError as exc: + logger.error("Cannot write metrics file {}: {}".format(destination, exc)) except KeyError as exc: logger.info("Metrics not configured: {}".format(exc)) except OSError as exc: