@grest-ts/metrics 0.0.6 → 0.0.7
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.
- package/LICENSE +21 -21
- package/README.md +40 -40
- package/dist/tsconfig.publish.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/GGMetric.ts +124 -124
- package/src/GGMetricKey.ts +38 -38
- package/src/GGMetrics.ts +34 -34
- package/src/GGMetricsDefineStorage.ts +8 -8
- package/src/GGMetricsLoader.ts +21 -21
- package/src/GGMetricsStore.ts +26 -26
- package/src/exporters/GGJsonMetricsExporter.ts +176 -176
- package/src/exporters/GGMetricsExporter.ts +88 -88
- package/src/exporters/GGNestedMetricsExporter.ts +335 -335
- package/src/index-browser.ts +16 -16
- package/src/index-node.ts +21 -21
- package/src/keys/GGCounterKey.ts +29 -29
- package/src/keys/GGGaugeKey.ts +37 -37
- package/src/keys/GGHistogramKey.ts +37 -37
- package/src/keys/GGLazyGaugeKey.ts +36 -36
- package/src/metric/GGCounter.ts +19 -19
- package/src/metric/GGGauge.ts +38 -38
- package/src/metric/GGHistogram.ts +68 -68
- package/src/metric/GGLazyGauge.ts +31 -31
package/src/index-browser.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export * from './GGMetric';
|
|
2
|
-
export * from './metric/GGCounter';
|
|
3
|
-
export * from './metric/GGGauge';
|
|
4
|
-
export * from './metric/GGLazyGauge';
|
|
5
|
-
export * from './metric/GGHistogram';
|
|
6
|
-
export * from './GGMetrics';
|
|
7
|
-
export * from './GGMetricsLoader';
|
|
8
|
-
export * from "./GGMetricKey";
|
|
9
|
-
export * from "./keys/GGCounterKey";
|
|
10
|
-
export * from "./keys/GGGaugeKey";
|
|
11
|
-
export * from "./keys/GGLazyGaugeKey";
|
|
12
|
-
export * from "./keys/GGHistogramKey";
|
|
13
|
-
export * from './exporters/GGMetricsExporter';
|
|
14
|
-
export * from './exporters/GGJsonMetricsExporter';
|
|
15
|
-
export * from './exporters/GGNestedMetricsExporter';
|
|
16
|
-
export * from "./GGMetricsStore";
|
|
1
|
+
export * from './GGMetric';
|
|
2
|
+
export * from './metric/GGCounter';
|
|
3
|
+
export * from './metric/GGGauge';
|
|
4
|
+
export * from './metric/GGLazyGauge';
|
|
5
|
+
export * from './metric/GGHistogram';
|
|
6
|
+
export * from './GGMetrics';
|
|
7
|
+
export * from './GGMetricsLoader';
|
|
8
|
+
export * from "./GGMetricKey";
|
|
9
|
+
export * from "./keys/GGCounterKey";
|
|
10
|
+
export * from "./keys/GGGaugeKey";
|
|
11
|
+
export * from "./keys/GGLazyGaugeKey";
|
|
12
|
+
export * from "./keys/GGHistogramKey";
|
|
13
|
+
export * from './exporters/GGMetricsExporter';
|
|
14
|
+
export * from './exporters/GGJsonMetricsExporter';
|
|
15
|
+
export * from './exporters/GGNestedMetricsExporter';
|
|
16
|
+
export * from "./GGMetricsStore";
|
package/src/index-node.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {AsyncLocalStorage} from "node:async_hooks";
|
|
2
|
-
import {_initMetricsStorage} from "./GGMetricsDefineStorage";
|
|
3
|
-
_initMetricsStorage(new AsyncLocalStorage());
|
|
4
|
-
|
|
5
|
-
export * from './GGMetric';
|
|
6
|
-
export * from './metric/GGCounter';
|
|
7
|
-
export * from './metric/GGGauge';
|
|
8
|
-
export * from './metric/GGLazyGauge';
|
|
9
|
-
export * from './metric/GGHistogram';
|
|
10
|
-
export * from './GGMetrics';
|
|
11
|
-
export * from './GGMetricsLoader';
|
|
12
|
-
export * from "./GGMetricKey";
|
|
13
|
-
export * from "./keys/GGCounterKey";
|
|
14
|
-
export * from "./keys/GGGaugeKey";
|
|
15
|
-
export * from "./keys/GGLazyGaugeKey";
|
|
16
|
-
export * from "./keys/GGHistogramKey";
|
|
17
|
-
export * from './exporters/GGMetricsExporter';
|
|
18
|
-
export * from './exporters/GGJsonMetricsExporter';
|
|
19
|
-
export * from './exporters/GGNestedMetricsExporter';
|
|
20
|
-
export * from "./GGMetricsStore";
|
|
21
|
-
|
|
1
|
+
import {AsyncLocalStorage} from "node:async_hooks";
|
|
2
|
+
import {_initMetricsStorage} from "./GGMetricsDefineStorage";
|
|
3
|
+
_initMetricsStorage(new AsyncLocalStorage());
|
|
4
|
+
|
|
5
|
+
export * from './GGMetric';
|
|
6
|
+
export * from './metric/GGCounter';
|
|
7
|
+
export * from './metric/GGGauge';
|
|
8
|
+
export * from './metric/GGLazyGauge';
|
|
9
|
+
export * from './metric/GGHistogram';
|
|
10
|
+
export * from './GGMetrics';
|
|
11
|
+
export * from './GGMetricsLoader';
|
|
12
|
+
export * from "./GGMetricKey";
|
|
13
|
+
export * from "./keys/GGCounterKey";
|
|
14
|
+
export * from "./keys/GGGaugeKey";
|
|
15
|
+
export * from "./keys/GGLazyGaugeKey";
|
|
16
|
+
export * from "./keys/GGHistogramKey";
|
|
17
|
+
export * from './exporters/GGMetricsExporter';
|
|
18
|
+
export * from './exporters/GGJsonMetricsExporter';
|
|
19
|
+
export * from './exporters/GGNestedMetricsExporter';
|
|
20
|
+
export * from "./GGMetricsStore";
|
|
21
|
+
|
package/src/keys/GGCounterKey.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
-
import {GGCounter} from "../metric/GGCounter.js";
|
|
3
|
-
import {GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
4
|
-
|
|
5
|
-
export class GGCounterKey<
|
|
6
|
-
TLabels extends GGMetricLabels = {}
|
|
7
|
-
> extends GGMetricKey<TLabels, GGCounter<TLabels>> {
|
|
8
|
-
|
|
9
|
-
constructor(name: string, options: GGMetricOptions<TLabels>) {
|
|
10
|
-
super(name, options);
|
|
11
|
-
Object.freeze(this);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public create(): GGCounter<TLabels> {
|
|
15
|
-
return new GGCounter<TLabels>(this);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
19
|
-
this.getMetric().inc(value, ...args);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public getValue(...args: LabelsArgs<TLabels>): number {
|
|
23
|
-
return this.getMetric().getValue(...args);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public reset(): void {
|
|
27
|
-
this.getMetric().reset();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
+
import {GGCounter} from "../metric/GGCounter.js";
|
|
3
|
+
import {GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
4
|
+
|
|
5
|
+
export class GGCounterKey<
|
|
6
|
+
TLabels extends GGMetricLabels = {}
|
|
7
|
+
> extends GGMetricKey<TLabels, GGCounter<TLabels>> {
|
|
8
|
+
|
|
9
|
+
constructor(name: string, options: GGMetricOptions<TLabels>) {
|
|
10
|
+
super(name, options);
|
|
11
|
+
Object.freeze(this);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public create(): GGCounter<TLabels> {
|
|
15
|
+
return new GGCounter<TLabels>(this);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
19
|
+
this.getMetric().inc(value, ...args);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public getValue(...args: LabelsArgs<TLabels>): number {
|
|
23
|
+
return this.getMetric().getValue(...args);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public reset(): void {
|
|
27
|
+
this.getMetric().reset();
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/keys/GGGaugeKey.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
-
import {GGGauge} from "../metric/GGGauge.js";
|
|
3
|
-
import {GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
4
|
-
|
|
5
|
-
export class GGGaugeKey<
|
|
6
|
-
TLabels extends GGMetricLabels = {}
|
|
7
|
-
> extends GGMetricKey<TLabels, GGGauge<TLabels>> {
|
|
8
|
-
|
|
9
|
-
constructor(name: string, options: GGMetricOptions<TLabels>) {
|
|
10
|
-
super(name, options);
|
|
11
|
-
Object.freeze(this);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public create(): GGGauge<TLabels> {
|
|
15
|
-
return new GGGauge<TLabels>(this);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public set(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
19
|
-
this.getMetric().set(value, ...args);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
23
|
-
this.getMetric().inc(value, ...args);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public dec(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
27
|
-
this.getMetric().dec(value, ...args);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getValue(...args: LabelsArgs<TLabels>): number {
|
|
31
|
-
return this.getMetric().getValue(...args);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public reset(): void {
|
|
35
|
-
this.getMetric().reset();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
+
import {GGGauge} from "../metric/GGGauge.js";
|
|
3
|
+
import {GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
4
|
+
|
|
5
|
+
export class GGGaugeKey<
|
|
6
|
+
TLabels extends GGMetricLabels = {}
|
|
7
|
+
> extends GGMetricKey<TLabels, GGGauge<TLabels>> {
|
|
8
|
+
|
|
9
|
+
constructor(name: string, options: GGMetricOptions<TLabels>) {
|
|
10
|
+
super(name, options);
|
|
11
|
+
Object.freeze(this);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public create(): GGGauge<TLabels> {
|
|
15
|
+
return new GGGauge<TLabels>(this);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public set(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
19
|
+
this.getMetric().set(value, ...args);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
23
|
+
this.getMetric().inc(value, ...args);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public dec(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
27
|
+
this.getMetric().dec(value, ...args);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getValue(...args: LabelsArgs<TLabels>): number {
|
|
31
|
+
return this.getMetric().getValue(...args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public reset(): void {
|
|
35
|
+
this.getMetric().reset();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
-
import {GGHistogram, HistogramData, HistogramOptions} from "../metric/GGHistogram.js";
|
|
3
|
-
import {GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
4
|
-
|
|
5
|
-
export class GGHistogramKey<
|
|
6
|
-
TLabels extends GGMetricLabels = {}
|
|
7
|
-
> extends GGMetricKey<TLabels, GGHistogram<TLabels>> {
|
|
8
|
-
|
|
9
|
-
public readonly buckets: number[];
|
|
10
|
-
|
|
11
|
-
constructor(name: string, options: HistogramOptions<TLabels>) {
|
|
12
|
-
super(name, options);
|
|
13
|
-
this.buckets = options.buckets.sort((a, b) => a - b)
|
|
14
|
-
Object.freeze(this.buckets)
|
|
15
|
-
Object.freeze(this);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public create(): GGHistogram<TLabels> {
|
|
19
|
-
return new GGHistogram<TLabels>(this);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public observe(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
23
|
-
this.getMetric().observe(value, ...args);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public startTimer(...args: LabelsArgs<TLabels>): () => void {
|
|
27
|
-
return this.getMetric().startTimer(...args);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getValue(...args: LabelsArgs<TLabels>): HistogramData | undefined {
|
|
31
|
-
return this.getMetric().getValue(...args);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public reset(): void {
|
|
35
|
-
this.getMetric().reset();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
+
import {GGHistogram, HistogramData, HistogramOptions} from "../metric/GGHistogram.js";
|
|
3
|
+
import {GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
4
|
+
|
|
5
|
+
export class GGHistogramKey<
|
|
6
|
+
TLabels extends GGMetricLabels = {}
|
|
7
|
+
> extends GGMetricKey<TLabels, GGHistogram<TLabels>> {
|
|
8
|
+
|
|
9
|
+
public readonly buckets: number[];
|
|
10
|
+
|
|
11
|
+
constructor(name: string, options: HistogramOptions<TLabels>) {
|
|
12
|
+
super(name, options);
|
|
13
|
+
this.buckets = options.buckets.sort((a, b) => a - b)
|
|
14
|
+
Object.freeze(this.buckets)
|
|
15
|
+
Object.freeze(this);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public create(): GGHistogram<TLabels> {
|
|
19
|
+
return new GGHistogram<TLabels>(this);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public observe(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
23
|
+
this.getMetric().observe(value, ...args);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public startTimer(...args: LabelsArgs<TLabels>): () => void {
|
|
27
|
+
return this.getMetric().startTimer(...args);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getValue(...args: LabelsArgs<TLabels>): HistogramData | undefined {
|
|
31
|
+
return this.getMetric().getValue(...args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public reset(): void {
|
|
35
|
+
this.getMetric().reset();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
-
import {GGLazyGauge} from "../metric/GGLazyGauge.js";
|
|
3
|
-
import {GGMetricOptionsBase} from "../GGMetric.js";
|
|
4
|
-
|
|
5
|
-
export interface LazyGaugeOptions extends GGMetricOptionsBase {
|
|
6
|
-
getValue: () => number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Key for a lazy gauge metric.
|
|
11
|
-
* The getValue function is called whenever the metric value is read.
|
|
12
|
-
*/
|
|
13
|
-
export class GGLazyGaugeKey extends GGMetricKey<{}, GGLazyGauge> {
|
|
14
|
-
|
|
15
|
-
public declare readonly options: LazyGaugeOptions;
|
|
16
|
-
|
|
17
|
-
constructor(name: string, options: LazyGaugeOptions) {
|
|
18
|
-
super(name, options);
|
|
19
|
-
Object.freeze(this);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public create(): GGLazyGauge {
|
|
23
|
-
return new GGLazyGauge(this);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Get the current value by calling the getValue function.
|
|
28
|
-
*/
|
|
29
|
-
public getValue(): number {
|
|
30
|
-
return this.getMetric().getValue();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public reset(): void {
|
|
34
|
-
// No-op for lazy gauge - nothing to reset
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
import {GGMetricKey} from "../GGMetricKey.js";
|
|
2
|
+
import {GGLazyGauge} from "../metric/GGLazyGauge.js";
|
|
3
|
+
import {GGMetricOptionsBase} from "../GGMetric.js";
|
|
4
|
+
|
|
5
|
+
export interface LazyGaugeOptions extends GGMetricOptionsBase {
|
|
6
|
+
getValue: () => number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Key for a lazy gauge metric.
|
|
11
|
+
* The getValue function is called whenever the metric value is read.
|
|
12
|
+
*/
|
|
13
|
+
export class GGLazyGaugeKey extends GGMetricKey<{}, GGLazyGauge> {
|
|
14
|
+
|
|
15
|
+
public declare readonly options: LazyGaugeOptions;
|
|
16
|
+
|
|
17
|
+
constructor(name: string, options: LazyGaugeOptions) {
|
|
18
|
+
super(name, options);
|
|
19
|
+
Object.freeze(this);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public create(): GGLazyGauge {
|
|
23
|
+
return new GGLazyGauge(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get the current value by calling the getValue function.
|
|
28
|
+
*/
|
|
29
|
+
public getValue(): number {
|
|
30
|
+
return this.getMetric().getValue();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public reset(): void {
|
|
34
|
+
// No-op for lazy gauge - nothing to reset
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/metric/GGCounter.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {GGMetric, GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
2
|
-
import type {GGCounterKey} from "../keys/GGCounterKey";
|
|
3
|
-
|
|
4
|
-
export class GGCounter<
|
|
5
|
-
TLabels extends GGMetricLabels = {}
|
|
6
|
-
> extends GGMetric<TLabels, number, GGCounterKey<TLabels>> {
|
|
7
|
-
|
|
8
|
-
protected getDefaultValue(): number {
|
|
9
|
-
return 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
13
|
-
const key = this.getKey(args[0] as TLabels);
|
|
14
|
-
const current = this.getByKey(key);
|
|
15
|
-
if (current !== undefined) {
|
|
16
|
-
this.setByKey(key, current + value);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
1
|
+
import {GGMetric, GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
2
|
+
import type {GGCounterKey} from "../keys/GGCounterKey";
|
|
3
|
+
|
|
4
|
+
export class GGCounter<
|
|
5
|
+
TLabels extends GGMetricLabels = {}
|
|
6
|
+
> extends GGMetric<TLabels, number, GGCounterKey<TLabels>> {
|
|
7
|
+
|
|
8
|
+
protected getDefaultValue(): number {
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
13
|
+
const key = this.getKey(args[0] as TLabels);
|
|
14
|
+
const current = this.getByKey(key);
|
|
15
|
+
if (current !== undefined) {
|
|
16
|
+
this.setByKey(key, current + value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/metric/GGGauge.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import {GGMetric, GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
2
|
-
import type {GGGaugeKey} from "../keys/GGGaugeKey";
|
|
3
|
-
|
|
4
|
-
export class GGGauge<
|
|
5
|
-
TLabels extends GGMetricLabels = {}
|
|
6
|
-
> extends GGMetric<TLabels, number, GGGaugeKey<TLabels>> {
|
|
7
|
-
|
|
8
|
-
protected getDefaultValue(): number {
|
|
9
|
-
return 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public set(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
13
|
-
const key = this.getKey(args[0] as TLabels);
|
|
14
|
-
const current = this.getByKey(key);
|
|
15
|
-
if (current === undefined) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
this.setByKey(key, value);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
22
|
-
const key = this.getKey(args[0] as TLabels);
|
|
23
|
-
const current = this.getByKey(key);
|
|
24
|
-
if (current === undefined) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
this.setByKey(key, current + value);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public dec(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
31
|
-
const key = this.getKey(args[0] as TLabels);
|
|
32
|
-
const current = this.getByKey(key);
|
|
33
|
-
if (current === undefined) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
this.setByKey(key, current - value);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
import {GGMetric, GGMetricLabels, LabelsArgs} from "../GGMetric.js";
|
|
2
|
+
import type {GGGaugeKey} from "../keys/GGGaugeKey";
|
|
3
|
+
|
|
4
|
+
export class GGGauge<
|
|
5
|
+
TLabels extends GGMetricLabels = {}
|
|
6
|
+
> extends GGMetric<TLabels, number, GGGaugeKey<TLabels>> {
|
|
7
|
+
|
|
8
|
+
protected getDefaultValue(): number {
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public set(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
13
|
+
const key = this.getKey(args[0] as TLabels);
|
|
14
|
+
const current = this.getByKey(key);
|
|
15
|
+
if (current === undefined) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.setByKey(key, value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public inc(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
22
|
+
const key = this.getKey(args[0] as TLabels);
|
|
23
|
+
const current = this.getByKey(key);
|
|
24
|
+
if (current === undefined) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.setByKey(key, current + value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public dec(value: number = 1, ...args: LabelsArgs<TLabels>): void {
|
|
31
|
+
const key = this.getKey(args[0] as TLabels);
|
|
32
|
+
const current = this.getByKey(key);
|
|
33
|
+
if (current === undefined) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
this.setByKey(key, current - value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import {GGMetric, GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
2
|
-
import type {GGHistogramKey} from "../keys/GGHistogramKey";
|
|
3
|
-
|
|
4
|
-
export type HistogramOptions<TLabels extends GGMetricLabels = {}> =
|
|
5
|
-
GGMetricOptions<TLabels> & { buckets: number[] };
|
|
6
|
-
|
|
7
|
-
export class GGHistogram<
|
|
8
|
-
TLabels extends GGMetricLabels = {}
|
|
9
|
-
> extends GGMetric<TLabels, HistogramData, GGHistogramKey<TLabels>> {
|
|
10
|
-
|
|
11
|
-
public getBuckets(): number[] {
|
|
12
|
-
return this.key.buckets;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
protected getDefaultValue(): HistogramData {
|
|
16
|
-
return {
|
|
17
|
-
count: 0,
|
|
18
|
-
sum: 0,
|
|
19
|
-
min: Infinity,
|
|
20
|
-
max: -Infinity,
|
|
21
|
-
values: this.key.buckets.map(() => 0)
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public observe(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
26
|
-
const key = this.getKey(args[0] as TLabels);
|
|
27
|
-
const data = this.getByKey(key);
|
|
28
|
-
if (data === undefined) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
data.count++;
|
|
32
|
-
data.sum += value;
|
|
33
|
-
if (value < data.min) {
|
|
34
|
-
data.min = value;
|
|
35
|
-
}
|
|
36
|
-
if (value > data.max) {
|
|
37
|
-
data.max = value;
|
|
38
|
-
}
|
|
39
|
-
for (let i = 0; i < this.key.buckets.length; i++) {
|
|
40
|
-
if (value <= this.key.buckets[i]) {
|
|
41
|
-
data.values[i]++;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public startTimer(...args: LabelsArgs<TLabels>): () => void {
|
|
47
|
-
const start = Date.now();
|
|
48
|
-
const labels = args[0] as TLabels;
|
|
49
|
-
return () => this.observe(Date.now() - start, ...([labels] as LabelsArgs<TLabels>));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface HistogramData {
|
|
54
|
-
count: number;
|
|
55
|
-
sum: number;
|
|
56
|
-
min: number;
|
|
57
|
-
max: number;
|
|
58
|
-
values: number[];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface SerializedHistogramData {
|
|
62
|
-
count: number;
|
|
63
|
-
sum: number;
|
|
64
|
-
min: number;
|
|
65
|
-
max: number;
|
|
66
|
-
buckets: number[];
|
|
67
|
-
values: number[];
|
|
68
|
-
}
|
|
1
|
+
import {GGMetric, GGMetricLabels, GGMetricOptions, LabelsArgs} from "../GGMetric.js";
|
|
2
|
+
import type {GGHistogramKey} from "../keys/GGHistogramKey";
|
|
3
|
+
|
|
4
|
+
export type HistogramOptions<TLabels extends GGMetricLabels = {}> =
|
|
5
|
+
GGMetricOptions<TLabels> & { buckets: number[] };
|
|
6
|
+
|
|
7
|
+
export class GGHistogram<
|
|
8
|
+
TLabels extends GGMetricLabels = {}
|
|
9
|
+
> extends GGMetric<TLabels, HistogramData, GGHistogramKey<TLabels>> {
|
|
10
|
+
|
|
11
|
+
public getBuckets(): number[] {
|
|
12
|
+
return this.key.buckets;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected getDefaultValue(): HistogramData {
|
|
16
|
+
return {
|
|
17
|
+
count: 0,
|
|
18
|
+
sum: 0,
|
|
19
|
+
min: Infinity,
|
|
20
|
+
max: -Infinity,
|
|
21
|
+
values: this.key.buckets.map(() => 0)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public observe(value: number, ...args: LabelsArgs<TLabels>): void {
|
|
26
|
+
const key = this.getKey(args[0] as TLabels);
|
|
27
|
+
const data = this.getByKey(key);
|
|
28
|
+
if (data === undefined) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
data.count++;
|
|
32
|
+
data.sum += value;
|
|
33
|
+
if (value < data.min) {
|
|
34
|
+
data.min = value;
|
|
35
|
+
}
|
|
36
|
+
if (value > data.max) {
|
|
37
|
+
data.max = value;
|
|
38
|
+
}
|
|
39
|
+
for (let i = 0; i < this.key.buckets.length; i++) {
|
|
40
|
+
if (value <= this.key.buckets[i]) {
|
|
41
|
+
data.values[i]++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public startTimer(...args: LabelsArgs<TLabels>): () => void {
|
|
47
|
+
const start = Date.now();
|
|
48
|
+
const labels = args[0] as TLabels;
|
|
49
|
+
return () => this.observe(Date.now() - start, ...([labels] as LabelsArgs<TLabels>));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface HistogramData {
|
|
54
|
+
count: number;
|
|
55
|
+
sum: number;
|
|
56
|
+
min: number;
|
|
57
|
+
max: number;
|
|
58
|
+
values: number[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface SerializedHistogramData {
|
|
62
|
+
count: number;
|
|
63
|
+
sum: number;
|
|
64
|
+
min: number;
|
|
65
|
+
max: number;
|
|
66
|
+
buckets: number[];
|
|
67
|
+
values: number[];
|
|
68
|
+
}
|