mozilla / jira-bugzilla-integration

Jira Bugzilla Integration (JBI) - system to sync bugs and issues
Mozilla Public License 2.0
8 stars 22 forks source link

Field value of `Fields.event.time` and `bug.creation_time` in logs are received as `datetime.datetime(2024, 4, 9, 15, 39, 56)` instead of ISO format #982

Open leplatrem opened 2 months ago

leplatrem commented 2 months ago
Screenshot 2024-04-29 at 13 03 24
alexcottner commented 2 months ago

This appears to be coming from dockerflow's json formatter. I have a unit test going over there that confirms the problem. Will open an issue.

alexcottner commented 2 months ago

Oh, there is already an issue there. https://github.com/mozilla-services/python-dockerflow/issues/10

alexcottner commented 2 months ago

Looking at this a bit more, this is more complicated than I'd like. But I think we have a few options:

  1. In JBI, and everywhere that we want to output dates to logs, we explicitly do dateprop.isoformat() when creating the log record
  2. In dockerflow, we add another JSON log formatter class called "JsonPrettyLogFormatter" that checks for specific object types and applies special formatting. Would require people to move over if they want this.
  3. In dockerflow, we extend the SafeJSONEncoder method with something like the options below. But this could be a breaking change for somebody.
# option 1, explicit but requires more code
class SafeJSONEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return o.isoformat()
        if isinstance(o, datetime.date):
            return o.isoformat()
        if isinstance(o, datetime.time):
            return o.isoformat()
        return repr(o)

# option 2, lazy but would work across multiple object types
class SafeJSONEncoder(json.JSONEncoder):
    def default(self, o):
        if hasattr(o, 'isoformat') and callable(o.isoformat):
            return o.isoformat()
        return repr(o)