@hmharness/observability 0.7.3 → 0.7.4
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/otel.d.ts +64 -0
- package/dist/otel.js +117 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/otel.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - OpenTelemetry-Compatible Traces (P2-06)
|
|
3
|
+
*
|
|
4
|
+
* The audit called for: "跨 Agent / Sandbox / MCP / Device 的统一 trace identity"
|
|
5
|
+
*
|
|
6
|
+
* Provides OTel-compatible span and trace structures so hmharness operations
|
|
7
|
+
* can be exported to any OTel collector (Jaeger, Zipkin, Grafana Tempo).
|
|
8
|
+
*/
|
|
9
|
+
export type SpanKind = 'internal' | 'server' | 'client' | 'producer' | 'consumer';
|
|
10
|
+
export type SpanStatus = 'unset' | 'ok' | 'error';
|
|
11
|
+
export interface OtelSpan {
|
|
12
|
+
traceId: string;
|
|
13
|
+
spanId: string;
|
|
14
|
+
parentSpanId?: string;
|
|
15
|
+
name: string;
|
|
16
|
+
kind: SpanKind;
|
|
17
|
+
startTime: number;
|
|
18
|
+
endTime?: number;
|
|
19
|
+
attributes: Record<string, string | number | boolean>;
|
|
20
|
+
status: SpanStatus;
|
|
21
|
+
statusMessage?: string;
|
|
22
|
+
/** hmharness-specific: which component produced this span */
|
|
23
|
+
component: 'agent' | 'sandbox' | 'mcp' | 'device' | 'evolution' | 'web' | 'cli';
|
|
24
|
+
}
|
|
25
|
+
export interface OtelTrace {
|
|
26
|
+
traceId: string;
|
|
27
|
+
rootSpan: OtelSpan;
|
|
28
|
+
spans: OtelSpan[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generate a valid OTel trace ID (32 hex chars).
|
|
32
|
+
* Pure - testable.
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateTraceId(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Generate a valid OTel span ID (16 hex chars).
|
|
37
|
+
* Pure - testable.
|
|
38
|
+
*/
|
|
39
|
+
export declare function generateSpanId(): string;
|
|
40
|
+
/**
|
|
41
|
+
* Create a new span.
|
|
42
|
+
* Pure - testable.
|
|
43
|
+
*/
|
|
44
|
+
export declare function createSpan(name: string, component: OtelSpan['component'], kind?: SpanKind, parent?: OtelSpan, attributes?: Record<string, string | number | boolean>): OtelSpan;
|
|
45
|
+
/**
|
|
46
|
+
* Finish a span (sets endTime and status).
|
|
47
|
+
* Pure - testable.
|
|
48
|
+
*/
|
|
49
|
+
export declare function finishSpan(span: OtelSpan, status?: SpanStatus, message?: string): OtelSpan;
|
|
50
|
+
/**
|
|
51
|
+
* Build a complete trace from spans.
|
|
52
|
+
* Pure - testable.
|
|
53
|
+
*/
|
|
54
|
+
export declare function buildTrace(spans: OtelSpan[]): OtelTrace | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Export spans in OTLP JSON format (what OTel collectors accept).
|
|
57
|
+
* Pure - testable.
|
|
58
|
+
*/
|
|
59
|
+
export declare function toOtlpJson(spans: OtelSpan[]): string;
|
|
60
|
+
/**
|
|
61
|
+
* Format a trace as a readable tree.
|
|
62
|
+
* Pure - testable.
|
|
63
|
+
*/
|
|
64
|
+
export declare function formatTrace(trace: OtelTrace): string;
|
package/dist/otel.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - OpenTelemetry-Compatible Traces (P2-06)
|
|
3
|
+
*
|
|
4
|
+
* The audit called for: "跨 Agent / Sandbox / MCP / Device 的统一 trace identity"
|
|
5
|
+
*
|
|
6
|
+
* Provides OTel-compatible span and trace structures so hmharness operations
|
|
7
|
+
* can be exported to any OTel collector (Jaeger, Zipkin, Grafana Tempo).
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Generate a valid OTel trace ID (32 hex chars).
|
|
11
|
+
* Pure - testable.
|
|
12
|
+
*/
|
|
13
|
+
export function generateTraceId() {
|
|
14
|
+
let id = '';
|
|
15
|
+
for (let i = 0; i < 32; i++)
|
|
16
|
+
id += Math.floor(Math.random() * 16).toString(16);
|
|
17
|
+
return id;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Generate a valid OTel span ID (16 hex chars).
|
|
21
|
+
* Pure - testable.
|
|
22
|
+
*/
|
|
23
|
+
export function generateSpanId() {
|
|
24
|
+
let id = '';
|
|
25
|
+
for (let i = 0; i < 16; i++)
|
|
26
|
+
id += Math.floor(Math.random() * 16).toString(16);
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a new span.
|
|
31
|
+
* Pure - testable.
|
|
32
|
+
*/
|
|
33
|
+
export function createSpan(name, component, kind = 'internal', parent, attributes = {}) {
|
|
34
|
+
return {
|
|
35
|
+
traceId: parent?.traceId ?? generateTraceId(),
|
|
36
|
+
spanId: generateSpanId(),
|
|
37
|
+
parentSpanId: parent?.spanId,
|
|
38
|
+
name, kind, component, attributes,
|
|
39
|
+
startTime: Date.now(),
|
|
40
|
+
status: 'unset',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Finish a span (sets endTime and status).
|
|
45
|
+
* Pure - testable.
|
|
46
|
+
*/
|
|
47
|
+
export function finishSpan(span, status = 'ok', message) {
|
|
48
|
+
return {
|
|
49
|
+
...span,
|
|
50
|
+
endTime: Date.now(),
|
|
51
|
+
status,
|
|
52
|
+
...(message ? { statusMessage: message } : {}),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a complete trace from spans.
|
|
57
|
+
* Pure - testable.
|
|
58
|
+
*/
|
|
59
|
+
export function buildTrace(spans) {
|
|
60
|
+
if (spans.length === 0)
|
|
61
|
+
return undefined;
|
|
62
|
+
const traceId = spans[0].traceId;
|
|
63
|
+
if (!spans.every(s => s.traceId === traceId))
|
|
64
|
+
return undefined;
|
|
65
|
+
const root = spans.find(s => !s.parentSpanId) ?? spans[0];
|
|
66
|
+
return { traceId, rootSpan: root, spans };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Export spans in OTLP JSON format (what OTel collectors accept).
|
|
70
|
+
* Pure - testable.
|
|
71
|
+
*/
|
|
72
|
+
export function toOtlpJson(spans) {
|
|
73
|
+
return JSON.stringify({
|
|
74
|
+
resourceSpans: [{
|
|
75
|
+
resource: { attributes: [{ key: 'service.name', value: { stringValue: 'hmharness' } }] },
|
|
76
|
+
scopeSpans: [{
|
|
77
|
+
spans: spans.map(s => ({
|
|
78
|
+
traceId: s.traceId,
|
|
79
|
+
spanId: s.spanId,
|
|
80
|
+
parentSpanId: s.parentSpanId,
|
|
81
|
+
name: s.name,
|
|
82
|
+
kind: s.kind,
|
|
83
|
+
startTimeUnixNano: String(s.startTime * 1_000_000),
|
|
84
|
+
endTimeUnixNano: s.endTime ? String(s.endTime * 1_000_000) : undefined,
|
|
85
|
+
attributes: Object.entries(s.attributes).map(([k, v]) => ({
|
|
86
|
+
key: k,
|
|
87
|
+
value: typeof v === 'string' ? { stringValue: v } : typeof v === 'number' ? { doubleValue: v } : { boolValue: v },
|
|
88
|
+
})),
|
|
89
|
+
status: { code: s.status === 'ok' ? 1 : s.status === 'error' ? 2 : 0 },
|
|
90
|
+
})),
|
|
91
|
+
}],
|
|
92
|
+
}],
|
|
93
|
+
}, null, 2);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Format a trace as a readable tree.
|
|
97
|
+
* Pure - testable.
|
|
98
|
+
*/
|
|
99
|
+
export function formatTrace(trace) {
|
|
100
|
+
const lines = [`Trace ${trace.traceId.slice(0, 16)}... (${trace.spans.length} spans)`];
|
|
101
|
+
const byParent = new Map();
|
|
102
|
+
for (const s of trace.spans) {
|
|
103
|
+
const key = s.parentSpanId;
|
|
104
|
+
if (!byParent.has(key))
|
|
105
|
+
byParent.set(key, []);
|
|
106
|
+
byParent.get(key).push(s);
|
|
107
|
+
}
|
|
108
|
+
function render(span, indent) {
|
|
109
|
+
const dur = span.endTime ? `${span.endTime - span.startTime}ms` : '...';
|
|
110
|
+
const status = span.status === 'error' ? '❌' : span.status === 'ok' ? '✅' : '⏳';
|
|
111
|
+
lines.push(`${' '.repeat(indent)}${status} ${span.name} [${span.component}] ${dur}`);
|
|
112
|
+
for (const child of byParent.get(span.spanId) ?? [])
|
|
113
|
+
render(child, indent + 1);
|
|
114
|
+
}
|
|
115
|
+
render(trace.rootSpan, 0);
|
|
116
|
+
return lines.join('\n');
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/observability",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "hmharness observability: typed RunEvent schema + append-only TrajectoryStore. Every agent run becomes a reproducible, comparable, exportable experiment sample (V2 blueprint M1 - flight recorder). Zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|