-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathwrapper.js
More file actions
87 lines (80 loc) · 2.85 KB
/
Copy pathwrapper.js
File metadata and controls
87 lines (80 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { default: ScubaClient } = require('scubaclient');
const { externalBackendHealthCheckInterval } = require('../../../constants');
const monitoring = require('../../utilities/monitoringHandler');
class ScubaClientImpl extends ScubaClient {
constructor(config) {
super(config.scuba);
this.enabled = false;
this.maxStaleness = config.quota.maxStaleness;
this._healthCheckTimer = null;
this._log = null;
if (config.scuba) {
this.enabled = true;
} else {
this.enabled = false;
}
}
setup(log) {
this._log = log;
if (this.enabled) {
this.periodicHealthCheck();
}
}
async _healthCheck() {
try {
const data = await this.healthCheck();
if (data?.date) {
const date = new Date(data.date);
if (Date.now() - date.getTime() > this.maxStaleness) {
throw new Error('Data is stale, disabling quotas');
}
}
if (!this.enabled) {
this._log.info('Scuba health check passed, enabling quotas');
}
monitoring.utilizationServiceAvailable.set(1);
this.enabled = true;
} catch (err) {
if (this.enabled) {
this._log.warn('Scuba health check failed, disabling quotas', {
err: err.name,
description: err.message,
});
}
monitoring.utilizationServiceAvailable.set(0);
this.enabled = false;
}
}
periodicHealthCheck() {
if (this._healthCheckTimer) {
clearInterval(this._healthCheckTimer);
}
this._healthCheck();
this._healthCheckTimer = setInterval(
() => this._healthCheck(),
Number(process.env.SCUBA_HEALTHCHECK_FREQUENCY) || externalBackendHealthCheckInterval,
);
}
async getUtilizationMetrics(metricsClass, resourceName, options, body, log) {
const requestStartTime = process.hrtime.bigint();
const reqUids = log?.getSerializedUids?.();
const mergedOptions = reqUids
? { ...options, headers: { ...options?.headers, 'X-Scal-Request-Uids': reqUids } }
: options;
let code = 200;
try {
return await super.getLatestMetrics(metricsClass, resourceName, mergedOptions, body);
} catch (err) {
code = err.response?.status || err.code || 500;
throw err;
} finally {
const responseTimeInNs = Number(process.hrtime.bigint() - requestStartTime);
monitoring.utilizationMetricsRetrievalDuration
.labels({ code, class: metricsClass })
.observe(responseTimeInNs / 1e9);
}
}
}
module.exports = {
ScubaClientImpl,
};