@bymax-one/nest-storage 1.1.18 → 1.1.20
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/CHANGELOG.md +145 -1
- package/README.md +184 -16
- package/dist/observability/index.cjs +151 -0
- package/dist/observability/index.d.cts +72 -0
- package/dist/observability/index.d.ts +72 -0
- package/dist/observability/index.mjs +145 -0
- package/dist/server/index.cjs +831 -444
- package/dist/server/index.d.cts +151 -12
- package/dist/server/index.d.ts +151 -12
- package/dist/server/index.mjs +831 -447
- package/package.json +32 -5
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var common = require('@nestjs/common');
|
|
4
|
+
var promClient = require('prom-client');
|
|
5
|
+
var nestStorage = require('@bymax-one/nest-storage');
|
|
6
|
+
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __typeError = (msg) => {
|
|
10
|
+
throw TypeError(msg);
|
|
11
|
+
};
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
14
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
15
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
16
|
+
if (decorator = decorators[i])
|
|
17
|
+
result = (decorator(result)) || result;
|
|
18
|
+
return result;
|
|
19
|
+
};
|
|
20
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
21
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
22
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
23
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
24
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
25
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
26
|
+
var METRICS_CONTRIBUTOR_METADATA = "bymax-one:metrics-contributor";
|
|
27
|
+
exports.StorageMetrics = class StorageMetrics {
|
|
28
|
+
constructor(tally) {
|
|
29
|
+
this.tally = tally;
|
|
30
|
+
}
|
|
31
|
+
tally;
|
|
32
|
+
/** Registers the collectors; called once at bootstrap by the metrics module. */
|
|
33
|
+
registerMetrics(registry) {
|
|
34
|
+
const { tally } = this;
|
|
35
|
+
new promClient.Counter({
|
|
36
|
+
name: "bymax_storage_operations_total",
|
|
37
|
+
help: "Storage operations by operation and outcome (success, not_found, aborted, error).",
|
|
38
|
+
labelNames: ["operation", "outcome"],
|
|
39
|
+
registers: [registry],
|
|
40
|
+
collect() {
|
|
41
|
+
this.reset();
|
|
42
|
+
const { operations } = tally.snapshot();
|
|
43
|
+
for (const operation of nestStorage.STORAGE_OPERATIONS) {
|
|
44
|
+
for (const outcome of nestStorage.STORAGE_OUTCOMES) {
|
|
45
|
+
this.inc({ operation, outcome }, operations[operation][outcome]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
const duration = new promClient.Histogram({
|
|
51
|
+
name: "bymax_storage_operation_duration_seconds",
|
|
52
|
+
help: "Time a storage operation took, by operation: to the response for a download, to the end otherwise.",
|
|
53
|
+
labelNames: ["operation"],
|
|
54
|
+
registers: [registry]
|
|
55
|
+
});
|
|
56
|
+
for (const operation of nestStorage.STORAGE_OPERATIONS) duration.zero({ operation });
|
|
57
|
+
tally.onDuration((operation, seconds) => {
|
|
58
|
+
duration.observe({ operation }, seconds);
|
|
59
|
+
});
|
|
60
|
+
new promClient.Counter({
|
|
61
|
+
name: "bymax_storage_bytes_uploaded_total",
|
|
62
|
+
help: "Bytes of objects uploaded, counted when the upload succeeded and not for a result the idempotency cache returned.",
|
|
63
|
+
registers: [registry],
|
|
64
|
+
collect() {
|
|
65
|
+
this.reset();
|
|
66
|
+
this.inc(tally.snapshot().bytesUploaded);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
new promClient.Counter({
|
|
70
|
+
name: "bymax_storage_scans_total",
|
|
71
|
+
help: "File scans by verdict (clean, infected, unknown, timeout, error).",
|
|
72
|
+
labelNames: ["verdict"],
|
|
73
|
+
registers: [registry],
|
|
74
|
+
collect() {
|
|
75
|
+
this.reset();
|
|
76
|
+
const { scans } = tally.snapshot();
|
|
77
|
+
for (const verdict of nestStorage.SCAN_VERDICTS) this.inc({ verdict }, scans[verdict]);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
exports.StorageMetrics = __decorateClass([
|
|
83
|
+
common.SetMetadata(METRICS_CONTRIBUTOR_METADATA, true),
|
|
84
|
+
common.Injectable(),
|
|
85
|
+
__decorateParam(0, common.Inject(nestStorage.StorageMetricsTally))
|
|
86
|
+
], exports.StorageMetrics);
|
|
87
|
+
var HEALTH_INDICATOR_METADATA = "bymax-one:health-indicator";
|
|
88
|
+
var STORAGE_HEALTH_INDICATOR_NAME = "storage";
|
|
89
|
+
var STORAGE_HEALTH_TIMEOUT_MS = 2e3;
|
|
90
|
+
var STORAGE_HEALTH_CACHE_MS = 5e3;
|
|
91
|
+
var _answer, _running;
|
|
92
|
+
exports.StorageHealthIndicator = class StorageHealthIndicator {
|
|
93
|
+
constructor(storage) {
|
|
94
|
+
this.storage = storage;
|
|
95
|
+
__publicField(this, "name", STORAGE_HEALTH_INDICATOR_NAME);
|
|
96
|
+
__privateAdd(this, _answer);
|
|
97
|
+
__privateAdd(this, _running);
|
|
98
|
+
}
|
|
99
|
+
storage;
|
|
100
|
+
/** Whether the bucket answers in time; the aggregator's signal ends this caller's wait. */
|
|
101
|
+
async check(signal) {
|
|
102
|
+
const kept = __privateGet(this, _answer);
|
|
103
|
+
if (kept !== void 0 && Date.now() - kept.at < STORAGE_HEALTH_CACHE_MS) {
|
|
104
|
+
return { status: kept.status };
|
|
105
|
+
}
|
|
106
|
+
const status = await this.waitFor(__privateGet(this, _running) ?? this.probe(), signal);
|
|
107
|
+
return { status };
|
|
108
|
+
}
|
|
109
|
+
/** The shared probe: one `HeadBucket` under its own deadline, remembered when it ends. */
|
|
110
|
+
probe() {
|
|
111
|
+
const running = this.storage.checkBucket({ abortSignal: AbortSignal.timeout(STORAGE_HEALTH_TIMEOUT_MS) }).then(
|
|
112
|
+
() => "up",
|
|
113
|
+
() => "down"
|
|
114
|
+
).then((status) => {
|
|
115
|
+
__privateSet(this, _answer, { at: Date.now(), status });
|
|
116
|
+
__privateSet(this, _running, void 0);
|
|
117
|
+
return status;
|
|
118
|
+
});
|
|
119
|
+
__privateSet(this, _running, running);
|
|
120
|
+
return running;
|
|
121
|
+
}
|
|
122
|
+
/** `probe`'s answer, or `down` as soon as `signal` aborts. */
|
|
123
|
+
waitFor(probe, signal) {
|
|
124
|
+
if (signal === void 0) return probe;
|
|
125
|
+
if (signal.aborted) return Promise.resolve("down");
|
|
126
|
+
return new Promise((resolve) => {
|
|
127
|
+
const stop = () => {
|
|
128
|
+
signal.removeEventListener("abort", stop);
|
|
129
|
+
resolve("down");
|
|
130
|
+
};
|
|
131
|
+
signal.addEventListener("abort", stop);
|
|
132
|
+
void probe.then((status) => {
|
|
133
|
+
signal.removeEventListener("abort", stop);
|
|
134
|
+
resolve(status);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
_answer = new WeakMap();
|
|
140
|
+
_running = new WeakMap();
|
|
141
|
+
exports.StorageHealthIndicator = __decorateClass([
|
|
142
|
+
common.SetMetadata(HEALTH_INDICATOR_METADATA, true),
|
|
143
|
+
common.Injectable(),
|
|
144
|
+
__decorateParam(0, common.Inject(nestStorage.StorageService))
|
|
145
|
+
], exports.StorageHealthIndicator);
|
|
146
|
+
|
|
147
|
+
exports.HEALTH_INDICATOR_METADATA = HEALTH_INDICATOR_METADATA;
|
|
148
|
+
exports.METRICS_CONTRIBUTOR_METADATA = METRICS_CONTRIBUTOR_METADATA;
|
|
149
|
+
exports.STORAGE_HEALTH_CACHE_MS = STORAGE_HEALTH_CACHE_MS;
|
|
150
|
+
exports.STORAGE_HEALTH_INDICATOR_NAME = STORAGE_HEALTH_INDICATOR_NAME;
|
|
151
|
+
exports.STORAGE_HEALTH_TIMEOUT_MS = STORAGE_HEALTH_TIMEOUT_MS;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Registry } from 'prom-client';
|
|
2
|
+
import { StorageMetricsTally, StorageService } from '@bymax-one/nest-storage';
|
|
3
|
+
|
|
4
|
+
/** The registry the application's scrape endpoint serves. */
|
|
5
|
+
type MetricsRegistry = Registry;
|
|
6
|
+
/** What a component that publishes metrics implements: it receives the registry at bootstrap. */
|
|
7
|
+
interface MetricsContributor {
|
|
8
|
+
registerMetrics(registry: MetricsRegistry): void;
|
|
9
|
+
}
|
|
10
|
+
/** The metadata key a metrics module discovers contributors by. */
|
|
11
|
+
declare const METRICS_CONTRIBUTOR_METADATA = "bymax-one:metrics-contributor";
|
|
12
|
+
/**
|
|
13
|
+
* Registers the storage collectors on the registry the scrape endpoint serves.
|
|
14
|
+
*
|
|
15
|
+
* The counters are read from the tally on each scrape, after a reset, instead of being written as events
|
|
16
|
+
* arrive: the tally starts counting before this class runs, and a counter fed by events would drop that window.
|
|
17
|
+
* The duration histogram cannot be rebuilt from counts, so it is fed as operations end, from the moment this
|
|
18
|
+
* class registers it. Labels are the closed sets the tally is keyed by; no key, bucket, prefix, tenant or message
|
|
19
|
+
* ever becomes one.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // providers: [StorageMetrics] (BymaxCoreModule discovers it and calls registerMetrics once at bootstrap)
|
|
23
|
+
*/
|
|
24
|
+
declare class StorageMetrics implements MetricsContributor {
|
|
25
|
+
private readonly tally;
|
|
26
|
+
constructor(tally: StorageMetricsTally);
|
|
27
|
+
/** Registers the collectors; called once at bootstrap by the metrics module. */
|
|
28
|
+
registerMetrics(registry: MetricsRegistry): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The outcome of one readiness check. */
|
|
32
|
+
interface HealthIndicatorResult {
|
|
33
|
+
status: 'up' | 'down';
|
|
34
|
+
details?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
/** What a readiness indicator implements. */
|
|
37
|
+
interface HealthIndicator {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
check(signal?: AbortSignal): Promise<HealthIndicatorResult>;
|
|
40
|
+
}
|
|
41
|
+
/** The metadata key a health module discovers indicators by. */
|
|
42
|
+
declare const HEALTH_INDICATOR_METADATA = "bymax-one:health-indicator";
|
|
43
|
+
/** The name the indicator reports under. */
|
|
44
|
+
declare const STORAGE_HEALTH_INDICATOR_NAME = "storage";
|
|
45
|
+
/** How long the probe may take before it is `down`. */
|
|
46
|
+
declare const STORAGE_HEALTH_TIMEOUT_MS = 2000;
|
|
47
|
+
/** How long an answer is kept before the bucket is asked again, so a readiness poll cannot become a request flood. */
|
|
48
|
+
declare const STORAGE_HEALTH_CACHE_MS = 5000;
|
|
49
|
+
/**
|
|
50
|
+
* The readiness check of the bucket.
|
|
51
|
+
*
|
|
52
|
+
* It asks `StorageService.checkBucket()` for one `HeadBucket`: nothing is written and no object is read. A bucket
|
|
53
|
+
* that cannot be reached, a refusal, or an answer that does not come within {@link STORAGE_HEALTH_TIMEOUT_MS} is
|
|
54
|
+
* `down`. The answer, up or down, is kept for {@link STORAGE_HEALTH_CACHE_MS}, and checks that arrive while one is
|
|
55
|
+
* running share it. No detail is reported, so nothing about the store (its name, its region or its endpoint)
|
|
56
|
+
* leaves with it. The aggregator's signal ends the wait of that caller only: the shared probe runs under its own
|
|
57
|
+
* deadline, so one caller giving up cannot fail the others.
|
|
58
|
+
*/
|
|
59
|
+
declare class StorageHealthIndicator implements HealthIndicator {
|
|
60
|
+
#private;
|
|
61
|
+
private readonly storage;
|
|
62
|
+
readonly name = "storage";
|
|
63
|
+
constructor(storage: StorageService);
|
|
64
|
+
/** Whether the bucket answers in time; the aggregator's signal ends this caller's wait. */
|
|
65
|
+
check(signal?: AbortSignal): Promise<HealthIndicatorResult>;
|
|
66
|
+
/** The shared probe: one `HeadBucket` under its own deadline, remembered when it ends. */
|
|
67
|
+
private probe;
|
|
68
|
+
/** `probe`'s answer, or `down` as soon as `signal` aborts. */
|
|
69
|
+
private waitFor;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { HEALTH_INDICATOR_METADATA, METRICS_CONTRIBUTOR_METADATA, STORAGE_HEALTH_CACHE_MS, STORAGE_HEALTH_INDICATOR_NAME, STORAGE_HEALTH_TIMEOUT_MS, StorageHealthIndicator, StorageMetrics };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Registry } from 'prom-client';
|
|
2
|
+
import { StorageMetricsTally, StorageService } from '@bymax-one/nest-storage';
|
|
3
|
+
|
|
4
|
+
/** The registry the application's scrape endpoint serves. */
|
|
5
|
+
type MetricsRegistry = Registry;
|
|
6
|
+
/** What a component that publishes metrics implements: it receives the registry at bootstrap. */
|
|
7
|
+
interface MetricsContributor {
|
|
8
|
+
registerMetrics(registry: MetricsRegistry): void;
|
|
9
|
+
}
|
|
10
|
+
/** The metadata key a metrics module discovers contributors by. */
|
|
11
|
+
declare const METRICS_CONTRIBUTOR_METADATA = "bymax-one:metrics-contributor";
|
|
12
|
+
/**
|
|
13
|
+
* Registers the storage collectors on the registry the scrape endpoint serves.
|
|
14
|
+
*
|
|
15
|
+
* The counters are read from the tally on each scrape, after a reset, instead of being written as events
|
|
16
|
+
* arrive: the tally starts counting before this class runs, and a counter fed by events would drop that window.
|
|
17
|
+
* The duration histogram cannot be rebuilt from counts, so it is fed as operations end, from the moment this
|
|
18
|
+
* class registers it. Labels are the closed sets the tally is keyed by; no key, bucket, prefix, tenant or message
|
|
19
|
+
* ever becomes one.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // providers: [StorageMetrics] (BymaxCoreModule discovers it and calls registerMetrics once at bootstrap)
|
|
23
|
+
*/
|
|
24
|
+
declare class StorageMetrics implements MetricsContributor {
|
|
25
|
+
private readonly tally;
|
|
26
|
+
constructor(tally: StorageMetricsTally);
|
|
27
|
+
/** Registers the collectors; called once at bootstrap by the metrics module. */
|
|
28
|
+
registerMetrics(registry: MetricsRegistry): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The outcome of one readiness check. */
|
|
32
|
+
interface HealthIndicatorResult {
|
|
33
|
+
status: 'up' | 'down';
|
|
34
|
+
details?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
/** What a readiness indicator implements. */
|
|
37
|
+
interface HealthIndicator {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
check(signal?: AbortSignal): Promise<HealthIndicatorResult>;
|
|
40
|
+
}
|
|
41
|
+
/** The metadata key a health module discovers indicators by. */
|
|
42
|
+
declare const HEALTH_INDICATOR_METADATA = "bymax-one:health-indicator";
|
|
43
|
+
/** The name the indicator reports under. */
|
|
44
|
+
declare const STORAGE_HEALTH_INDICATOR_NAME = "storage";
|
|
45
|
+
/** How long the probe may take before it is `down`. */
|
|
46
|
+
declare const STORAGE_HEALTH_TIMEOUT_MS = 2000;
|
|
47
|
+
/** How long an answer is kept before the bucket is asked again, so a readiness poll cannot become a request flood. */
|
|
48
|
+
declare const STORAGE_HEALTH_CACHE_MS = 5000;
|
|
49
|
+
/**
|
|
50
|
+
* The readiness check of the bucket.
|
|
51
|
+
*
|
|
52
|
+
* It asks `StorageService.checkBucket()` for one `HeadBucket`: nothing is written and no object is read. A bucket
|
|
53
|
+
* that cannot be reached, a refusal, or an answer that does not come within {@link STORAGE_HEALTH_TIMEOUT_MS} is
|
|
54
|
+
* `down`. The answer, up or down, is kept for {@link STORAGE_HEALTH_CACHE_MS}, and checks that arrive while one is
|
|
55
|
+
* running share it. No detail is reported, so nothing about the store (its name, its region or its endpoint)
|
|
56
|
+
* leaves with it. The aggregator's signal ends the wait of that caller only: the shared probe runs under its own
|
|
57
|
+
* deadline, so one caller giving up cannot fail the others.
|
|
58
|
+
*/
|
|
59
|
+
declare class StorageHealthIndicator implements HealthIndicator {
|
|
60
|
+
#private;
|
|
61
|
+
private readonly storage;
|
|
62
|
+
readonly name = "storage";
|
|
63
|
+
constructor(storage: StorageService);
|
|
64
|
+
/** Whether the bucket answers in time; the aggregator's signal ends this caller's wait. */
|
|
65
|
+
check(signal?: AbortSignal): Promise<HealthIndicatorResult>;
|
|
66
|
+
/** The shared probe: one `HeadBucket` under its own deadline, remembered when it ends. */
|
|
67
|
+
private probe;
|
|
68
|
+
/** `probe`'s answer, or `down` as soon as `signal` aborts. */
|
|
69
|
+
private waitFor;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { HEALTH_INDICATOR_METADATA, METRICS_CONTRIBUTOR_METADATA, STORAGE_HEALTH_CACHE_MS, STORAGE_HEALTH_INDICATOR_NAME, STORAGE_HEALTH_TIMEOUT_MS, StorageHealthIndicator, StorageMetrics };
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { SetMetadata, Injectable, Inject } from '@nestjs/common';
|
|
2
|
+
import { Counter, Histogram } from 'prom-client';
|
|
3
|
+
import { StorageMetricsTally, StorageService, STORAGE_OPERATIONS, STORAGE_OUTCOMES, SCAN_VERDICTS } from '@bymax-one/nest-storage';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __typeError = (msg) => {
|
|
8
|
+
throw TypeError(msg);
|
|
9
|
+
};
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
12
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
13
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
14
|
+
if (decorator = decorators[i])
|
|
15
|
+
result = (decorator(result)) || result;
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
19
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
20
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
21
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
22
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
23
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
24
|
+
var METRICS_CONTRIBUTOR_METADATA = "bymax-one:metrics-contributor";
|
|
25
|
+
var StorageMetrics = class {
|
|
26
|
+
constructor(tally) {
|
|
27
|
+
this.tally = tally;
|
|
28
|
+
}
|
|
29
|
+
tally;
|
|
30
|
+
/** Registers the collectors; called once at bootstrap by the metrics module. */
|
|
31
|
+
registerMetrics(registry) {
|
|
32
|
+
const { tally } = this;
|
|
33
|
+
new Counter({
|
|
34
|
+
name: "bymax_storage_operations_total",
|
|
35
|
+
help: "Storage operations by operation and outcome (success, not_found, aborted, error).",
|
|
36
|
+
labelNames: ["operation", "outcome"],
|
|
37
|
+
registers: [registry],
|
|
38
|
+
collect() {
|
|
39
|
+
this.reset();
|
|
40
|
+
const { operations } = tally.snapshot();
|
|
41
|
+
for (const operation of STORAGE_OPERATIONS) {
|
|
42
|
+
for (const outcome of STORAGE_OUTCOMES) {
|
|
43
|
+
this.inc({ operation, outcome }, operations[operation][outcome]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const duration = new Histogram({
|
|
49
|
+
name: "bymax_storage_operation_duration_seconds",
|
|
50
|
+
help: "Time a storage operation took, by operation: to the response for a download, to the end otherwise.",
|
|
51
|
+
labelNames: ["operation"],
|
|
52
|
+
registers: [registry]
|
|
53
|
+
});
|
|
54
|
+
for (const operation of STORAGE_OPERATIONS) duration.zero({ operation });
|
|
55
|
+
tally.onDuration((operation, seconds) => {
|
|
56
|
+
duration.observe({ operation }, seconds);
|
|
57
|
+
});
|
|
58
|
+
new Counter({
|
|
59
|
+
name: "bymax_storage_bytes_uploaded_total",
|
|
60
|
+
help: "Bytes of objects uploaded, counted when the upload succeeded and not for a result the idempotency cache returned.",
|
|
61
|
+
registers: [registry],
|
|
62
|
+
collect() {
|
|
63
|
+
this.reset();
|
|
64
|
+
this.inc(tally.snapshot().bytesUploaded);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
new Counter({
|
|
68
|
+
name: "bymax_storage_scans_total",
|
|
69
|
+
help: "File scans by verdict (clean, infected, unknown, timeout, error).",
|
|
70
|
+
labelNames: ["verdict"],
|
|
71
|
+
registers: [registry],
|
|
72
|
+
collect() {
|
|
73
|
+
this.reset();
|
|
74
|
+
const { scans } = tally.snapshot();
|
|
75
|
+
for (const verdict of SCAN_VERDICTS) this.inc({ verdict }, scans[verdict]);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
StorageMetrics = __decorateClass([
|
|
81
|
+
SetMetadata(METRICS_CONTRIBUTOR_METADATA, true),
|
|
82
|
+
Injectable(),
|
|
83
|
+
__decorateParam(0, Inject(StorageMetricsTally))
|
|
84
|
+
], StorageMetrics);
|
|
85
|
+
var HEALTH_INDICATOR_METADATA = "bymax-one:health-indicator";
|
|
86
|
+
var STORAGE_HEALTH_INDICATOR_NAME = "storage";
|
|
87
|
+
var STORAGE_HEALTH_TIMEOUT_MS = 2e3;
|
|
88
|
+
var STORAGE_HEALTH_CACHE_MS = 5e3;
|
|
89
|
+
var _answer, _running;
|
|
90
|
+
var StorageHealthIndicator = class {
|
|
91
|
+
constructor(storage) {
|
|
92
|
+
this.storage = storage;
|
|
93
|
+
__publicField(this, "name", STORAGE_HEALTH_INDICATOR_NAME);
|
|
94
|
+
__privateAdd(this, _answer);
|
|
95
|
+
__privateAdd(this, _running);
|
|
96
|
+
}
|
|
97
|
+
storage;
|
|
98
|
+
/** Whether the bucket answers in time; the aggregator's signal ends this caller's wait. */
|
|
99
|
+
async check(signal) {
|
|
100
|
+
const kept = __privateGet(this, _answer);
|
|
101
|
+
if (kept !== void 0 && Date.now() - kept.at < STORAGE_HEALTH_CACHE_MS) {
|
|
102
|
+
return { status: kept.status };
|
|
103
|
+
}
|
|
104
|
+
const status = await this.waitFor(__privateGet(this, _running) ?? this.probe(), signal);
|
|
105
|
+
return { status };
|
|
106
|
+
}
|
|
107
|
+
/** The shared probe: one `HeadBucket` under its own deadline, remembered when it ends. */
|
|
108
|
+
probe() {
|
|
109
|
+
const running = this.storage.checkBucket({ abortSignal: AbortSignal.timeout(STORAGE_HEALTH_TIMEOUT_MS) }).then(
|
|
110
|
+
() => "up",
|
|
111
|
+
() => "down"
|
|
112
|
+
).then((status) => {
|
|
113
|
+
__privateSet(this, _answer, { at: Date.now(), status });
|
|
114
|
+
__privateSet(this, _running, void 0);
|
|
115
|
+
return status;
|
|
116
|
+
});
|
|
117
|
+
__privateSet(this, _running, running);
|
|
118
|
+
return running;
|
|
119
|
+
}
|
|
120
|
+
/** `probe`'s answer, or `down` as soon as `signal` aborts. */
|
|
121
|
+
waitFor(probe, signal) {
|
|
122
|
+
if (signal === void 0) return probe;
|
|
123
|
+
if (signal.aborted) return Promise.resolve("down");
|
|
124
|
+
return new Promise((resolve) => {
|
|
125
|
+
const stop = () => {
|
|
126
|
+
signal.removeEventListener("abort", stop);
|
|
127
|
+
resolve("down");
|
|
128
|
+
};
|
|
129
|
+
signal.addEventListener("abort", stop);
|
|
130
|
+
void probe.then((status) => {
|
|
131
|
+
signal.removeEventListener("abort", stop);
|
|
132
|
+
resolve(status);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
_answer = new WeakMap();
|
|
138
|
+
_running = new WeakMap();
|
|
139
|
+
StorageHealthIndicator = __decorateClass([
|
|
140
|
+
SetMetadata(HEALTH_INDICATOR_METADATA, true),
|
|
141
|
+
Injectable(),
|
|
142
|
+
__decorateParam(0, Inject(StorageService))
|
|
143
|
+
], StorageHealthIndicator);
|
|
144
|
+
|
|
145
|
+
export { HEALTH_INDICATOR_METADATA, METRICS_CONTRIBUTOR_METADATA, STORAGE_HEALTH_CACHE_MS, STORAGE_HEALTH_INDICATOR_NAME, STORAGE_HEALTH_TIMEOUT_MS, StorageHealthIndicator, StorageMetrics };
|