@grest-ts/metrics 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/dist/src/GGMetric.d.ts +60 -0
  4. package/dist/src/GGMetric.d.ts.map +1 -0
  5. package/dist/src/GGMetric.js +64 -0
  6. package/dist/src/GGMetric.js.map +1 -0
  7. package/dist/src/GGMetricKey.d.ts +13 -0
  8. package/dist/src/GGMetricKey.d.ts.map +1 -0
  9. package/dist/src/GGMetricKey.js +29 -0
  10. package/dist/src/GGMetricKey.js.map +1 -0
  11. package/dist/src/GGMetrics.d.ts +10 -0
  12. package/dist/src/GGMetrics.d.ts.map +1 -0
  13. package/dist/src/GGMetrics.js +29 -0
  14. package/dist/src/GGMetrics.js.map +1 -0
  15. package/dist/src/GGMetricsDefineStorage.d.ts +4 -0
  16. package/dist/src/GGMetricsDefineStorage.d.ts.map +1 -0
  17. package/dist/src/GGMetricsDefineStorage.js +7 -0
  18. package/dist/src/GGMetricsDefineStorage.js.map +1 -0
  19. package/dist/src/GGMetricsLoader.d.ts +8 -0
  20. package/dist/src/GGMetricsLoader.d.ts.map +1 -0
  21. package/dist/src/GGMetricsLoader.js +17 -0
  22. package/dist/src/GGMetricsLoader.js.map +1 -0
  23. package/dist/src/GGMetricsStore.d.ts +9 -0
  24. package/dist/src/GGMetricsStore.d.ts.map +1 -0
  25. package/dist/src/GGMetricsStore.js +20 -0
  26. package/dist/src/GGMetricsStore.js.map +1 -0
  27. package/dist/src/exporters/GGJsonMetricsExporter.d.ts +41 -0
  28. package/dist/src/exporters/GGJsonMetricsExporter.d.ts.map +1 -0
  29. package/dist/src/exporters/GGJsonMetricsExporter.js +129 -0
  30. package/dist/src/exporters/GGJsonMetricsExporter.js.map +1 -0
  31. package/dist/src/exporters/GGMetricsExporter.d.ts +41 -0
  32. package/dist/src/exporters/GGMetricsExporter.d.ts.map +1 -0
  33. package/dist/src/exporters/GGMetricsExporter.js +70 -0
  34. package/dist/src/exporters/GGMetricsExporter.js.map +1 -0
  35. package/dist/src/exporters/GGNestedMetricsExporter.d.ts +33 -0
  36. package/dist/src/exporters/GGNestedMetricsExporter.d.ts.map +1 -0
  37. package/dist/src/exporters/GGNestedMetricsExporter.js +275 -0
  38. package/dist/src/exporters/GGNestedMetricsExporter.js.map +1 -0
  39. package/dist/src/index-browser.d.ts +17 -0
  40. package/dist/src/index-browser.d.ts.map +1 -0
  41. package/dist/src/index-browser.js +17 -0
  42. package/dist/src/index-browser.js.map +1 -0
  43. package/dist/src/index-node.d.ts +17 -0
  44. package/dist/src/index-node.d.ts.map +1 -0
  45. package/dist/src/index-node.js +20 -0
  46. package/dist/src/index-node.js.map +1 -0
  47. package/dist/src/keys/GGCounterKey.d.ts +11 -0
  48. package/dist/src/keys/GGCounterKey.d.ts.map +1 -0
  49. package/dist/src/keys/GGCounterKey.js +21 -0
  50. package/dist/src/keys/GGCounterKey.js.map +1 -0
  51. package/dist/src/keys/GGGaugeKey.d.ts +13 -0
  52. package/dist/src/keys/GGGaugeKey.d.ts.map +1 -0
  53. package/dist/src/keys/GGGaugeKey.js +27 -0
  54. package/dist/src/keys/GGGaugeKey.js.map +1 -0
  55. package/dist/src/keys/GGHistogramKey.d.ts +13 -0
  56. package/dist/src/keys/GGHistogramKey.d.ts.map +1 -0
  57. package/dist/src/keys/GGHistogramKey.js +27 -0
  58. package/dist/src/keys/GGHistogramKey.js.map +1 -0
  59. package/dist/src/keys/GGLazyGaugeKey.d.ts +21 -0
  60. package/dist/src/keys/GGLazyGaugeKey.d.ts.map +1 -0
  61. package/dist/src/keys/GGLazyGaugeKey.js +25 -0
  62. package/dist/src/keys/GGLazyGaugeKey.js.map +1 -0
  63. package/dist/src/metric/GGCounter.d.ts +7 -0
  64. package/dist/src/metric/GGCounter.d.ts.map +1 -0
  65. package/dist/src/metric/GGCounter.js +14 -0
  66. package/dist/src/metric/GGCounter.js.map +1 -0
  67. package/dist/src/metric/GGGauge.d.ts +9 -0
  68. package/dist/src/metric/GGGauge.d.ts.map +1 -0
  69. package/dist/src/metric/GGGauge.js +31 -0
  70. package/dist/src/metric/GGGauge.js.map +1 -0
  71. package/dist/src/metric/GGHistogram.d.ts +27 -0
  72. package/dist/src/metric/GGHistogram.d.ts.map +1 -0
  73. package/dist/src/metric/GGHistogram.js +41 -0
  74. package/dist/src/metric/GGHistogram.js.map +1 -0
  75. package/dist/src/metric/GGLazyGauge.d.ts +20 -0
  76. package/dist/src/metric/GGLazyGauge.d.ts.map +1 -0
  77. package/dist/src/metric/GGLazyGauge.js +27 -0
  78. package/dist/src/metric/GGLazyGauge.js.map +1 -0
  79. package/dist/src/tsconfig.json +16 -0
  80. package/dist/testkit/GGMetricsCommands.d.ts +18 -0
  81. package/dist/testkit/GGMetricsCommands.d.ts.map +1 -0
  82. package/dist/testkit/GGMetricsCommands.js +77 -0
  83. package/dist/testkit/GGMetricsCommands.js.map +1 -0
  84. package/dist/testkit/GGMetricsInterceptor.d.ts +34 -0
  85. package/dist/testkit/GGMetricsInterceptor.d.ts.map +1 -0
  86. package/dist/testkit/GGMetricsInterceptor.js +202 -0
  87. package/dist/testkit/GGMetricsInterceptor.js.map +1 -0
  88. package/dist/testkit/GGMetricsWith.d.ts +22 -0
  89. package/dist/testkit/GGMetricsWith.d.ts.map +1 -0
  90. package/dist/testkit/GGMetricsWith.js +61 -0
  91. package/dist/testkit/GGMetricsWith.js.map +1 -0
  92. package/dist/testkit/GGTestMetricsExporter.d.ts +40 -0
  93. package/dist/testkit/GGTestMetricsExporter.d.ts.map +1 -0
  94. package/dist/testkit/GGTestMetricsExporter.js +119 -0
  95. package/dist/testkit/GGTestMetricsExporter.js.map +1 -0
  96. package/dist/testkit/GGTestSelectorMetrics.d.ts +15 -0
  97. package/dist/testkit/GGTestSelectorMetrics.d.ts.map +1 -0
  98. package/dist/testkit/GGTestSelectorMetrics.js +16 -0
  99. package/dist/testkit/GGTestSelectorMetrics.js.map +1 -0
  100. package/dist/testkit/index-testkit.d.ts +6 -0
  101. package/dist/testkit/index-testkit.d.ts.map +1 -0
  102. package/dist/testkit/index-testkit.js +6 -0
  103. package/dist/testkit/index-testkit.js.map +1 -0
  104. package/dist/tsconfig.publish.tsbuildinfo +1 -0
  105. package/package.json +58 -0
  106. package/src/GGMetric.ts +124 -0
  107. package/src/GGMetricKey.ts +38 -0
  108. package/src/GGMetrics.ts +34 -0
  109. package/src/GGMetricsDefineStorage.ts +8 -0
  110. package/src/GGMetricsLoader.ts +21 -0
  111. package/src/GGMetricsStore.ts +26 -0
  112. package/src/exporters/GGJsonMetricsExporter.ts +176 -0
  113. package/src/exporters/GGMetricsExporter.ts +88 -0
  114. package/src/exporters/GGNestedMetricsExporter.ts +335 -0
  115. package/src/index-browser.ts +16 -0
  116. package/src/index-node.ts +21 -0
  117. package/src/keys/GGCounterKey.ts +29 -0
  118. package/src/keys/GGGaugeKey.ts +37 -0
  119. package/src/keys/GGHistogramKey.ts +37 -0
  120. package/src/keys/GGLazyGaugeKey.ts +36 -0
  121. package/src/metric/GGCounter.ts +19 -0
  122. package/src/metric/GGGauge.ts +38 -0
  123. package/src/metric/GGHistogram.ts +68 -0
  124. package/src/metric/GGLazyGauge.ts +31 -0
  125. package/src/tsconfig.json +16 -0
@@ -0,0 +1,21 @@
1
+ import { GGMetricKey } from "../GGMetricKey.js";
2
+ import { GGLazyGauge } from "../metric/GGLazyGauge.js";
3
+ import { GGMetricOptionsBase } from "../GGMetric.js";
4
+ export interface LazyGaugeOptions extends GGMetricOptionsBase {
5
+ getValue: () => number;
6
+ }
7
+ /**
8
+ * Key for a lazy gauge metric.
9
+ * The getValue function is called whenever the metric value is read.
10
+ */
11
+ export declare class GGLazyGaugeKey extends GGMetricKey<{}, GGLazyGauge> {
12
+ readonly options: LazyGaugeOptions;
13
+ constructor(name: string, options: LazyGaugeOptions);
14
+ create(): GGLazyGauge;
15
+ /**
16
+ * Get the current value by calling the getValue function.
17
+ */
18
+ getValue(): number;
19
+ reset(): void;
20
+ }
21
+ //# sourceMappingURL=GGLazyGaugeKey.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGLazyGaugeKey.d.ts","sourceRoot":"","sources":["../../../src/keys/GGLazyGaugeKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAC,mBAAmB,EAAC,MAAM,gBAAgB,CAAC;AAEnD,MAAM,WAAW,gBAAiB,SAAQ,mBAAmB;IACzD,QAAQ,EAAE,MAAM,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,qBAAa,cAAe,SAAQ,WAAW,CAAC,EAAE,EAAE,WAAW,CAAC;IAE5D,SAAwB,OAAO,EAAE,gBAAgB,CAAC;gBAEtC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB;IAK5C,MAAM,IAAI,WAAW;IAI5B;;OAEG;IACI,QAAQ,IAAI,MAAM;IAIlB,KAAK,IAAI,IAAI;CAGvB"}
@@ -0,0 +1,25 @@
1
+ import { GGMetricKey } from "../GGMetricKey.js";
2
+ import { GGLazyGauge } from "../metric/GGLazyGauge.js";
3
+ /**
4
+ * Key for a lazy gauge metric.
5
+ * The getValue function is called whenever the metric value is read.
6
+ */
7
+ export class GGLazyGaugeKey extends GGMetricKey {
8
+ constructor(name, options) {
9
+ super(name, options);
10
+ Object.freeze(this);
11
+ }
12
+ create() {
13
+ return new GGLazyGauge(this);
14
+ }
15
+ /**
16
+ * Get the current value by calling the getValue function.
17
+ */
18
+ getValue() {
19
+ return this.getMetric().getValue();
20
+ }
21
+ reset() {
22
+ // No-op for lazy gauge - nothing to reset
23
+ }
24
+ }
25
+ //# sourceMappingURL=GGLazyGaugeKey.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGLazyGaugeKey.js","sourceRoot":"","sources":["../../../src/keys/GGLazyGaugeKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAOrD;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,WAA4B;IAI5D,YAAY,IAAY,EAAE,OAAyB;QAC/C,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACrB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAEM,MAAM;QACT,OAAO,IAAI,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED;;OAEG;IACI,QAAQ;QACX,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,CAAC;IACvC,CAAC;IAEM,KAAK;QACR,0CAA0C;IAC9C,CAAC;CACJ"}
@@ -0,0 +1,7 @@
1
+ import { GGMetric, GGMetricLabels, LabelsArgs } from "../GGMetric.js";
2
+ import type { GGCounterKey } from "../keys/GGCounterKey";
3
+ export declare class GGCounter<TLabels extends GGMetricLabels = {}> extends GGMetric<TLabels, number, GGCounterKey<TLabels>> {
4
+ protected getDefaultValue(): number;
5
+ inc(value?: number, ...args: LabelsArgs<TLabels>): void;
6
+ }
7
+ //# sourceMappingURL=GGCounter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGCounter.d.ts","sourceRoot":"","sources":["../../../src/metric/GGCounter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,cAAc,EAAE,UAAU,EAAC,MAAM,gBAAgB,CAAC;AACpE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,sBAAsB,CAAC;AAEvD,qBAAa,SAAS,CAClB,OAAO,SAAS,cAAc,GAAG,EAAE,CACrC,SAAQ,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;IAEtD,SAAS,CAAC,eAAe,IAAI,MAAM;IAI5B,GAAG,CAAC,KAAK,GAAE,MAAU,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI;CAOpE"}
@@ -0,0 +1,14 @@
1
+ import { GGMetric } from "../GGMetric.js";
2
+ export class GGCounter extends GGMetric {
3
+ getDefaultValue() {
4
+ return 0;
5
+ }
6
+ inc(value = 1, ...args) {
7
+ const key = this.getKey(args[0]);
8
+ const current = this.getByKey(key);
9
+ if (current !== undefined) {
10
+ this.setByKey(key, current + value);
11
+ }
12
+ }
13
+ }
14
+ //# sourceMappingURL=GGCounter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGCounter.js","sourceRoot":"","sources":["../../../src/metric/GGCounter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAA6B,MAAM,gBAAgB,CAAC;AAGpE,MAAM,OAAO,SAEX,SAAQ,QAAgD;IAE5C,eAAe;QACrB,OAAO,CAAC,CAAC;IACb,CAAC;IAEM,GAAG,CAAC,QAAgB,CAAC,EAAE,GAAG,IAAyB;QACtD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAY,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACxB,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAAC,CAAC;QACxC,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,9 @@
1
+ import { GGMetric, GGMetricLabels, LabelsArgs } from "../GGMetric.js";
2
+ import type { GGGaugeKey } from "../keys/GGGaugeKey";
3
+ export declare class GGGauge<TLabels extends GGMetricLabels = {}> extends GGMetric<TLabels, number, GGGaugeKey<TLabels>> {
4
+ protected getDefaultValue(): number;
5
+ set(value: number, ...args: LabelsArgs<TLabels>): void;
6
+ inc(value?: number, ...args: LabelsArgs<TLabels>): void;
7
+ dec(value?: number, ...args: LabelsArgs<TLabels>): void;
8
+ }
9
+ //# sourceMappingURL=GGGauge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGGauge.d.ts","sourceRoot":"","sources":["../../../src/metric/GGGauge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,cAAc,EAAE,UAAU,EAAC,MAAM,gBAAgB,CAAC;AACpE,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAEnD,qBAAa,OAAO,CAChB,OAAO,SAAS,cAAc,GAAG,EAAE,CACrC,SAAQ,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IAEpD,SAAS,CAAC,eAAe,IAAI,MAAM;IAI5B,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI;IAStD,GAAG,CAAC,KAAK,GAAE,MAAU,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI;IAS1D,GAAG,CAAC,KAAK,GAAE,MAAU,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI;CAQpE"}
@@ -0,0 +1,31 @@
1
+ import { GGMetric } from "../GGMetric.js";
2
+ export class GGGauge extends GGMetric {
3
+ getDefaultValue() {
4
+ return 0;
5
+ }
6
+ set(value, ...args) {
7
+ const key = this.getKey(args[0]);
8
+ const current = this.getByKey(key);
9
+ if (current === undefined) {
10
+ return;
11
+ }
12
+ this.setByKey(key, value);
13
+ }
14
+ inc(value = 1, ...args) {
15
+ const key = this.getKey(args[0]);
16
+ const current = this.getByKey(key);
17
+ if (current === undefined) {
18
+ return;
19
+ }
20
+ this.setByKey(key, current + value);
21
+ }
22
+ dec(value = 1, ...args) {
23
+ const key = this.getKey(args[0]);
24
+ const current = this.getByKey(key);
25
+ if (current === undefined) {
26
+ return;
27
+ }
28
+ this.setByKey(key, current - value);
29
+ }
30
+ }
31
+ //# sourceMappingURL=GGGauge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGGauge.js","sourceRoot":"","sources":["../../../src/metric/GGGauge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAA6B,MAAM,gBAAgB,CAAC;AAGpE,MAAM,OAAO,OAEX,SAAQ,QAA8C;IAE1C,eAAe;QACrB,OAAO,CAAC,CAAC;IACb,CAAC;IAEM,GAAG,CAAC,KAAa,EAAE,GAAG,IAAyB;QAClD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAY,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;IAEM,GAAG,CAAC,QAAgB,CAAC,EAAE,GAAG,IAAyB;QACtD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAY,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAAC,CAAC;IACxC,CAAC;IAEM,GAAG,CAAC,QAAgB,CAAC,EAAE,GAAG,IAAyB;QACtD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAY,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAAC,CAAC;IACxC,CAAC;CACJ"}
@@ -0,0 +1,27 @@
1
+ import { GGMetric, GGMetricLabels, GGMetricOptions, LabelsArgs } from "../GGMetric.js";
2
+ import type { GGHistogramKey } from "../keys/GGHistogramKey";
3
+ export type HistogramOptions<TLabels extends GGMetricLabels = {}> = GGMetricOptions<TLabels> & {
4
+ buckets: number[];
5
+ };
6
+ export declare class GGHistogram<TLabels extends GGMetricLabels = {}> extends GGMetric<TLabels, HistogramData, GGHistogramKey<TLabels>> {
7
+ getBuckets(): number[];
8
+ protected getDefaultValue(): HistogramData;
9
+ observe(value: number, ...args: LabelsArgs<TLabels>): void;
10
+ startTimer(...args: LabelsArgs<TLabels>): () => void;
11
+ }
12
+ export interface HistogramData {
13
+ count: number;
14
+ sum: number;
15
+ min: number;
16
+ max: number;
17
+ values: number[];
18
+ }
19
+ export interface SerializedHistogramData {
20
+ count: number;
21
+ sum: number;
22
+ min: number;
23
+ max: number;
24
+ buckets: number[];
25
+ values: number[];
26
+ }
27
+ //# sourceMappingURL=GGHistogram.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGHistogram.d.ts","sourceRoot":"","sources":["../../../src/metric/GGHistogram.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,cAAc,EAAE,eAAe,EAAE,UAAU,EAAC,MAAM,gBAAgB,CAAC;AACrF,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAE3D,MAAM,MAAM,gBAAgB,CAAC,OAAO,SAAS,cAAc,GAAG,EAAE,IAC5D,eAAe,CAAC,OAAO,CAAC,GAAG;IAAE,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAErD,qBAAa,WAAW,CACpB,OAAO,SAAS,cAAc,GAAG,EAAE,CACrC,SAAQ,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IAExD,UAAU,IAAI,MAAM,EAAE;IAI7B,SAAS,CAAC,eAAe,IAAI,aAAa;IAUnC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI;IAqB1D,UAAU,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI;CAK9D;AAED,MAAM,WAAW,aAAa;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,EAAE,CAAC;CACpB"}
@@ -0,0 +1,41 @@
1
+ import { GGMetric } from "../GGMetric.js";
2
+ export class GGHistogram extends GGMetric {
3
+ getBuckets() {
4
+ return this.key.buckets;
5
+ }
6
+ getDefaultValue() {
7
+ return {
8
+ count: 0,
9
+ sum: 0,
10
+ min: Infinity,
11
+ max: -Infinity,
12
+ values: this.key.buckets.map(() => 0)
13
+ };
14
+ }
15
+ observe(value, ...args) {
16
+ const key = this.getKey(args[0]);
17
+ const data = this.getByKey(key);
18
+ if (data === undefined) {
19
+ return;
20
+ }
21
+ data.count++;
22
+ data.sum += value;
23
+ if (value < data.min) {
24
+ data.min = value;
25
+ }
26
+ if (value > data.max) {
27
+ data.max = value;
28
+ }
29
+ for (let i = 0; i < this.key.buckets.length; i++) {
30
+ if (value <= this.key.buckets[i]) {
31
+ data.values[i]++;
32
+ }
33
+ }
34
+ }
35
+ startTimer(...args) {
36
+ const start = Date.now();
37
+ const labels = args[0];
38
+ return () => this.observe(Date.now() - start, ...[labels]);
39
+ }
40
+ }
41
+ //# sourceMappingURL=GGHistogram.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGHistogram.js","sourceRoot":"","sources":["../../../src/metric/GGHistogram.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAA8C,MAAM,gBAAgB,CAAC;AAMrF,MAAM,OAAO,WAEX,SAAQ,QAAyD;IAExD,UAAU;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC5B,CAAC;IAES,eAAe;QACrB,OAAO;YACH,KAAK,EAAE,CAAC;YACR,GAAG,EAAE,CAAC;YACN,GAAG,EAAE,QAAQ;YACb,GAAG,EAAE,CAAC,QAAQ;YACd,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;SACxC,CAAC;IACN,CAAC;IAEM,OAAO,CAAC,KAAa,EAAE,GAAG,IAAyB;QACtD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAY,CAAC,CAAC;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACb,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC;QAClB,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACnB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC;QACrB,CAAC;QACD,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACnB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC;QACrB,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/C,IAAI,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC/B,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YACrB,CAAC;QACL,CAAC;IACL,CAAC;IAEM,UAAU,CAAC,GAAG,IAAyB;QAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,CAAC,CAAY,CAAC;QAClC,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,EAAE,GAAI,CAAC,MAAM,CAAyB,CAAC,CAAC;IACxF,CAAC;CACJ"}
@@ -0,0 +1,20 @@
1
+ import { GGMetric } from "../GGMetric.js";
2
+ import type { GGLazyGaugeKey } from "../keys/GGLazyGaugeKey";
3
+ /**
4
+ * A gauge that computes its value lazily by calling a function.
5
+ * Perfect for "current state" metrics like memory usage, active handles, etc.
6
+ * Does not support labels - use regular GGGauge if you need labels.
7
+ */
8
+ export declare class GGLazyGauge extends GGMetric<{}, number, GGLazyGaugeKey> {
9
+ protected getDefaultValue(): number;
10
+ /**
11
+ * Get the current value by calling the getValue function.
12
+ */
13
+ getValue(): number;
14
+ /**
15
+ * Returns a map with single entry (empty key -> current value).
16
+ * Compatible with exporter iteration pattern.
17
+ */
18
+ getValues(): Map<string, number>;
19
+ }
20
+ //# sourceMappingURL=GGLazyGauge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGLazyGauge.d.ts","sourceRoot":"","sources":["../../../src/metric/GGLazyGauge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACxC,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAE3D;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,cAAc,CAAC;IAEjE,SAAS,CAAC,eAAe,IAAI,MAAM;IAInC;;OAEG;IACI,QAAQ,IAAI,MAAM;IAIzB;;;OAGG;IACI,SAAS,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;CAK1C"}
@@ -0,0 +1,27 @@
1
+ import { GGMetric } from "../GGMetric.js";
2
+ /**
3
+ * A gauge that computes its value lazily by calling a function.
4
+ * Perfect for "current state" metrics like memory usage, active handles, etc.
5
+ * Does not support labels - use regular GGGauge if you need labels.
6
+ */
7
+ export class GGLazyGauge extends GGMetric {
8
+ getDefaultValue() {
9
+ return 0;
10
+ }
11
+ /**
12
+ * Get the current value by calling the getValue function.
13
+ */
14
+ getValue() {
15
+ return this.key.options.getValue();
16
+ }
17
+ /**
18
+ * Returns a map with single entry (empty key -> current value).
19
+ * Compatible with exporter iteration pattern.
20
+ */
21
+ getValues() {
22
+ const map = new Map();
23
+ map.set('', this.getValue());
24
+ return map;
25
+ }
26
+ }
27
+ //# sourceMappingURL=GGLazyGauge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGLazyGauge.js","sourceRoot":"","sources":["../../../src/metric/GGLazyGauge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAGxC;;;;GAIG;AACH,MAAM,OAAO,WAAY,SAAQ,QAAoC;IAEvD,eAAe;QACrB,OAAO,CAAC,CAAC;IACb,CAAC;IAED;;OAEG;IACI,QAAQ;QACX,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACvC,CAAC;IAED;;;OAGG;IACI,SAAS;QACZ,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;QACtC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC7B,OAAO,GAAG,CAAC;IACf,CAAC;CACJ"}
@@ -0,0 +1,16 @@
1
+ {
2
+ "//": "THIS FILE IS GENERATED - DO NOT EDIT",
3
+ "extends": "../../../tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "rootDir": ".",
6
+ "lib": [
7
+ "ES2022"
8
+ ],
9
+ "types": [
10
+ "node"
11
+ ]
12
+ },
13
+ "include": [
14
+ "**/*"
15
+ ]
16
+ }
@@ -0,0 +1,18 @@
1
+ import { SerializedHistogramData } from "../src/metric/GGHistogram.js";
2
+ export interface MetricSnapshotRequest {
3
+ metricName: string;
4
+ }
5
+ export interface MetricSnapshotResponse {
6
+ name: string;
7
+ type: string;
8
+ help: string;
9
+ buckets?: number[];
10
+ values: Array<[string, number | SerializedHistogramData]>;
11
+ }
12
+ export declare const GGMetricsIPC: {
13
+ worker: {
14
+ getSnapshot: import("@grest-ts/ipc").IPCClientRequest<MetricSnapshotRequest, MetricSnapshotResponse>;
15
+ reset: import("@grest-ts/ipc").IPCClientRequest<void, void>;
16
+ };
17
+ };
18
+ //# sourceMappingURL=GGMetricsCommands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGMetricsCommands.d.ts","sourceRoot":"","sources":["../../testkit/GGMetricsCommands.ts"],"names":[],"mappings":"AAKA,OAAO,EAA6B,uBAAuB,EAAC,MAAM,8BAA8B,CAAC;AAEjG,MAAM,WAAW,qBAAqB;IAClC,UAAU,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,sBAAsB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,uBAAuB,CAAC,CAAC,CAAC;CAC7D;AAaD,eAAO,MAAM,YAAY;;;;;CAKxB,CAAA"}
@@ -0,0 +1,77 @@
1
+ import { GG_TEST_RUNTIME_WORKER, GGTestRuntimeWorker } from "@grest-ts/testkit";
2
+ import { IPCClient } from "@grest-ts/ipc";
3
+ import { GG_METRICS } from "../src/GGMetricsLoader.js";
4
+ import { GGCounter } from "../src/metric/GGCounter.js";
5
+ import { GGGauge } from "../src/metric/GGGauge.js";
6
+ import { GGHistogram } from "../src/metric/GGHistogram.js";
7
+ function serializeHistogramData(data, buckets) {
8
+ return {
9
+ count: data.count,
10
+ sum: data.sum,
11
+ min: data.min,
12
+ max: data.max,
13
+ buckets: buckets,
14
+ values: [...data.values]
15
+ };
16
+ }
17
+ export const GGMetricsIPC = {
18
+ worker: {
19
+ getSnapshot: IPCClient.defineRequest("metrics.getSnapshot"),
20
+ reset: IPCClient.defineRequest("metrics.reset"),
21
+ }
22
+ };
23
+ GGTestRuntimeWorker.onBeforeRuntimeStart(() => {
24
+ const worker = GG_TEST_RUNTIME_WORKER.get();
25
+ worker.onIpcRequest(GGMetricsIPC.worker.getSnapshot, (payload) => {
26
+ const store = GG_METRICS.tryGet();
27
+ if (!store)
28
+ return null;
29
+ for (const metric of store.getAllMetrics()) {
30
+ if (metric.name === payload.metricName) {
31
+ if (metric instanceof GGCounter) {
32
+ const values = [];
33
+ for (const [key, value] of metric.getValues()) {
34
+ values.push([key, value]);
35
+ }
36
+ return {
37
+ name: metric.name,
38
+ type: 'counter',
39
+ help: metric.key.help,
40
+ values
41
+ };
42
+ }
43
+ else if (metric instanceof GGGauge) {
44
+ const values = [];
45
+ for (const [key, value] of metric.getValues()) {
46
+ values.push([key, value]);
47
+ }
48
+ return {
49
+ name: metric.name,
50
+ type: 'gauge',
51
+ help: metric.key.help,
52
+ values
53
+ };
54
+ }
55
+ else if (metric instanceof GGHistogram) {
56
+ const buckets = metric.getBuckets();
57
+ const values = [];
58
+ for (const [key, data] of metric.getValues()) {
59
+ values.push([key, serializeHistogramData(data, buckets)]);
60
+ }
61
+ return {
62
+ name: metric.name,
63
+ type: 'histogram',
64
+ help: metric.key.help,
65
+ buckets,
66
+ values
67
+ };
68
+ }
69
+ }
70
+ }
71
+ return null;
72
+ });
73
+ worker.onIpcRequest(GGMetricsIPC.worker.reset, () => {
74
+ GG_METRICS.tryGet()?.reset();
75
+ });
76
+ });
77
+ //# sourceMappingURL=GGMetricsCommands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGMetricsCommands.js","sourceRoot":"","sources":["../../testkit/GGMetricsCommands.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAE,mBAAmB,EAAC,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AACxC,OAAO,EAAC,UAAU,EAAC,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAC,SAAS,EAAC,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAC,OAAO,EAAC,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAC,WAAW,EAAyC,MAAM,8BAA8B,CAAC;AAcjG,SAAS,sBAAsB,CAAC,IAAmB,EAAE,OAAiB;IAClE,OAAO;QACH,KAAK,EAAE,IAAI,CAAC,KAAK;QACjB,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,OAAO,EAAE,OAAO;QAChB,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;KAC3B,CAAC;AACN,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB,MAAM,EAAE;QACJ,WAAW,EAAE,SAAS,CAAC,aAAa,CAAuD,qBAAqB,CAAC;QACjH,KAAK,EAAE,SAAS,CAAC,aAAa,CAAa,eAAe,CAAC;KAC9D;CACJ,CAAA;AAED,mBAAmB,CAAC,oBAAoB,CAAC,GAAG,EAAE;IAC1C,MAAM,MAAM,GAAG,sBAAsB,CAAC,GAAG,EAAE,CAAC;IAE5C,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,OAAO,EAAE,EAAE;QAC7D,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC;QAClC,IAAI,CAAC,KAAK;YAAE,OAAO,IAAI,CAAC;QAExB,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,aAAa,EAAE,EAAE,CAAC;YACzC,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO,CAAC,UAAU,EAAE,CAAC;gBACrC,IAAI,MAAM,YAAY,SAAS,EAAE,CAAC;oBAC9B,MAAM,MAAM,GAA4B,EAAE,CAAC;oBAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;wBAC5C,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;oBAC9B,CAAC;oBACD,OAAO;wBACH,IAAI,EAAE,MAAM,CAAC,IAAI;wBACjB,IAAI,EAAE,SAAS;wBACf,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI;wBACrB,MAAM;qBACT,CAAC;gBACN,CAAC;qBAAM,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;oBACnC,MAAM,MAAM,GAA4B,EAAE,CAAC;oBAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;wBAC5C,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;oBAC9B,CAAC;oBACD,OAAO;wBACH,IAAI,EAAE,MAAM,CAAC,IAAI;wBACjB,IAAI,EAAE,OAAO;wBACb,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI;wBACrB,MAAM;qBACT,CAAC;gBACN,CAAC;qBAAM,IAAI,MAAM,YAAY,WAAW,EAAE,CAAC;oBACvC,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;oBACpC,MAAM,MAAM,GAA6C,EAAE,CAAC;oBAC5D,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,SAAS,EAAgC,EAAE,CAAC;wBACzE,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,sBAAsB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;oBAC9D,CAAC;oBACD,OAAO;wBACH,IAAI,EAAE,MAAM,CAAC,IAAI;wBACjB,IAAI,EAAE,WAAW;wBACjB,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI;wBACrB,OAAO;wBACP,MAAM;qBACT,CAAC;gBACN,CAAC;YACL,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,EAAE;QAChD,UAAU,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC;IACjC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { IGGTestInterceptor } from "@grest-ts/testkit";
2
+ import type { GGTestRuntime } from "@grest-ts/testkit";
3
+ import { GGMetricLabels } from "../src/GGMetric.js";
4
+ export interface MetricExpectation<TLabels> {
5
+ labels: TLabels;
6
+ delta: number;
7
+ type: 'inc' | 'incAtLeast' | 'dec' | 'observation' | 'noChange';
8
+ }
9
+ export declare class GGMetricsInterceptor<TLabels extends GGMetricLabels> implements IGGTestInterceptor {
10
+ private readonly metricName;
11
+ private readonly labelNames;
12
+ private readonly runtimes;
13
+ private readonly expectations;
14
+ private readonly noChangeExpected;
15
+ private readonly definedInSourceFile;
16
+ private beforeSnapshot;
17
+ private validationError;
18
+ private wasRegistered;
19
+ constructor(metricName: string, labelNames: readonly string[], runtimes: GGTestRuntime[], expectations: MetricExpectation<TLabels>[], noChangeExpected: boolean, definedInSourceFile: string);
20
+ register(): Promise<void>;
21
+ unregister(): void;
22
+ validate(): Promise<void>;
23
+ private getSnapshotsFromRuntimes;
24
+ private mergeSnapshots;
25
+ private mergeValues;
26
+ private validateNoChange;
27
+ private validateExpectation;
28
+ private getCount;
29
+ private valuesEqual;
30
+ private labelsToKey;
31
+ getMockValidationError(): Error | undefined;
32
+ isCalled(): boolean;
33
+ }
34
+ //# sourceMappingURL=GGMetricsInterceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GGMetricsInterceptor.d.ts","sourceRoot":"","sources":["../../testkit/GGMetricsInterceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,mBAAmB,CAAC;AAErD,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAGlD,MAAM,WAAW,iBAAiB,CAAC,OAAO;IACtC,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,KAAK,GAAG,YAAY,GAAG,KAAK,GAAG,aAAa,GAAG,UAAU,CAAC;CACnE;AA8BD,qBAAa,oBAAoB,CAAC,OAAO,SAAS,cAAc,CAAE,YAAW,kBAAkB;IAMvF,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,mBAAmB;IAVxC,OAAO,CAAC,cAAc,CAAkD;IACxE,OAAO,CAAC,eAAe,CAAoB;IAC3C,OAAO,CAAC,aAAa,CAAS;gBAGT,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,QAAQ,EAAE,aAAa,EAAE,EACzB,YAAY,EAAE,iBAAiB,CAAC,OAAO,CAAC,EAAE,EAC1C,gBAAgB,EAAE,OAAO,EACzB,mBAAmB,EAAE,MAAM;IAG1C,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAM/B,UAAU,IAAI,IAAI;IAEZ,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;YAkBjB,wBAAwB;IAkBtC,OAAO,CAAC,cAAc;IA0BtB,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,gBAAgB;IAqBxB,OAAO,CAAC,mBAAmB;IAgE3B,OAAO,CAAC,QAAQ;IAMhB,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,WAAW;IAQnB,sBAAsB,IAAI,KAAK,GAAG,SAAS;IAI3C,QAAQ,IAAI,OAAO;CAGtB"}
@@ -0,0 +1,202 @@
1
+ import { GGMetricsIPC } from "./GGMetricsCommands.js";
2
+ function deserializeHistogramData(data) {
3
+ return {
4
+ count: data.count,
5
+ sum: data.sum,
6
+ min: data.min,
7
+ max: data.max,
8
+ values: [...data.values]
9
+ };
10
+ }
11
+ function isSerializedHistogram(value) {
12
+ return typeof value === 'object' && value !== null && 'buckets' in value && 'count' in value;
13
+ }
14
+ function snapshotToMap(snapshot) {
15
+ const map = new Map();
16
+ if (!snapshot)
17
+ return map;
18
+ for (const [key, value] of snapshot.values) {
19
+ if (isSerializedHistogram(value)) {
20
+ map.set(key, deserializeHistogramData(value));
21
+ }
22
+ else {
23
+ map.set(key, value);
24
+ }
25
+ }
26
+ return map;
27
+ }
28
+ export class GGMetricsInterceptor {
29
+ metricName;
30
+ labelNames;
31
+ runtimes;
32
+ expectations;
33
+ noChangeExpected;
34
+ definedInSourceFile;
35
+ beforeSnapshot = new Map();
36
+ validationError;
37
+ wasRegistered = false;
38
+ constructor(metricName, labelNames, runtimes, expectations, noChangeExpected, definedInSourceFile) {
39
+ this.metricName = metricName;
40
+ this.labelNames = labelNames;
41
+ this.runtimes = runtimes;
42
+ this.expectations = expectations;
43
+ this.noChangeExpected = noChangeExpected;
44
+ this.definedInSourceFile = definedInSourceFile;
45
+ }
46
+ async register() {
47
+ this.wasRegistered = true;
48
+ const snapshots = await this.getSnapshotsFromRuntimes();
49
+ this.beforeSnapshot = this.mergeSnapshots(snapshots);
50
+ }
51
+ unregister() { }
52
+ async validate() {
53
+ const afterSnapshots = await this.getSnapshotsFromRuntimes();
54
+ const after = this.mergeSnapshots(afterSnapshots);
55
+ try {
56
+ if (this.noChangeExpected) {
57
+ this.validateNoChange(this.beforeSnapshot, after);
58
+ }
59
+ for (const expectation of this.expectations) {
60
+ this.validateExpectation(expectation, this.beforeSnapshot, after);
61
+ }
62
+ }
63
+ catch (e) {
64
+ this.validationError = e instanceof Error ? e : new Error(String(e));
65
+ throw this.validationError;
66
+ }
67
+ }
68
+ async getSnapshotsFromRuntimes() {
69
+ const results = [];
70
+ await Promise.all(this.runtimes.map(async (runtime) => {
71
+ const snapshot = await runtime.sendCommand(GGMetricsIPC.worker.getSnapshot, { metricName: this.metricName });
72
+ if (snapshot) {
73
+ results.push(snapshot);
74
+ }
75
+ }));
76
+ return results;
77
+ }
78
+ mergeSnapshots(snapshots) {
79
+ const merged = new Map();
80
+ for (const snapshot of snapshots) {
81
+ const map = snapshotToMap(snapshot);
82
+ for (const [key, value] of map) {
83
+ const existing = merged.get(key);
84
+ if (existing === undefined) {
85
+ merged.set(key, value);
86
+ }
87
+ else if (typeof existing === 'number' && typeof value === 'number') {
88
+ merged.set(key, existing + value);
89
+ }
90
+ else if (typeof existing === 'object' && typeof value === 'object') {
91
+ merged.set(key, {
92
+ count: existing.count + value.count,
93
+ sum: existing.sum + value.sum,
94
+ min: Math.min(existing.min, value.min),
95
+ max: Math.max(existing.max, value.max),
96
+ values: this.mergeValues(existing.values, value.values)
97
+ });
98
+ }
99
+ }
100
+ }
101
+ return merged;
102
+ }
103
+ mergeValues(a, b) {
104
+ const result = [];
105
+ const len = Math.max(a.length, b.length);
106
+ for (let i = 0; i < len; i++) {
107
+ result.push((a[i] ?? 0) + (b[i] ?? 0));
108
+ }
109
+ return result;
110
+ }
111
+ validateNoChange(before, after) {
112
+ for (const [key, beforeVal] of before) {
113
+ const afterVal = after.get(key);
114
+ if (!this.valuesEqual(beforeVal, afterVal)) {
115
+ throw new Error(`[Metrics Test Failed] Expected no change in metric "${this.metricName}" ` +
116
+ `but {${key}} changed\n\t${this.definedInSourceFile}`);
117
+ }
118
+ }
119
+ for (const key of after.keys()) {
120
+ if (!before.has(key)) {
121
+ throw new Error(`[Metrics Test Failed] Expected no change in metric "${this.metricName}" ` +
122
+ `but new label combination {${key}} appeared\n\t${this.definedInSourceFile}`);
123
+ }
124
+ }
125
+ }
126
+ validateExpectation(exp, before, after) {
127
+ const key = this.labelsToKey(exp.labels);
128
+ const beforeVal = before.get(key);
129
+ const afterVal = after.get(key);
130
+ switch (exp.type) {
131
+ case 'inc':
132
+ case 'dec': {
133
+ const beforeNum = typeof beforeVal === 'number' ? beforeVal : 0;
134
+ const afterNum = typeof afterVal === 'number' ? afterVal : 0;
135
+ const actualDelta = afterNum - beforeNum;
136
+ if (actualDelta !== exp.delta) {
137
+ throw new Error(`[Metrics Test Failed] Expected metric "${this.metricName}" {${key || '(no labels)'}} ` +
138
+ `to change by ${exp.delta}, but actual delta was ${actualDelta}\n\t${this.definedInSourceFile}`);
139
+ }
140
+ break;
141
+ }
142
+ case 'incAtLeast': {
143
+ const beforeNum = typeof beforeVal === 'number' ? beforeVal : 0;
144
+ const afterNum = typeof afterVal === 'number' ? afterVal : 0;
145
+ const actualDelta = afterNum - beforeNum;
146
+ if (actualDelta < exp.delta) {
147
+ throw new Error(`[Metrics Test Failed] Expected metric "${this.metricName}" {${key || '(no labels)'}} ` +
148
+ `to change by at least ${exp.delta}, but actual delta was ${actualDelta}\n\t${this.definedInSourceFile}`);
149
+ }
150
+ break;
151
+ }
152
+ case 'observation': {
153
+ const beforeCount = this.getCount(beforeVal);
154
+ const afterCount = this.getCount(afterVal);
155
+ if (afterCount <= beforeCount) {
156
+ throw new Error(`[Metrics Test Failed] Expected observation in metric "${this.metricName}" {${key || '(no labels)'}} ` +
157
+ `but count did not increase\n\t${this.definedInSourceFile}`);
158
+ }
159
+ break;
160
+ }
161
+ case 'noChange': {
162
+ if (!this.valuesEqual(beforeVal, afterVal)) {
163
+ throw new Error(`[Metrics Test Failed] Expected no change for metric "${this.metricName}" {${key || '(no labels)'}} ` +
164
+ `but value changed\n\t${this.definedInSourceFile}`);
165
+ }
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ getCount(val) {
171
+ if (val === undefined)
172
+ return 0;
173
+ if (typeof val === 'number')
174
+ return val;
175
+ return val.count;
176
+ }
177
+ valuesEqual(a, b) {
178
+ if (a === b)
179
+ return true;
180
+ if (typeof a === 'number' && typeof b === 'number')
181
+ return a === b;
182
+ if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
183
+ return a.count === b.count && a.sum === b.sum;
184
+ }
185
+ return false;
186
+ }
187
+ labelsToKey(labels) {
188
+ if (!labels || Object.keys(labels).length === 0)
189
+ return '';
190
+ // Use the same order as the metric's labelNames
191
+ return this.labelNames
192
+ .map(name => `${name}=${labels[name] ?? ''}`)
193
+ .join(',');
194
+ }
195
+ getMockValidationError() {
196
+ return this.validationError;
197
+ }
198
+ isCalled() {
199
+ return this.wasRegistered;
200
+ }
201
+ }
202
+ //# sourceMappingURL=GGMetricsInterceptor.js.map