Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Apr 29, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@leplatrem
Copy link
Contributor

Screenshot 2024-04-29 at 13 03 24
@leplatrem leplatrem added the bug Something isn't working label Apr 29, 2024
@leplatrem leplatrem changed the title Field value of Fields.event.time' in logs is received as datetime.datetime(2024, 4, 9, 15, 39, 56)` instead of ISO format 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 Apr 29, 2024
@alexcottner
Copy link
Contributor

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
Copy link
Contributor

Oh, there is already an issue there. mozilla-services/python-dockerflow#10

@alexcottner
Copy link
Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants