pool_metrics.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from __future__ import absolute_import
  2. import logging
  3. import six
  4. log = logging.getLogger(__name__)
  5. class PoolMetricsMetaclass(type):
  6. __instances__ = dict()
  7. __blacklist = ('PoolMetrics', 'PoolMetricsMetaclass')
  8. def __new__(cls, name, bases, attrs):
  9. if name in cls.__blacklist:
  10. return super(PoolMetricsMetaclass, cls).__new__(cls, name,
  11. bases, attrs)
  12. pool_metrics_key = attrs.get('pool_metrics_key') or \
  13. '{}PoolMetrics'.format(name)
  14. new_class = super(PoolMetricsMetaclass, cls).__new__(cls,
  15. pool_metrics_key,
  16. bases, attrs)
  17. setattr(new_class, 'pool_metrics_key', pool_metrics_key)
  18. if pool_metrics_key not in cls.__instances__:
  19. cls.__instances__[pool_metrics_key] = new_class
  20. return cls.__instances__[pool_metrics_key]
  21. class PoolMetrics(six.with_metaclass(PoolMetricsMetaclass, object)):
  22. pool_metrics_key = None