From 90949eae6976640240ee830d3a96d485ff1e1ce2 Mon Sep 17 00:00:00 2001 From: CamDavidsonPilon Date: Tue, 4 Jul 2023 15:33:15 -0400 Subject: [PATCH] fix logging error --- pioreactor/logging.py | 2 +- pioreactor/pubsub.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pioreactor/logging.py b/pioreactor/logging.py index 4a7bd612..7b3366de 100644 --- a/pioreactor/logging.py +++ b/pioreactor/logging.py @@ -115,7 +115,7 @@ def emit(self, record) -> None: if (record.levelno == logging.ERROR) and config.getboolean( "data_sharing_with_pioreactor", "send_errors_to_Pioreactor", fallback=False ): - publish_to_pioreactor_cloud("reported_errors", payload) + publish_to_pioreactor_cloud("reported_errors", data_str=payload) # if Python exits too quickly, the last msg might never make it to the broker. mqtt_msg.wait_for_publish(timeout=1) diff --git a/pioreactor/pubsub.py b/pioreactor/pubsub.py index b5138fa5..fc09acc2 100644 --- a/pioreactor/pubsub.py +++ b/pioreactor/pubsub.py @@ -382,30 +382,38 @@ def __exit__(self, *args): self.client.disconnect() -def publish_to_pioreactor_cloud(endpoint: str, json=None) -> None: +def publish_to_pioreactor_cloud( + endpoint: str, data_dict: Optional[dict] = None, data_str: Optional[str] = None +) -> None: """ Parameters ------------ endpoint: the function to send to the data to - json: (optional) json data to send in the body. + json: (optional) data to send in the body. """ from pioreactor.mureq import post from pioreactor.whoami import get_hashed_serial_number, is_testing_env from pioreactor.utils.timing import current_utc_timestamp + from json import dumps + + assert data_dict is not None and data_str is not None if is_testing_env(): return - if json is not None: - json["hashed_serial_number"] = get_hashed_serial_number() - json["timestamp"] = current_utc_timestamp() + if data_dict is not None: + data_dict["hashed_serial_number"] = get_hashed_serial_number() + data_dict["timestamp"] = current_utc_timestamp() + body = dumps(data_dict).encode("utf-8") + elif data_str is not None: + body = data_str.encode("utf-8") headers = {"Content-type": "application/json", "Accept": "text/plain"} try: post( f"https://cloud.pioreactor.com/{endpoint}", - body=json.encode("utf-8"), + body=body, headers=headers, ) except Exception: