Agent: Add telemetry json encoder

Telemetry json encoder is needed to encode OperatingSystems enums when sending back telemetries
This commit is contained in:
vakaris_zilius 2022-06-23 11:44:13 +00:00
parent 77f8be523c
commit 717801e9ad
2 changed files with 12 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import logging
from infection_monkey.control import ControlClient
from infection_monkey.telemetry.i_telem import ITelem
from infection_monkey.telemetry.telem_encoder import TelemetryJSONEncoder
logger = logging.getLogger(__name__)
LOGGED_DATA_LENGTH = 300 # How many characters of telemetry data will be logged
@ -39,7 +40,7 @@ class BaseTelem(ITelem, metaclass=abc.ABCMeta):
@property
def json_encoder(self):
return json.JSONEncoder
return TelemetryJSONEncoder
def _log_telem_sending(self, serialized_data: str, log_data=True):
logger.debug(f"Sending {self.telem_category} telemetry.")

View File

@ -0,0 +1,10 @@
import json
from common import OperatingSystems
class TelemetryJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, OperatingSystems):
return obj.name
return json.JSONEncoder.default(self, obj)