qualink 0.1.1 → 0.3.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 +1 -1
- package/README.md +1 -0
- package/dist/cli/command-factory.d.ts +31 -19
- package/dist/cli/commands/biome.d.ts +31 -19
- package/dist/cli/commands/coverage-dotnet.d.ts +31 -19
- package/dist/cli/commands/coverage-js.d.ts +31 -19
- package/dist/cli/commands/eslint.d.ts +31 -19
- package/dist/cli/commands/lighthouse.d.ts +31 -19
- package/dist/cli/commands/lighthouse.js +4 -1
- package/dist/cli/commands/meta.d.ts +31 -19
- package/dist/cli/commands/sarif.d.ts +31 -19
- package/dist/cli/common-args.d.ts +16 -0
- package/dist/cli/common-args.js +4 -0
- package/dist/cli/send-to-sink.js +24 -7
- package/dist/collectors/eslint.d.ts +2 -0
- package/dist/collectors/eslint.js +32 -3
- package/dist/collectors/lighthouse.d.ts +4 -1
- package/dist/collectors/lighthouse.js +89 -1
- package/dist/sinks/index.d.ts +5 -1
- package/dist/sinks/index.js +14 -0
- package/dist/sinks/loki.d.ts +30 -0
- package/dist/sinks/loki.js +84 -0
- package/dist/types.d.ts +19 -0
- package/package.json +6 -6
|
@@ -1,63 +1,75 @@
|
|
|
1
1
|
export declare const metaCommand: import("citty").CommandDef<{
|
|
2
|
-
input: {
|
|
2
|
+
readonly input: {
|
|
3
3
|
readonly type: "string";
|
|
4
4
|
readonly required: true;
|
|
5
5
|
};
|
|
6
|
-
sink: {
|
|
6
|
+
readonly sink: {
|
|
7
7
|
readonly type: "string";
|
|
8
8
|
readonly default: "elastic";
|
|
9
9
|
};
|
|
10
|
-
repo: {
|
|
10
|
+
readonly repo: {
|
|
11
11
|
readonly type: "string";
|
|
12
12
|
};
|
|
13
|
-
category: {
|
|
13
|
+
readonly category: {
|
|
14
14
|
readonly type: "string";
|
|
15
15
|
};
|
|
16
|
-
tags: {
|
|
16
|
+
readonly tags: {
|
|
17
17
|
readonly type: "string";
|
|
18
18
|
};
|
|
19
|
-
branch: {
|
|
19
|
+
readonly branch: {
|
|
20
20
|
readonly type: "string";
|
|
21
21
|
};
|
|
22
|
-
"commit-sha": {
|
|
22
|
+
readonly "commit-sha": {
|
|
23
23
|
readonly type: "string";
|
|
24
24
|
};
|
|
25
|
-
"pipeline-run-id": {
|
|
25
|
+
readonly "pipeline-run-id": {
|
|
26
26
|
readonly type: "string";
|
|
27
27
|
};
|
|
28
|
-
"pipeline-provider": {
|
|
28
|
+
readonly "pipeline-provider": {
|
|
29
29
|
readonly type: "string";
|
|
30
30
|
};
|
|
31
|
-
environment: {
|
|
31
|
+
readonly environment: {
|
|
32
32
|
readonly type: "string";
|
|
33
33
|
readonly default: "ci";
|
|
34
34
|
};
|
|
35
|
-
package: {
|
|
35
|
+
readonly package: {
|
|
36
36
|
readonly type: "string";
|
|
37
37
|
};
|
|
38
|
-
project: {
|
|
38
|
+
readonly project: {
|
|
39
39
|
readonly type: "string";
|
|
40
40
|
};
|
|
41
|
-
"collector-version": {
|
|
41
|
+
readonly "collector-version": {
|
|
42
42
|
readonly type: "string";
|
|
43
43
|
};
|
|
44
|
-
"elastic-url": {
|
|
44
|
+
readonly "elastic-url": {
|
|
45
45
|
readonly type: "string";
|
|
46
46
|
};
|
|
47
|
-
"elastic-api-key": {
|
|
47
|
+
readonly "elastic-api-key": {
|
|
48
48
|
readonly type: "string";
|
|
49
49
|
};
|
|
50
|
-
"
|
|
50
|
+
readonly "loki-url": {
|
|
51
51
|
readonly type: "string";
|
|
52
52
|
};
|
|
53
|
-
"
|
|
53
|
+
readonly "loki-username": {
|
|
54
54
|
readonly type: "string";
|
|
55
55
|
};
|
|
56
|
-
"
|
|
56
|
+
readonly "loki-password": {
|
|
57
|
+
readonly type: "string";
|
|
58
|
+
};
|
|
59
|
+
readonly "loki-tenant-id": {
|
|
60
|
+
readonly type: "string";
|
|
61
|
+
};
|
|
62
|
+
readonly "retry-max": {
|
|
63
|
+
readonly type: "string";
|
|
64
|
+
};
|
|
65
|
+
readonly "retry-backoff-ms": {
|
|
66
|
+
readonly type: "string";
|
|
67
|
+
};
|
|
68
|
+
readonly "allow-empty": {
|
|
57
69
|
readonly type: "boolean";
|
|
58
70
|
readonly default: false;
|
|
59
71
|
};
|
|
60
|
-
"dry-run": {
|
|
72
|
+
readonly "dry-run": {
|
|
61
73
|
readonly type: "boolean";
|
|
62
74
|
readonly default: false;
|
|
63
75
|
};
|
|
@@ -1,63 +1,75 @@
|
|
|
1
1
|
export declare const sarifCommand: import("citty").CommandDef<{
|
|
2
|
-
input: {
|
|
2
|
+
readonly input: {
|
|
3
3
|
readonly type: "string";
|
|
4
4
|
readonly required: true;
|
|
5
5
|
};
|
|
6
|
-
sink: {
|
|
6
|
+
readonly sink: {
|
|
7
7
|
readonly type: "string";
|
|
8
8
|
readonly default: "elastic";
|
|
9
9
|
};
|
|
10
|
-
repo: {
|
|
10
|
+
readonly repo: {
|
|
11
11
|
readonly type: "string";
|
|
12
12
|
};
|
|
13
|
-
category: {
|
|
13
|
+
readonly category: {
|
|
14
14
|
readonly type: "string";
|
|
15
15
|
};
|
|
16
|
-
tags: {
|
|
16
|
+
readonly tags: {
|
|
17
17
|
readonly type: "string";
|
|
18
18
|
};
|
|
19
|
-
branch: {
|
|
19
|
+
readonly branch: {
|
|
20
20
|
readonly type: "string";
|
|
21
21
|
};
|
|
22
|
-
"commit-sha": {
|
|
22
|
+
readonly "commit-sha": {
|
|
23
23
|
readonly type: "string";
|
|
24
24
|
};
|
|
25
|
-
"pipeline-run-id": {
|
|
25
|
+
readonly "pipeline-run-id": {
|
|
26
26
|
readonly type: "string";
|
|
27
27
|
};
|
|
28
|
-
"pipeline-provider": {
|
|
28
|
+
readonly "pipeline-provider": {
|
|
29
29
|
readonly type: "string";
|
|
30
30
|
};
|
|
31
|
-
environment: {
|
|
31
|
+
readonly environment: {
|
|
32
32
|
readonly type: "string";
|
|
33
33
|
readonly default: "ci";
|
|
34
34
|
};
|
|
35
|
-
package: {
|
|
35
|
+
readonly package: {
|
|
36
36
|
readonly type: "string";
|
|
37
37
|
};
|
|
38
|
-
project: {
|
|
38
|
+
readonly project: {
|
|
39
39
|
readonly type: "string";
|
|
40
40
|
};
|
|
41
|
-
"collector-version": {
|
|
41
|
+
readonly "collector-version": {
|
|
42
42
|
readonly type: "string";
|
|
43
43
|
};
|
|
44
|
-
"elastic-url": {
|
|
44
|
+
readonly "elastic-url": {
|
|
45
45
|
readonly type: "string";
|
|
46
46
|
};
|
|
47
|
-
"elastic-api-key": {
|
|
47
|
+
readonly "elastic-api-key": {
|
|
48
48
|
readonly type: "string";
|
|
49
49
|
};
|
|
50
|
-
"
|
|
50
|
+
readonly "loki-url": {
|
|
51
51
|
readonly type: "string";
|
|
52
52
|
};
|
|
53
|
-
"
|
|
53
|
+
readonly "loki-username": {
|
|
54
54
|
readonly type: "string";
|
|
55
55
|
};
|
|
56
|
-
"
|
|
56
|
+
readonly "loki-password": {
|
|
57
|
+
readonly type: "string";
|
|
58
|
+
};
|
|
59
|
+
readonly "loki-tenant-id": {
|
|
60
|
+
readonly type: "string";
|
|
61
|
+
};
|
|
62
|
+
readonly "retry-max": {
|
|
63
|
+
readonly type: "string";
|
|
64
|
+
};
|
|
65
|
+
readonly "retry-backoff-ms": {
|
|
66
|
+
readonly type: "string";
|
|
67
|
+
};
|
|
68
|
+
readonly "allow-empty": {
|
|
57
69
|
readonly type: "boolean";
|
|
58
70
|
readonly default: false;
|
|
59
71
|
};
|
|
60
|
-
"dry-run": {
|
|
72
|
+
readonly "dry-run": {
|
|
61
73
|
readonly type: "boolean";
|
|
62
74
|
readonly default: false;
|
|
63
75
|
};
|
|
@@ -15,6 +15,10 @@ export interface CommonArgs {
|
|
|
15
15
|
collectorVersion?: unknown;
|
|
16
16
|
elasticUrl?: unknown;
|
|
17
17
|
elasticApiKey?: unknown;
|
|
18
|
+
lokiUrl?: unknown;
|
|
19
|
+
lokiUsername?: unknown;
|
|
20
|
+
lokiPassword?: unknown;
|
|
21
|
+
lokiTenantId?: unknown;
|
|
18
22
|
retryMax?: unknown;
|
|
19
23
|
retryBackoffMs?: unknown;
|
|
20
24
|
allowEmpty?: unknown;
|
|
@@ -71,6 +75,18 @@ export declare const commonArgs: {
|
|
|
71
75
|
readonly "elastic-api-key": {
|
|
72
76
|
readonly type: "string";
|
|
73
77
|
};
|
|
78
|
+
readonly "loki-url": {
|
|
79
|
+
readonly type: "string";
|
|
80
|
+
};
|
|
81
|
+
readonly "loki-username": {
|
|
82
|
+
readonly type: "string";
|
|
83
|
+
};
|
|
84
|
+
readonly "loki-password": {
|
|
85
|
+
readonly type: "string";
|
|
86
|
+
};
|
|
87
|
+
readonly "loki-tenant-id": {
|
|
88
|
+
readonly type: "string";
|
|
89
|
+
};
|
|
74
90
|
readonly "retry-max": {
|
|
75
91
|
readonly type: "string";
|
|
76
92
|
};
|
package/dist/cli/common-args.js
CHANGED
|
@@ -37,6 +37,10 @@ export const commonArgs = {
|
|
|
37
37
|
"collector-version": { type: "string" },
|
|
38
38
|
"elastic-url": { type: "string" },
|
|
39
39
|
"elastic-api-key": { type: "string" },
|
|
40
|
+
"loki-url": { type: "string" },
|
|
41
|
+
"loki-username": { type: "string" },
|
|
42
|
+
"loki-password": { type: "string" },
|
|
43
|
+
"loki-tenant-id": { type: "string" },
|
|
40
44
|
"retry-max": { type: "string" },
|
|
41
45
|
"retry-backoff-ms": { type: "string" },
|
|
42
46
|
"allow-empty": { type: "boolean", default: false },
|
package/dist/cli/send-to-sink.js
CHANGED
|
@@ -27,26 +27,43 @@ export async function sendToSink(metricType, args, documents) {
|
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
29
|
const sinkKindRaw = (envOrArg(argValue(args, "sink"), "QUALINK_SINK") ?? "elastic").toLowerCase();
|
|
30
|
-
if (sinkKindRaw !== "elastic" && sinkKindRaw !== "stdout") {
|
|
31
|
-
throw new CliError(`Unsupported sink '${sinkKindRaw}'. Expected elastic|stdout`, 2);
|
|
30
|
+
if (sinkKindRaw !== "elastic" && sinkKindRaw !== "loki" && sinkKindRaw !== "stdout") {
|
|
31
|
+
throw new CliError(`Unsupported sink '${sinkKindRaw}'. Expected elastic|loki|stdout`, 2);
|
|
32
32
|
}
|
|
33
|
-
const sinkKind = sinkKindRaw
|
|
33
|
+
const sinkKind = sinkKindRaw;
|
|
34
34
|
const retryMax = parseNumberInput(argValue(args, "retryMax", "retry-max"), 2);
|
|
35
35
|
const retryBackoffMs = parseNumberInput(argValue(args, "retryBackoffMs", "retry-backoff-ms"), 500);
|
|
36
36
|
const elasticUrl = envOrArg(argValue(args, "elasticUrl", "elastic-url"), "ELASTIC_URL");
|
|
37
37
|
const elasticApiKey = envOrArg(argValue(args, "elasticApiKey", "elastic-api-key"), "ELASTIC_API_KEY");
|
|
38
|
+
const lokiUrl = envOrArg(argValue(args, "lokiUrl", "loki-url"), "LOKI_URL");
|
|
39
|
+
const lokiUsername = envOrArg(argValue(args, "lokiUsername", "loki-username"), "LOKI_USERNAME");
|
|
40
|
+
const lokiPassword = envOrArg(argValue(args, "lokiPassword", "loki-password"), "LOKI_PASSWORD");
|
|
41
|
+
const lokiTenantId = envOrArg(argValue(args, "lokiTenantId", "loki-tenant-id"), "LOKI_TENANT_ID");
|
|
38
42
|
const sinkConfigBase = {
|
|
39
43
|
kind: sinkKind,
|
|
40
44
|
retryMax,
|
|
41
45
|
retryBackoffMs,
|
|
42
46
|
};
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
let sinkConfig;
|
|
48
|
+
if (sinkKind === "elastic") {
|
|
49
|
+
sinkConfig = {
|
|
45
50
|
...sinkConfigBase,
|
|
46
51
|
...(elasticUrl ? { elasticUrl } : {}),
|
|
47
52
|
...(elasticApiKey ? { elasticApiKey } : {}),
|
|
48
|
-
}
|
|
49
|
-
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
else if (sinkKind === "loki") {
|
|
56
|
+
sinkConfig = {
|
|
57
|
+
...sinkConfigBase,
|
|
58
|
+
...(lokiUrl ? { lokiUrl } : {}),
|
|
59
|
+
...(lokiUsername ? { lokiUsername } : {}),
|
|
60
|
+
...(lokiPassword ? { lokiPassword } : {}),
|
|
61
|
+
...(lokiTenantId ? { lokiTenantId } : {}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
sinkConfig = sinkConfigBase;
|
|
66
|
+
}
|
|
50
67
|
const sink = createSink(sinkConfig);
|
|
51
68
|
await sink.send({
|
|
52
69
|
metricType,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CommonMetadata, EslintMetricDocument, Language } from "../types.js";
|
|
2
2
|
interface EslintMessage {
|
|
3
3
|
ruleId: string | null;
|
|
4
|
+
severity?: number | undefined;
|
|
4
5
|
}
|
|
5
6
|
interface EslintFileResult {
|
|
6
7
|
filePath: string;
|
|
@@ -9,6 +10,7 @@ interface EslintFileResult {
|
|
|
9
10
|
fixableErrorCount: number;
|
|
10
11
|
fixableWarningCount: number;
|
|
11
12
|
messages: EslintMessage[];
|
|
13
|
+
suppressedMessages: EslintMessage[];
|
|
12
14
|
}
|
|
13
15
|
export interface EslintCollectorOptions {
|
|
14
16
|
includeRules: boolean;
|
|
@@ -12,16 +12,20 @@ function asEslintResults(input) {
|
|
|
12
12
|
if (!Array.isArray(messagesRaw)) {
|
|
13
13
|
throw new Error("Invalid ESLint messages array");
|
|
14
14
|
}
|
|
15
|
-
const
|
|
15
|
+
const parseMessages = (raw) => raw.map((message) => {
|
|
16
16
|
if (!isRecord(message)) {
|
|
17
17
|
return { ruleId: null };
|
|
18
18
|
}
|
|
19
19
|
const ruleValue = message.ruleId;
|
|
20
|
+
const severity = typeof message.severity === "number" ? message.severity : undefined;
|
|
20
21
|
if (typeof ruleValue !== "string" && ruleValue !== null) {
|
|
21
|
-
return { ruleId: null };
|
|
22
|
+
return { ruleId: null, severity };
|
|
22
23
|
}
|
|
23
|
-
return { ruleId: ruleValue };
|
|
24
|
+
return { ruleId: ruleValue, severity };
|
|
24
25
|
});
|
|
26
|
+
const messages = parseMessages(messagesRaw);
|
|
27
|
+
const suppressedRaw = entry.suppressedMessages;
|
|
28
|
+
const suppressedMessages = Array.isArray(suppressedRaw) ? parseMessages(suppressedRaw) : [];
|
|
25
29
|
return {
|
|
26
30
|
filePath: typeof entry.filePath === "string" ? entry.filePath : "unknown",
|
|
27
31
|
errorCount: typeof entry.errorCount === "number" ? entry.errorCount : 0,
|
|
@@ -29,6 +33,7 @@ function asEslintResults(input) {
|
|
|
29
33
|
fixableErrorCount: typeof entry.fixableErrorCount === "number" ? entry.fixableErrorCount : 0,
|
|
30
34
|
fixableWarningCount: typeof entry.fixableWarningCount === "number" ? entry.fixableWarningCount : 0,
|
|
31
35
|
messages,
|
|
36
|
+
suppressedMessages,
|
|
32
37
|
};
|
|
33
38
|
});
|
|
34
39
|
}
|
|
@@ -58,7 +63,10 @@ export function collectEslint(input, metadata, options) {
|
|
|
58
63
|
let warnings = 0;
|
|
59
64
|
let fixableErrors = 0;
|
|
60
65
|
let fixableWarnings = 0;
|
|
66
|
+
let suppressedErrors = 0;
|
|
67
|
+
let suppressedWarnings = 0;
|
|
61
68
|
const rules = new Map();
|
|
69
|
+
const suppressedRules = new Map();
|
|
62
70
|
const files = [];
|
|
63
71
|
for (const row of rows) {
|
|
64
72
|
errors += row.errorCount;
|
|
@@ -81,6 +89,20 @@ export function collectEslint(input, metadata, options) {
|
|
|
81
89
|
const existing = rules.get(message.ruleId) ?? 0;
|
|
82
90
|
rules.set(message.ruleId, existing + 1);
|
|
83
91
|
}
|
|
92
|
+
for (const message of row.suppressedMessages) {
|
|
93
|
+
// severity: 2 = error, 1 = warning
|
|
94
|
+
if (message.severity === 2) {
|
|
95
|
+
suppressedErrors++;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
suppressedWarnings++;
|
|
99
|
+
}
|
|
100
|
+
if (!message.ruleId) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const existing = suppressedRules.get(message.ruleId) ?? 0;
|
|
104
|
+
suppressedRules.set(message.ruleId, existing + 1);
|
|
105
|
+
}
|
|
84
106
|
}
|
|
85
107
|
const languages = options.languages ?? detectEslintLanguages(rows);
|
|
86
108
|
const doc = {
|
|
@@ -94,11 +116,18 @@ export function collectEslint(input, metadata, options) {
|
|
|
94
116
|
warnings,
|
|
95
117
|
fixable_errors: fixableErrors,
|
|
96
118
|
fixable_warnings: fixableWarnings,
|
|
119
|
+
suppressed_errors: suppressedErrors,
|
|
120
|
+
suppressed_warnings: suppressedWarnings,
|
|
97
121
|
};
|
|
98
122
|
if (options.includeRules) {
|
|
99
123
|
const sortedRules = [...rules.entries()].sort((a, b) => b[1] - a[1]);
|
|
100
124
|
const slice = options.topRules > 0 ? sortedRules.slice(0, options.topRules) : sortedRules;
|
|
101
125
|
doc.rules_violated = Object.fromEntries(slice);
|
|
126
|
+
if (suppressedRules.size > 0) {
|
|
127
|
+
const sortedSuppressed = [...suppressedRules.entries()].sort((a, b) => b[1] - a[1]);
|
|
128
|
+
const suppressedSlice = options.topRules > 0 ? sortedSuppressed.slice(0, options.topRules) : sortedSuppressed;
|
|
129
|
+
doc.suppressed_rules = Object.fromEntries(suppressedSlice);
|
|
130
|
+
}
|
|
102
131
|
}
|
|
103
132
|
if (files.length > 0 && (options.topFiles > 0 || options.includeAllFiles)) {
|
|
104
133
|
const sortedFiles = [...files].sort((a, b) => {
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { CommonMetadata, LighthouseMetricDocument } from "../types.js";
|
|
2
|
-
export
|
|
2
|
+
export interface LighthouseCollectorOptions {
|
|
3
|
+
includeFilmstrip: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function collectLighthouse(input: unknown, metadata: CommonMetadata, url: string, options?: LighthouseCollectorOptions): LighthouseMetricDocument[];
|
|
@@ -12,7 +12,42 @@ function readCategoryScore(categories, key) {
|
|
|
12
12
|
}
|
|
13
13
|
return toPctFromUnitScore(score);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
function readAuditNumeric(audits, key) {
|
|
16
|
+
const audit = audits[key];
|
|
17
|
+
if (!isRecord(audit)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const value = audit.numericValue;
|
|
21
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function extractFilmstrip(audits) {
|
|
27
|
+
const thumbnail = audits["screenshot-thumbnails"];
|
|
28
|
+
if (!isRecord(thumbnail))
|
|
29
|
+
return undefined;
|
|
30
|
+
const details = thumbnail.details;
|
|
31
|
+
if (!isRecord(details))
|
|
32
|
+
return undefined;
|
|
33
|
+
if (details.type !== "filmstrip")
|
|
34
|
+
return undefined;
|
|
35
|
+
const items = details.items;
|
|
36
|
+
if (!Array.isArray(items))
|
|
37
|
+
return undefined;
|
|
38
|
+
const frames = [];
|
|
39
|
+
for (const item of items) {
|
|
40
|
+
if (!isRecord(item))
|
|
41
|
+
continue;
|
|
42
|
+
if (typeof item.timing !== "number" || !Number.isFinite(item.timing))
|
|
43
|
+
continue;
|
|
44
|
+
if (typeof item.data !== "string")
|
|
45
|
+
continue;
|
|
46
|
+
frames.push({ timing: item.timing, data: item.data });
|
|
47
|
+
}
|
|
48
|
+
return frames.length > 0 ? frames : undefined;
|
|
49
|
+
}
|
|
50
|
+
export function collectLighthouse(input, metadata, url, options) {
|
|
16
51
|
if (!isRecord(input)) {
|
|
17
52
|
throw new Error("Lighthouse input must be an object");
|
|
18
53
|
}
|
|
@@ -33,5 +68,58 @@ export function collectLighthouse(input, metadata, url) {
|
|
|
33
68
|
best_practices: readCategoryScore(categoriesUnknown, "best-practices"),
|
|
34
69
|
seo: readCategoryScore(categoriesUnknown, "seo"),
|
|
35
70
|
};
|
|
71
|
+
const audits = input.audits;
|
|
72
|
+
if (isRecord(audits)) {
|
|
73
|
+
const fcp = readAuditNumeric(audits, "first-contentful-paint");
|
|
74
|
+
const lcp = readAuditNumeric(audits, "largest-contentful-paint");
|
|
75
|
+
const tbt = readAuditNumeric(audits, "total-blocking-time");
|
|
76
|
+
const cls = readAuditNumeric(audits, "cumulative-layout-shift");
|
|
77
|
+
const si = readAuditNumeric(audits, "speed-index");
|
|
78
|
+
const tti = readAuditNumeric(audits, "interactive");
|
|
79
|
+
const ttfb = readAuditNumeric(audits, "server-response-time");
|
|
80
|
+
const totalByteWeight = readAuditNumeric(audits, "total-byte-weight");
|
|
81
|
+
const domSize = readAuditNumeric(audits, "dom-size");
|
|
82
|
+
if (fcp !== undefined)
|
|
83
|
+
doc.fcp = fcp;
|
|
84
|
+
if (lcp !== undefined)
|
|
85
|
+
doc.lcp = lcp;
|
|
86
|
+
if (tbt !== undefined)
|
|
87
|
+
doc.tbt = tbt;
|
|
88
|
+
if (cls !== undefined)
|
|
89
|
+
doc.cls = cls;
|
|
90
|
+
if (si !== undefined)
|
|
91
|
+
doc.si = si;
|
|
92
|
+
if (tti !== undefined)
|
|
93
|
+
doc.tti = tti;
|
|
94
|
+
if (ttfb !== undefined)
|
|
95
|
+
doc.ttfb = ttfb;
|
|
96
|
+
if (totalByteWeight !== undefined)
|
|
97
|
+
doc.total_byte_weight = totalByteWeight;
|
|
98
|
+
if (domSize !== undefined)
|
|
99
|
+
doc.dom_size = domSize;
|
|
100
|
+
const auditScores = {};
|
|
101
|
+
const auditValues = {};
|
|
102
|
+
for (const [id, raw] of Object.entries(audits)) {
|
|
103
|
+
if (!isRecord(raw))
|
|
104
|
+
continue;
|
|
105
|
+
if (typeof raw.score === "number" && Number.isFinite(raw.score)) {
|
|
106
|
+
auditScores[id] = toPctFromUnitScore(raw.score);
|
|
107
|
+
}
|
|
108
|
+
if (typeof raw.numericValue === "number" && Number.isFinite(raw.numericValue)) {
|
|
109
|
+
auditValues[id] = raw.numericValue;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (Object.keys(auditScores).length > 0) {
|
|
113
|
+
doc.audit_scores = auditScores;
|
|
114
|
+
}
|
|
115
|
+
if (Object.keys(auditValues).length > 0) {
|
|
116
|
+
doc.audit_values = auditValues;
|
|
117
|
+
}
|
|
118
|
+
if (options?.includeFilmstrip) {
|
|
119
|
+
const filmstrip = extractFilmstrip(audits);
|
|
120
|
+
if (filmstrip)
|
|
121
|
+
doc.filmstrip = filmstrip;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
36
124
|
return [doc];
|
|
37
125
|
}
|
package/dist/sinks/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { Sink } from "./types.js";
|
|
2
|
-
export type SinkKind = "elastic" | "stdout";
|
|
2
|
+
export type SinkKind = "elastic" | "loki" | "stdout";
|
|
3
3
|
export interface SinkConfig {
|
|
4
4
|
kind: SinkKind;
|
|
5
5
|
elasticUrl?: string;
|
|
6
6
|
elasticApiKey?: string;
|
|
7
|
+
lokiUrl?: string;
|
|
8
|
+
lokiUsername?: string;
|
|
9
|
+
lokiPassword?: string;
|
|
10
|
+
lokiTenantId?: string;
|
|
7
11
|
retryMax: number;
|
|
8
12
|
retryBackoffMs: number;
|
|
9
13
|
}
|
package/dist/sinks/index.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { ElasticSink } from "./elastic.js";
|
|
2
|
+
import { LokiSink } from "./loki.js";
|
|
2
3
|
import { StdoutSink } from "./stdout.js";
|
|
3
4
|
export function createSink(config) {
|
|
4
5
|
if (config.kind === "stdout") {
|
|
5
6
|
return new StdoutSink();
|
|
6
7
|
}
|
|
8
|
+
if (config.kind === "loki") {
|
|
9
|
+
if (!config.lokiUrl) {
|
|
10
|
+
throw new Error("Loki sink requires LOKI_URL (or --loki-url)");
|
|
11
|
+
}
|
|
12
|
+
return new LokiSink({
|
|
13
|
+
url: config.lokiUrl,
|
|
14
|
+
username: config.lokiUsername,
|
|
15
|
+
password: config.lokiPassword,
|
|
16
|
+
tenantId: config.lokiTenantId,
|
|
17
|
+
retryMax: config.retryMax,
|
|
18
|
+
retryBackoffMs: config.retryBackoffMs,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
7
21
|
if (!config.elasticUrl || !config.elasticApiKey) {
|
|
8
22
|
throw new Error("Elastic sink requires ELASTIC_URL and ELASTIC_API_KEY (or CLI overrides)");
|
|
9
23
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { NormalizedDocument } from "../types.js";
|
|
2
|
+
import type { SendInput, Sink } from "./types.js";
|
|
3
|
+
export interface LokiSinkOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
username?: string | undefined;
|
|
6
|
+
password?: string | undefined;
|
|
7
|
+
tenantId?: string | undefined;
|
|
8
|
+
retryMax: number;
|
|
9
|
+
retryBackoffMs: number;
|
|
10
|
+
}
|
|
11
|
+
interface LokiStream {
|
|
12
|
+
stream: Record<string, string>;
|
|
13
|
+
values: [string, string][];
|
|
14
|
+
}
|
|
15
|
+
interface LokiPushPayload {
|
|
16
|
+
streams: LokiStream[];
|
|
17
|
+
}
|
|
18
|
+
export declare function toNanosecondEpoch(isoTimestamp: string): string;
|
|
19
|
+
export declare function buildLokiPayload(documents: NormalizedDocument[]): LokiPushPayload;
|
|
20
|
+
export declare class LokiSink implements Sink {
|
|
21
|
+
private readonly url;
|
|
22
|
+
private readonly username;
|
|
23
|
+
private readonly password;
|
|
24
|
+
private readonly tenantId;
|
|
25
|
+
private readonly retryMax;
|
|
26
|
+
private readonly retryBackoffMs;
|
|
27
|
+
constructor(options: LokiSinkOptions);
|
|
28
|
+
send(input: SendInput): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
function sleep(ms) {
|
|
2
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3
|
+
}
|
|
4
|
+
function isRetryableStatus(status) {
|
|
5
|
+
return status === 429 || status >= 500;
|
|
6
|
+
}
|
|
7
|
+
function toLabelKey(doc) {
|
|
8
|
+
return `${doc.metric_type}|${doc.repo}|${doc.environment}`;
|
|
9
|
+
}
|
|
10
|
+
export function toNanosecondEpoch(isoTimestamp) {
|
|
11
|
+
const ms = new Date(isoTimestamp).getTime();
|
|
12
|
+
return `${ms}000000`;
|
|
13
|
+
}
|
|
14
|
+
export function buildLokiPayload(documents) {
|
|
15
|
+
const streamMap = new Map();
|
|
16
|
+
for (const doc of documents) {
|
|
17
|
+
const key = toLabelKey(doc);
|
|
18
|
+
let stream = streamMap.get(key);
|
|
19
|
+
if (!stream) {
|
|
20
|
+
stream = {
|
|
21
|
+
stream: {
|
|
22
|
+
metric_type: doc.metric_type,
|
|
23
|
+
repo: doc.repo,
|
|
24
|
+
environment: doc.environment,
|
|
25
|
+
},
|
|
26
|
+
values: [],
|
|
27
|
+
};
|
|
28
|
+
streamMap.set(key, stream);
|
|
29
|
+
}
|
|
30
|
+
stream.values.push([toNanosecondEpoch(doc["@timestamp"]), JSON.stringify(doc)]);
|
|
31
|
+
}
|
|
32
|
+
return { streams: Array.from(streamMap.values()) };
|
|
33
|
+
}
|
|
34
|
+
export class LokiSink {
|
|
35
|
+
url;
|
|
36
|
+
username;
|
|
37
|
+
password;
|
|
38
|
+
tenantId;
|
|
39
|
+
retryMax;
|
|
40
|
+
retryBackoffMs;
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.url = options.url.replace(/\/$/, "");
|
|
43
|
+
this.username = options.username;
|
|
44
|
+
this.password = options.password;
|
|
45
|
+
this.tenantId = options.tenantId;
|
|
46
|
+
this.retryMax = options.retryMax;
|
|
47
|
+
this.retryBackoffMs = options.retryBackoffMs;
|
|
48
|
+
}
|
|
49
|
+
async send(input) {
|
|
50
|
+
if (input.documents.length === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const payload = buildLokiPayload(input.documents);
|
|
54
|
+
const body = JSON.stringify(payload);
|
|
55
|
+
const headers = {
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
};
|
|
58
|
+
if (this.username && this.password) {
|
|
59
|
+
const encoded = btoa(`${this.username}:${this.password}`);
|
|
60
|
+
headers.Authorization = `Basic ${encoded}`;
|
|
61
|
+
}
|
|
62
|
+
if (this.tenantId) {
|
|
63
|
+
headers["X-Scope-OrgID"] = this.tenantId;
|
|
64
|
+
}
|
|
65
|
+
let attempt = 0;
|
|
66
|
+
for (;;) {
|
|
67
|
+
attempt += 1;
|
|
68
|
+
const response = await fetch(`${this.url}/loki/api/v1/push`, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers,
|
|
71
|
+
body,
|
|
72
|
+
});
|
|
73
|
+
if (response.status === 204 || response.ok) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const responseText = await response.text();
|
|
77
|
+
if (!isRetryableStatus(response.status) || attempt > this.retryMax) {
|
|
78
|
+
process.stderr.write(`[qualink] Dead-letter payload:\n${body}\n`);
|
|
79
|
+
throw new Error(`Loki push failed (${response.status}): ${responseText}`);
|
|
80
|
+
}
|
|
81
|
+
await sleep(this.retryBackoffMs * attempt);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|