@lokalise/healthcheck-utils 1.1.0
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.md +13 -0
- package/README.md +3 -0
- package/dist/HealthcheckResultsStore.d.ts +23 -0
- package/dist/HealthcheckResultsStore.js +55 -0
- package/dist/healthchecks.d.ts +22 -0
- package/dist/healthchecks.js +52 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/package.json +48 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2024 Lokalise, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type HealthcheckEntry = {
|
|
2
|
+
isSuccessful?: boolean;
|
|
3
|
+
errorMessage?: string;
|
|
4
|
+
checkTimestamp: Date;
|
|
5
|
+
latency?: number;
|
|
6
|
+
};
|
|
7
|
+
export type HealthcheckResultsStoreParams = {
|
|
8
|
+
healthcheckNumber: number;
|
|
9
|
+
stalenessThresholdInMsecs?: number;
|
|
10
|
+
healthCheckResultTtlInMsecs?: number;
|
|
11
|
+
};
|
|
12
|
+
export declare class HealthcheckResultsStore<SupportedHealthchecks extends string> {
|
|
13
|
+
private readonly stalenessThresholdInMsecs;
|
|
14
|
+
private readonly store;
|
|
15
|
+
constructor(params: HealthcheckResultsStoreParams);
|
|
16
|
+
set(healthcheck: SupportedHealthchecks, result: HealthcheckEntry): void;
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if check is passing, false if not
|
|
19
|
+
*/
|
|
20
|
+
getHealthcheckResult(healthcheck: SupportedHealthchecks): boolean;
|
|
21
|
+
getHealthcheckLatency(healthcheck: SupportedHealthchecks): number | undefined;
|
|
22
|
+
resetHealthcheckStores(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HealthcheckResultsStore = void 0;
|
|
4
|
+
const toad_cache_1 = require("toad-cache");
|
|
5
|
+
const DEFAULT_STALENESS_THRESHOLD_IN_MSECS = 30000;
|
|
6
|
+
const DEFAULT_HEALTHCHECK_TTL_IN_MSECS = 15000;
|
|
7
|
+
class HealthcheckResultsStore {
|
|
8
|
+
stalenessThresholdInMsecs;
|
|
9
|
+
store;
|
|
10
|
+
constructor(params) {
|
|
11
|
+
this.stalenessThresholdInMsecs =
|
|
12
|
+
params.stalenessThresholdInMsecs ?? DEFAULT_STALENESS_THRESHOLD_IN_MSECS;
|
|
13
|
+
this.store = new toad_cache_1.FifoMap(params.healthcheckNumber, params.healthCheckResultTtlInMsecs ?? DEFAULT_HEALTHCHECK_TTL_IN_MSECS);
|
|
14
|
+
}
|
|
15
|
+
set(healthcheck, result) {
|
|
16
|
+
this.store.set(healthcheck, result);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns true if check is passing, false if not
|
|
20
|
+
*/
|
|
21
|
+
getHealthcheckResult(healthcheck) {
|
|
22
|
+
const healthcheckEntry = this.store.get(healthcheck);
|
|
23
|
+
// If we don't have any results yet, we assume service is healthy
|
|
24
|
+
if (!healthcheckEntry) {
|
|
25
|
+
const emptyEntry = {
|
|
26
|
+
checkTimestamp: new Date(),
|
|
27
|
+
};
|
|
28
|
+
this.store.set(healthcheck, emptyEntry);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (healthcheckEntry.isSuccessful) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (healthcheckEntry.isSuccessful === false) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// If we still don't have healthcheck results, check how old the undefined state is
|
|
38
|
+
// If it is very stale, assume check is broken and report unhealthy service
|
|
39
|
+
return isLessThanMSecsAgo(healthcheckEntry.checkTimestamp, this.stalenessThresholdInMsecs);
|
|
40
|
+
}
|
|
41
|
+
getHealthcheckLatency(healthcheck) {
|
|
42
|
+
const healthcheckEntry = this.store.get(healthcheck);
|
|
43
|
+
if (!healthcheckEntry) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return healthcheckEntry.latency;
|
|
47
|
+
}
|
|
48
|
+
resetHealthcheckStores() {
|
|
49
|
+
this.store.clear();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.HealthcheckResultsStore = HealthcheckResultsStore;
|
|
53
|
+
function isLessThanMSecsAgo(givenDate, deltaInMsecs) {
|
|
54
|
+
return Date.now() - givenDate.getTime() < deltaInMsecs;
|
|
55
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Either } from '@lokalise/node-core';
|
|
2
|
+
import type { HealthcheckResultsStore } from './HealthcheckResultsStore';
|
|
3
|
+
export type Healthcheck = {
|
|
4
|
+
areMetricsEnabled: boolean;
|
|
5
|
+
instantiateMetrics: () => void;
|
|
6
|
+
execute: () => Promise<void>;
|
|
7
|
+
check: () => Promise<Either<Error, number>>;
|
|
8
|
+
storeResult: (result: Either<Error, number>) => void;
|
|
9
|
+
};
|
|
10
|
+
export type HealthcheckDependencies<SupportedHealthchecks extends string> = {
|
|
11
|
+
healthcheckStore: HealthcheckResultsStore<SupportedHealthchecks>;
|
|
12
|
+
};
|
|
13
|
+
export declare abstract class AbstractHealthcheck<SupportedHealthchecks extends string> implements Healthcheck {
|
|
14
|
+
readonly areMetricsEnabled: boolean;
|
|
15
|
+
private readonly store;
|
|
16
|
+
abstract check(): Promise<Either<Error, number>>;
|
|
17
|
+
abstract getId(): SupportedHealthchecks;
|
|
18
|
+
constructor(dependencies: HealthcheckDependencies<SupportedHealthchecks>, areMetricsEnabled: boolean);
|
|
19
|
+
instantiateMetrics(): void;
|
|
20
|
+
execute(): Promise<void>;
|
|
21
|
+
storeResult(result: Either<Error, number>): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractHealthcheck = void 0;
|
|
4
|
+
const prom_client_1 = require("prom-client");
|
|
5
|
+
class AbstractHealthcheck {
|
|
6
|
+
areMetricsEnabled;
|
|
7
|
+
store;
|
|
8
|
+
constructor(dependencies, areMetricsEnabled) {
|
|
9
|
+
this.areMetricsEnabled = areMetricsEnabled;
|
|
10
|
+
this.store = dependencies.healthcheckStore;
|
|
11
|
+
if (areMetricsEnabled) {
|
|
12
|
+
this.instantiateMetrics();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
instantiateMetrics() {
|
|
16
|
+
if (!this.areMetricsEnabled) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const store = this.store;
|
|
20
|
+
const id = this.getId();
|
|
21
|
+
new prom_client_1.Gauge({
|
|
22
|
+
name: `${id}_availability`,
|
|
23
|
+
help: `Whether ${id} was available at the time`,
|
|
24
|
+
collect() {
|
|
25
|
+
const checkResult = store.getHealthcheckResult(id);
|
|
26
|
+
this.set(checkResult !== false ? 1 : 0);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
new prom_client_1.Gauge({
|
|
30
|
+
name: `${id}_latency_msecs`,
|
|
31
|
+
help: `How long the healthcheck for ${id} took`,
|
|
32
|
+
collect() {
|
|
33
|
+
const checkLength = store.getHealthcheckLatency(id);
|
|
34
|
+
this.set(checkLength ?? 0);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async execute() {
|
|
39
|
+
const result = await this.check();
|
|
40
|
+
this.storeResult(result);
|
|
41
|
+
}
|
|
42
|
+
storeResult(result) {
|
|
43
|
+
const id = this.getId();
|
|
44
|
+
this.store.set(id, {
|
|
45
|
+
latency: result.error ? undefined : result.result,
|
|
46
|
+
checkTimestamp: new Date(),
|
|
47
|
+
isSuccessful: !result.error,
|
|
48
|
+
errorMessage: result.error ? result.error.message : undefined,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.AbstractHealthcheck = AbstractHealthcheck;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lokalise/healthcheck-utils",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist/**",
|
|
7
|
+
"LICENSE.md",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"homepage": "https://github.com/lokalise/shared-ts-libs",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git://github.com/lokalise/shared-ts-libs.git"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rimraf dist && tsc",
|
|
20
|
+
"clean": "rimraf dist",
|
|
21
|
+
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
|
|
22
|
+
"lint:fix": "biome check --write",
|
|
23
|
+
"test:ci": "vitest run --coverage",
|
|
24
|
+
"prepublishOnly": "npm run build",
|
|
25
|
+
"package-version": "echo $npm_package_version"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@lokalise/node-core": "^11.2.0",
|
|
29
|
+
"toad-cache": "^3.7.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"prom-client": "^15.1.3",
|
|
33
|
+
"toad-scheduler": "^3.0.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@biomejs/biome": "^1.8.3",
|
|
37
|
+
"@lokalise/biome-config": "^1.2.2",
|
|
38
|
+
"@lokalise/node-core": "^11.2.0",
|
|
39
|
+
"@lokalise/package-vite-config": "latest",
|
|
40
|
+
"@vitest/coverage-v8": "^2.0.4",
|
|
41
|
+
"prom-client": "^15.1.3",
|
|
42
|
+
"rimraf": "^6.0.1",
|
|
43
|
+
"toad-scheduler": "^3.0.1",
|
|
44
|
+
"typescript": "5.5.4",
|
|
45
|
+
"vite": "5.3.5",
|
|
46
|
+
"vitest": "^2.0.4"
|
|
47
|
+
}
|
|
48
|
+
}
|