cyberdelia / metrology

A library to easily measure what's going on in your python.
https://metrology.readthedocs.org
MIT License
309 stars 25 forks source link

creating Gauge causes TypeError #11

Closed toxsick closed 11 years ago

toxsick commented 11 years ago

Hello cyberdelia,

i'm having problems getting a gauge to work with librato reporter. Here's my dummy Code:


import time
from metrology import Metrology
from metrology import instruments
from metrology.reporter.librato import LibratoReporter

class JobGauge(instruments.Gauge):
    def value(self):
        return 10

if __name__ == "__main__":
    gauge = Metrology.gauge('pending-jobs', JobGauge())

    # Start Librato Reporter
    reporter = LibratoReporter("foo", "bar")
    reporter.start()

    while True:
        time.sleep(1)

I get the following exception:


Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/metrology/utils/periodic.py", line 24, in run
    self.task()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/metrology/reporter/base.py", line 11, in task
    self.write()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/metrology/reporter/librato.py", line 88, in write
    data=dumps(metrics),
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <bound method JobGauge.value of <__main__.JobGauge object at 0x101b8da90>> is not JSON serializable

seems to me that value() is not called and a method object is sent instead. Did I get the example wrong?

regards hanso

cyberdelia commented 11 years ago

value should be a property :

class JobGauge(instruments.Gauge):
    @property
    def value(self):
        return 10
toxsick commented 11 years ago

i thought i tried that, but seems that i haven't. Thx for the fast help.

regards hanso