@hozon/store-telemetry 0.0.0-stage → 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paul Le Cam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,16 @@
1
- # Temporary Holding Version
1
+ # @hozon/store-telemetry
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Persistent OpenTelemetry span store with trace lookup, upsert, and retention.
4
+
5
+ ```sh
6
+ pnpm add @hozon/store-telemetry @hozon/db
7
+ ```
8
+
9
+ ```ts
10
+ import { getTelemetryStore, telemetryStoreDefinition } from '@hozon/store-telemetry'
11
+
12
+ db.register(telemetryStoreDefinition)
13
+ const telemetry = await getTelemetryStore(db)
14
+ ```
15
+
16
+ See the [store reference](../../docs/reference/stores/telemetry.md).
package/lib/api.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { Adapter } from '@hozon/adapter';
2
+ import type { Kysely } from 'kysely';
3
+ import type { TelemetryTables } from './tables.js';
4
+ import type { TelemetryStore } from './types.js';
5
+ export declare function createTelemetryStoreAPI(db: Kysely<TelemetryTables>, adapter: Adapter): TelemetryStore;
package/lib/api.js ADDED
@@ -0,0 +1,72 @@
1
+ import { chunk, withKeepSet, withStoreTransaction } from '@hozon/db';
2
+ export function createTelemetryStoreAPI(db, adapter) {
3
+ return {
4
+ async addSpans (spans) {
5
+ if (spans.length === 0) return;
6
+ const rows = spans.map((span)=>({
7
+ trace_id: span.traceID,
8
+ span_id: span.spanID,
9
+ start_time: span.startTime,
10
+ end_time: span.endTime,
11
+ data: adapter.encodeJSON(span)
12
+ }));
13
+ await withStoreTransaction(db, async (trx)=>{
14
+ let batch = [];
15
+ let pairs = new Set();
16
+ const insertBatch = async ()=>{
17
+ if (batch.length === 0) return;
18
+ await trx.insertInto('spans').values(batch).onConflict((conflict)=>conflict.columns([
19
+ 'trace_id',
20
+ 'span_id'
21
+ ]).doUpdateSet((eb)=>({
22
+ start_time: eb.ref('excluded.start_time'),
23
+ end_time: eb.ref('excluded.end_time'),
24
+ data: eb.ref('excluded.data')
25
+ }))).execute();
26
+ batch = [];
27
+ pairs = new Set();
28
+ };
29
+ for (const row of rows){
30
+ const pair = JSON.stringify([
31
+ row.trace_id,
32
+ row.span_id
33
+ ]);
34
+ if (pairs.has(pair) || batch.length === 100) await insertBatch();
35
+ batch.push(row);
36
+ pairs.add(pair);
37
+ }
38
+ await insertBatch();
39
+ });
40
+ },
41
+ async getSpans (traceID) {
42
+ const rows = await db.selectFrom('spans').select('data').where('trace_id', '=', traceID).orderBy('start_time').orderBy('seq').execute();
43
+ return rows.map((row)=>row.data);
44
+ },
45
+ async deleteByTrace (traceIDs) {
46
+ if (traceIDs.length === 0) return 0;
47
+ return withStoreTransaction(db, async (trx)=>{
48
+ let count = 0;
49
+ for (const ids of chunk(traceIDs)){
50
+ const result = await trx.deleteFrom('spans').where('trace_id', 'in', ids).executeTakeFirstOrThrow();
51
+ count += Number(result.numDeletedRows);
52
+ }
53
+ return count;
54
+ });
55
+ },
56
+ async deleteBefore (time, params) {
57
+ return withStoreTransaction(db, async (trx)=>{
58
+ if (params?.keepTraceIDs === undefined || params.keepTraceIDs.length === 0) {
59
+ const result = await trx.deleteFrom('spans').where('end_time', '<', time).executeTakeFirstOrThrow();
60
+ return Number(result.numDeletedRows);
61
+ }
62
+ return withKeepSet(trx, {
63
+ table: 'keep_telemetry',
64
+ ids: params.keepTraceIDs
65
+ }, async (selectKeep)=>{
66
+ const result = await trx.deleteFrom('spans').where('end_time', '<', time).where('trace_id', 'not in', selectKeep()).executeTakeFirstOrThrow();
67
+ return Number(result.numDeletedRows);
68
+ });
69
+ });
70
+ }
71
+ };
72
+ }
@@ -0,0 +1,6 @@
1
+ import type { StoreDefinition, StoreProvider } from '@hozon/db';
2
+ import type { TelemetryTables } from './tables.js';
3
+ import type { TelemetryStore } from './types.js';
4
+ export declare const TELEMETRY_STORE = "telemetry";
5
+ export declare const telemetryStoreDefinition: StoreDefinition<TelemetryTables, TelemetryStore>;
6
+ export declare function getTelemetryStore(provider: StoreProvider): Promise<TelemetryStore>;
@@ -0,0 +1,11 @@
1
+ import { createTelemetryStoreAPI } from './api.js';
2
+ import { telemetryStoreMigrations } from './migrations.js';
3
+ export const TELEMETRY_STORE = 'telemetry';
4
+ export const telemetryStoreDefinition = {
5
+ name: TELEMETRY_STORE,
6
+ migrations: telemetryStoreMigrations,
7
+ createAPI: createTelemetryStoreAPI
8
+ };
9
+ export async function getTelemetryStore(provider) {
10
+ return provider.getStore(TELEMETRY_STORE);
11
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { getTelemetryStore, TELEMETRY_STORE, telemetryStoreDefinition } from './definition.js';
2
+ export type { TelemetryTables } from './tables.js';
3
+ export type { StoredSpan, TelemetryStore } from './types.js';
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ export { getTelemetryStore, TELEMETRY_STORE, telemetryStoreDefinition } from './definition.js';
@@ -0,0 +1,3 @@
1
+ import type { MigrationContext } from '@hozon/db';
2
+ import type { Migration } from 'kysely/migration';
3
+ export declare function telemetryStoreMigrations(ctx: MigrationContext): Record<string, Migration>;
@@ -0,0 +1,24 @@
1
+ export function telemetryStoreMigrations(ctx) {
2
+ return {
3
+ '0-init': {
4
+ async up (db) {
5
+ await db.schema.createTable('spans').addColumn('seq', ctx.types.serial, (column)=>{
6
+ const primary = column.primaryKey();
7
+ return ctx.kind === 'sqlite' ? primary.autoIncrement() : primary;
8
+ }).addColumn('trace_id', ctx.types.text, (column)=>column.notNull()).addColumn('span_id', ctx.types.text, (column)=>column.notNull()).addColumn('start_time', ctx.types.double, (column)=>column.notNull()).addColumn('end_time', ctx.types.double, (column)=>column.notNull()).addColumn('data', ctx.types.json, (column)=>column.notNull()).addUniqueConstraint(`${ctx.tablePrefix}_spans_trace_span`, [
9
+ 'trace_id',
10
+ 'span_id'
11
+ ]).execute();
12
+ await db.schema.createIndex(`${ctx.tablePrefix}_spans_trace_start_seq`).on('spans').columns([
13
+ 'trace_id',
14
+ 'start_time',
15
+ 'seq'
16
+ ]).execute();
17
+ await db.schema.createIndex(`${ctx.tablePrefix}_spans_end_time`).on('spans').column('end_time').execute();
18
+ },
19
+ async down (db) {
20
+ await db.schema.dropTable('spans').execute();
21
+ }
22
+ }
23
+ };
24
+ }
@@ -0,0 +1,12 @@
1
+ import type { ColumnType, Generated } from 'kysely';
2
+ import type { StoredSpan } from './types.js';
3
+ export type TelemetryTables = {
4
+ spans: {
5
+ seq: Generated<number>;
6
+ trace_id: string;
7
+ span_id: string;
8
+ start_time: number;
9
+ end_time: number;
10
+ data: ColumnType<StoredSpan, unknown, unknown>;
11
+ };
12
+ };
package/lib/tables.js ADDED
@@ -0,0 +1 @@
1
+ export { };
package/lib/types.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import type { JSONValue } from '@sozai/json';
2
+ export type StoredSpan = {
3
+ traceID: string;
4
+ spanID: string;
5
+ parentSpanID?: string;
6
+ name: string;
7
+ kind: number;
8
+ startTime: number;
9
+ endTime: number;
10
+ status: {
11
+ code: number;
12
+ message?: string;
13
+ };
14
+ attributes: Record<string, JSONValue>;
15
+ events: Array<{
16
+ name: string;
17
+ time: number;
18
+ attributes: Record<string, JSONValue>;
19
+ }>;
20
+ links: Array<{
21
+ traceID: string;
22
+ spanID: string;
23
+ }>;
24
+ };
25
+ export type TelemetryStore = {
26
+ addSpans(spans: Array<StoredSpan>): Promise<void>;
27
+ getSpans(traceID: string): Promise<Array<StoredSpan>>;
28
+ deleteByTrace(traceIDs: Array<string>): Promise<number>;
29
+ /** An empty or omitted keepTraceIDs keeps nothing. */
30
+ deleteBefore(time: number, params?: {
31
+ keepTraceIDs?: Array<string>;
32
+ }): Promise<number>;
33
+ };
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export { };
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@hozon/store-telemetry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Portable span store for Hozon",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/TairuFramework/hozon.git",
8
+ "directory": "packages/store-telemetry"
9
+ },
10
+ "license": "MIT",
11
+ "sideEffects": false,
12
+ "type": "module",
13
+ "exports": {
14
+ ".": "./lib/index.js"
15
+ },
16
+ "files": [
17
+ "lib/*"
18
+ ],
19
+ "dependencies": {
20
+ "@hozon/adapter": "^0.1.0",
21
+ "@hozon/db": "^0.2.0",
22
+ "@sozai/json": "^0.3.0",
23
+ "kysely": "^0.29.6"
24
+ },
25
+ "devDependencies": {
26
+ "@hozon/node-sqlite": "^0.1.0",
27
+ "@types/node": "^26.6.4",
28
+ "del-cli": "^7.0.0",
29
+ "vitest": "5.0.3"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
36
+ "build:clean": "del-cli lib",
37
+ "build:js": "del-cli \"lib/**/*.js\" && swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
38
+ "build:types": "del-cli \"lib/**/*.d.ts\" \"lib/**/*.d.ts.map\" && tsc --emitDeclarationOnly --skipLibCheck",
39
+ "build:types:ci": "tsc --emitDeclarationOnly --skipLibCheck --declarationMap false",
40
+ "test": "pnpm run test:types && pnpm run test:unit",
41
+ "test:types": "tsc --noEmit --skipLibCheck -p tsconfig.test.json",
42
+ "test:unit": "vitest run"
43
+ }
6
44
  }