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

JSONFormatter: Object of type 'Stage' is not JSON serializable #31

Open
displaced opened this issue Sep 5, 2018 · 2 comments
Open

JSONFormatter: Object of type 'Stage' is not JSON serializable #31

displaced opened this issue Sep 5, 2018 · 2 comments

Comments

@displaced
Copy link

Hi,

When using nameko_tracer.formatters.JSONFormatter, logging fails with the following trace:

--- Logging error ---
Traceback (most recent call last):
  File "c:\Users\chris.platts\AppData\Local\Programs\Python\Python36-32\lib\logging\handlers.py", line 633, in emit
    s = self.makePickle(record)
  File "c:\Users\chris.platts\Dropbox\platform-tool-svc\process-service\env\lib\site-packages\graypy\rabbitmq.py", line 74, in makePickle
    return json.dumps(message_dict)
  File "c:\Users\chris.platts\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "c:\Users\chris.platts\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "c:\Users\chris.platts\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "c:\Users\chris.platts\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Stage' is not JSON serializable
Call stack:
  File "c:\Users\chris.platts\Dropbox\platform-tool-svc\process-service\env\lib\site-packages\eventlet\greenthread.py", line 219, in main
    result = function(*args, **kwargs)
  File "c:\Users\chris.platts\Dropbox\platform-tool-svc\process-service\env\lib\site-packages\nameko\containers.py", line 411, in _run_worker
    self._worker_result(worker_ctx, result, exc_info)
  File "c:\Users\chris.platts\Dropbox\platform-tool-svc\process-service\env\lib\site-packages\nameko\containers.py", line 432, in _worker_result
    provider.worker_result(worker_ctx, result, exc_info)
  File "c:\Users\chris.platts\Dropbox\platform-tool-svc\process-service\env\lib\site-packages\nameko_tracer\dependency.py", line 95, in worker_result
    extra=extra)
Message: '[%s] entrypoint result trace'
Arguments: ('process_service.start_job.97879a32-41db-4bed-a6c8-9db1cdf82242',)

Environment is:

  • Windows 10
  • Python v3.6.3
  • nameko_tracer v 1.2.0

A quick fix appears to be to use 'stage': constants.Stage.request.value rather than 'stage': constants.Stage.request

@iky
Copy link
Contributor

iky commented Sep 10, 2018

Hi,

could you please send the config file of your setup? Based on the stack trace it does not look it's failing inside the JSON formatter.

Also the constants.Stage goes to the formatter already as it's string value (line 52 bellow):
https://github.com/Overseas-Student-Living/nameko-tracer/blob/ea1ffb047cc78e1b65dc03bfdfa8157f9da171cd/nameko_tracer/adapters.py#L52-L58

It seem that the quickfix you posted most probably causes all traces to be treated as "request" stage (see line 58)

@iky
Copy link
Contributor

iky commented Sep 10, 2018

Interesting part of your stack-trace is that the python built-in StreamHandler ends up calling makePickle of graypy\rabbitmq.py. Looks like this is the culprit - the unformatted extra is included by the Graypy handler causing the JSON serialisation to fail. GELFRabbitHandler extends SocketHandler which for some reason bypasses any formatter set and tries to pickle the log record instead (probably because the intended use is to unwrap the pickled log record on the other side to be formatted there). Unfortunately GELFRabbitHandler overrides makePickle method replacing the pickle serialisation with JSON one and with hardcoded formatting which than includes extra to the serialised dictionary.

I quick fix may be passing False to extra_fields of your GELFRabbitHandler handler:

LOGGING:
    handlers:
        tracer:
            formatter: tracer
            class: graypy.GELFRabbitHandler
            extra_fields: False

Please let me know if that helps .)

A bit better fix may be making graypy's smarter_repr even smarter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants