instance.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const constants = require('./constants.js');
  3. const utils = require('./utils.js');
  4. /**
  5. * A metric instance representing a counter.
  6. */
  7. class CounterMetric {
  8. constructor( _value) {this._value = _value;}
  9. /** @inheritDoc */
  10. get weight() {
  11. return 1;
  12. }
  13. /** @inheritdoc */
  14. add(value) {
  15. this._value += value;
  16. }
  17. /** @inheritdoc */
  18. toString() {
  19. return `${this._value}`;
  20. }
  21. }
  22. /**
  23. * A metric instance representing a gauge.
  24. */
  25. class GaugeMetric {
  26. constructor(value) {
  27. this._last = value;
  28. this._min = value;
  29. this._max = value;
  30. this._sum = value;
  31. this._count = 1;
  32. }
  33. /** @inheritDoc */
  34. get weight() {
  35. return 5;
  36. }
  37. /** @inheritdoc */
  38. add(value) {
  39. this._last = value;
  40. if (value < this._min) {
  41. this._min = value;
  42. }
  43. if (value > this._max) {
  44. this._max = value;
  45. }
  46. this._sum += value;
  47. this._count++;
  48. }
  49. /** @inheritdoc */
  50. toString() {
  51. return `${this._last}:${this._min}:${this._max}:${this._sum}:${this._count}`;
  52. }
  53. }
  54. /**
  55. * A metric instance representing a distribution.
  56. */
  57. class DistributionMetric {
  58. constructor(first) {
  59. this._value = [first];
  60. }
  61. /** @inheritDoc */
  62. get weight() {
  63. return this._value.length;
  64. }
  65. /** @inheritdoc */
  66. add(value) {
  67. this._value.push(value);
  68. }
  69. /** @inheritdoc */
  70. toString() {
  71. return this._value.join(':');
  72. }
  73. }
  74. /**
  75. * A metric instance representing a set.
  76. */
  77. class SetMetric {
  78. constructor( first) {this.first = first;
  79. this._value = new Set([first]);
  80. }
  81. /** @inheritDoc */
  82. get weight() {
  83. return this._value.size;
  84. }
  85. /** @inheritdoc */
  86. add(value) {
  87. this._value.add(value);
  88. }
  89. /** @inheritdoc */
  90. toString() {
  91. return Array.from(this._value)
  92. .map(val => (typeof val === 'string' ? utils.simpleHash(val) : val))
  93. .join(':');
  94. }
  95. }
  96. const METRIC_MAP = {
  97. [constants.COUNTER_METRIC_TYPE]: CounterMetric,
  98. [constants.GAUGE_METRIC_TYPE]: GaugeMetric,
  99. [constants.DISTRIBUTION_METRIC_TYPE]: DistributionMetric,
  100. [constants.SET_METRIC_TYPE]: SetMetric,
  101. };
  102. exports.CounterMetric = CounterMetric;
  103. exports.DistributionMetric = DistributionMetric;
  104. exports.GaugeMetric = GaugeMetric;
  105. exports.METRIC_MAP = METRIC_MAP;
  106. exports.SetMetric = SetMetric;
  107. //# sourceMappingURL=instance.js.map