-
Notifications
You must be signed in to change notification settings - Fork 177
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
Use class variable for instantiating statsd object to use timer decorator #81
Comments
There are a couple of options. Let's look at why the first couple don't work. statsd = ''
class Foo(object):
def __init__(self):
statsd = StatsClient()
@statsd.timer('foo.bar')
def bar(self):
pass The expression in the decorator ( One other important thing: Using the hostname in the timer name is going to cause some weird behavior. In almost any case, probably the best option is not to create the from .. import config
statsd = StatsClient(config['statsd_host'])
class Foo(object):
@statsd.timer('my_timer_name')
def heavy_method(self):
pass Even better would be to configure statsd via environment variables, including a per-host prefix, and use the default instance: from statsd.defaults.env import statsd
class Foo(object):
@statsd.timer('foo_consumer_IOPerf')
def heavy_method(self):
pass and then set the environment variables before running the program: STATSD_HOST="my.statsd.host" STATSD_PREFIX="$(hostname)" my_program.py If the config that gets passed to the constructor may actually change the statsd host, then the client really needs to be an instance variable ( class Foo(object):
def __init__(self, config):
self.config = config
self.statsd = StatsClient(config['statsd_host'], prefix=socket.gethostname())
def heavy_method(self):
with self.statsd.timer(self.config['timer_metric']):
# heavy stuff goes here or def heavy_method(self, *args, **kwargs):
with self.statsd.timer(self.config['timer_metric']):
self._heavy_wrapped(*args, **kwargs)
def _heavy_wrapped(self, arg1, kwarg1=None):
# heavy stuff goes here Hope that helps! |
* Use FastAPI * PR review * Use uvicorn * Fix typo
I am trying to use the
statsd
timer decoratorMy file structure is something like this
Inside
foo_consumer.py
This class is inherited by another class and then instantiated by it in the parent folder.
Error that I get
AttributeError: 'str' object has no attribute 'timer'
What else I have tried
It is necessary that the
statsd_client
IP be specified over at theconfig.py
fileI thought about passing the
statsd_client
parameter to the decorator like thisNow wouldn't work as the class hasn't been instantiated and
self
is not known at this point.Any alternatives?
The text was updated successfully, but these errors were encountered: