@hozon/store-log 0.2.1 → 0.2.3
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/lib/api.js +17 -8
- package/lib/migrations.js +6 -0
- package/lib/tables.d.ts +1 -1
- package/lib/types.d.ts +4 -0
- package/package.json +6 -5
package/lib/api.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { chunk, withKeepSet, withStoreTransaction } from '@hozon/db';
|
|
2
|
+
import { b64uFromUTF, b64uToUTF } from '@sozai/codec';
|
|
2
3
|
import { categoryRange, encodeCategory } from './category.js';
|
|
4
|
+
function decodeLog(data) {
|
|
5
|
+
// Decode only the JSON column; nested JSON-looking strings are payload values.
|
|
6
|
+
return typeof data === 'string' ? JSON.parse(data) : data;
|
|
7
|
+
}
|
|
3
8
|
function encodeCursor(timestamp, seq) {
|
|
4
|
-
return
|
|
9
|
+
return b64uFromUTF(JSON.stringify([
|
|
5
10
|
timestamp,
|
|
6
11
|
seq
|
|
7
|
-
]))
|
|
12
|
+
]));
|
|
8
13
|
}
|
|
9
14
|
function decodeCursor(cursor) {
|
|
10
15
|
try {
|
|
11
|
-
const value = JSON.parse(
|
|
16
|
+
const value = JSON.parse(b64uToUTF(cursor));
|
|
12
17
|
if (!Array.isArray(value) || value.length !== 2 || !value.every((item)=>typeof item === 'number' && Number.isFinite(item)) || !Number.isInteger(value[1])) {
|
|
13
18
|
throw new TypeError('Invalid log cursor');
|
|
14
19
|
}
|
|
@@ -45,6 +50,9 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
45
50
|
},
|
|
46
51
|
async queryLogs (params) {
|
|
47
52
|
if (!Number.isInteger(params.limit) || params.limit <= 0) throw new RangeError('Log limit must be a positive integer');
|
|
53
|
+
if (params.spanID !== undefined && params.traceID === undefined) throw new TypeError('Log spanID filter requires traceID');
|
|
54
|
+
const order = params.order ?? 'asc';
|
|
55
|
+
const after = order === 'asc' ? '>' : '<';
|
|
48
56
|
const limit = Math.min(params.limit, 1000);
|
|
49
57
|
let query = db.selectFrom('logs').select([
|
|
50
58
|
'timestamp',
|
|
@@ -66,21 +74,22 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
66
74
|
query = query.where('category', '>=', gte).where('category', '<', lt);
|
|
67
75
|
}
|
|
68
76
|
if (params.traceID !== undefined) query = query.where('trace_id', '=', params.traceID);
|
|
77
|
+
if (params.spanID !== undefined) query = query.where('span_id', '=', params.spanID);
|
|
69
78
|
if (params.cursor !== undefined) {
|
|
70
79
|
const [timestamp, seq] = decodeCursor(params.cursor);
|
|
71
80
|
query = query.where((eb)=>eb.or([
|
|
72
|
-
eb('timestamp',
|
|
81
|
+
eb('timestamp', after, timestamp),
|
|
73
82
|
eb.and([
|
|
74
83
|
eb('timestamp', '=', timestamp),
|
|
75
|
-
eb('seq',
|
|
84
|
+
eb('seq', after, seq)
|
|
76
85
|
])
|
|
77
86
|
]));
|
|
78
87
|
}
|
|
79
|
-
const rows = await query.orderBy('timestamp').orderBy('seq').limit(limit + 1).execute();
|
|
88
|
+
const rows = await query.orderBy('timestamp', order).orderBy('seq', order).limit(limit + 1).execute();
|
|
80
89
|
const page = rows.slice(0, limit);
|
|
81
90
|
const last = page.at(-1);
|
|
82
91
|
return {
|
|
83
|
-
logs: page.map((row)=>row.data),
|
|
92
|
+
logs: page.map((row)=>decodeLog(row.data)),
|
|
84
93
|
...rows.length > limit && last !== undefined ? {
|
|
85
94
|
cursor: encodeCursor(last.timestamp, last.seq)
|
|
86
95
|
} : {}
|
|
@@ -89,7 +98,7 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
89
98
|
async getTraceLogs (traceID) {
|
|
90
99
|
const rows = await db.selectFrom('logs').select('data').where('trace_id', '=', traceID).orderBy('timestamp').orderBy('seq').execute();
|
|
91
100
|
// addLogs enforces paired IDs for every traced row.
|
|
92
|
-
return rows.map((row)=>row.data);
|
|
101
|
+
return rows.map((row)=>decodeLog(row.data));
|
|
93
102
|
},
|
|
94
103
|
async deleteByTrace (traceIDs) {
|
|
95
104
|
if (traceIDs.length === 0) return 0;
|
package/lib/migrations.js
CHANGED
|
@@ -16,6 +16,12 @@ export function logStoreMigrations(ctx) {
|
|
|
16
16
|
'timestamp',
|
|
17
17
|
'seq'
|
|
18
18
|
]).execute();
|
|
19
|
+
await db.schema.createIndex(`${ctx.tablePrefix}_logs_trace_span_timestamp_seq`).on('logs').columns([
|
|
20
|
+
'trace_id',
|
|
21
|
+
'span_id',
|
|
22
|
+
'timestamp',
|
|
23
|
+
'seq'
|
|
24
|
+
]).execute();
|
|
19
25
|
await db.schema.createIndex(`${ctx.tablePrefix}_logs_level_timestamp`).on('logs').columns([
|
|
20
26
|
'level',
|
|
21
27
|
'timestamp'
|
package/lib/tables.d.ts
CHANGED
package/lib/types.d.ts
CHANGED
|
@@ -20,6 +20,10 @@ export type QueryLogsParams = {
|
|
|
20
20
|
levels?: Array<LogLevel>;
|
|
21
21
|
categoryPrefix?: Array<string>;
|
|
22
22
|
traceID?: string;
|
|
23
|
+
/** Requires traceID. */
|
|
24
|
+
spanID?: string;
|
|
25
|
+
/** Defaults to 'asc'. */
|
|
26
|
+
order?: 'asc' | 'desc';
|
|
23
27
|
limit: number;
|
|
24
28
|
cursor?: string;
|
|
25
29
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hozon/store-log",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Portable structured log store for Hozon",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,15 +18,16 @@
|
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@hozon/adapter": "^0.1.0",
|
|
21
|
-
"@hozon/db": "^0.2.
|
|
21
|
+
"@hozon/db": "^0.2.3",
|
|
22
|
+
"@sozai/codec": "^0.4.3",
|
|
22
23
|
"@sozai/json": "^0.3.0"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@hozon/node-sqlite": "^0.1.0",
|
|
26
|
-
"@types/node": "^26.6.
|
|
27
|
-
"del-cli": "^
|
|
27
|
+
"@types/node": "^26.6.5",
|
|
28
|
+
"del-cli": "^8.0.1",
|
|
28
29
|
"kysely": "^0.29.6",
|
|
29
|
-
"vitest": "5.0.
|
|
30
|
+
"vitest": "5.0.4"
|
|
30
31
|
},
|
|
31
32
|
"publishConfig": {
|
|
32
33
|
"access": "public"
|