metric_name.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import absolute_import
  2. import copy
  3. class MetricName(object):
  4. """
  5. This class encapsulates a metric's name, logical group and its
  6. related attributes (tags).
  7. group, tags parameters can be used to create unique metric names.
  8. e.g. domainName:type=group,key1=val1,key2=val2
  9. Usage looks something like this:
  10. # set up metrics:
  11. metric_tags = {'client-id': 'producer-1', 'topic': 'topic'}
  12. metric_config = MetricConfig(tags=metric_tags)
  13. # metrics is the global repository of metrics and sensors
  14. metrics = Metrics(metric_config)
  15. sensor = metrics.sensor('message-sizes')
  16. metric_name = metrics.metric_name('message-size-avg',
  17. 'producer-metrics',
  18. 'average message size')
  19. sensor.add(metric_name, Avg())
  20. metric_name = metrics.metric_name('message-size-max',
  21. sensor.add(metric_name, Max())
  22. tags = {'client-id': 'my-client', 'topic': 'my-topic'}
  23. metric_name = metrics.metric_name('message-size-min',
  24. 'producer-metrics',
  25. 'message minimum size', tags)
  26. sensor.add(metric_name, Min())
  27. # as messages are sent we record the sizes
  28. sensor.record(message_size)
  29. """
  30. def __init__(self, name, group, description=None, tags=None):
  31. """
  32. Arguments:
  33. name (str): The name of the metric.
  34. group (str): The logical group name of the metrics to which this
  35. metric belongs.
  36. description (str, optional): A human-readable description to
  37. include in the metric.
  38. tags (dict, optional): Additional key/val attributes of the metric.
  39. """
  40. if not (name and group):
  41. raise Exception('name and group must be non-empty.')
  42. if tags is not None and not isinstance(tags, dict):
  43. raise Exception('tags must be a dict if present.')
  44. self._name = name
  45. self._group = group
  46. self._description = description
  47. self._tags = copy.copy(tags)
  48. self._hash = 0
  49. @property
  50. def name(self):
  51. return self._name
  52. @property
  53. def group(self):
  54. return self._group
  55. @property
  56. def description(self):
  57. return self._description
  58. @property
  59. def tags(self):
  60. return copy.copy(self._tags)
  61. def __hash__(self):
  62. if self._hash != 0:
  63. return self._hash
  64. prime = 31
  65. result = 1
  66. result = prime * result + hash(self.group)
  67. result = prime * result + hash(self.name)
  68. tags_hash = hash(frozenset(self.tags.items())) if self.tags else 0
  69. result = prime * result + tags_hash
  70. self._hash = result
  71. return result
  72. def __eq__(self, other):
  73. if self is other:
  74. return True
  75. if other is None:
  76. return False
  77. return (type(self) == type(other) and
  78. self.group == other.group and
  79. self.name == other.name and
  80. self.tags == other.tags)
  81. def __ne__(self, other):
  82. return not self.__eq__(other)
  83. def __str__(self):
  84. return 'MetricName(name=%s, group=%s, description=%s, tags=%s)' % (
  85. self.name, self.group, self.description, self.tags)