@deepseek-ai/dsh-session-telemetry-otel 0.2.3-alpha.2 → 0.2.3-alpha.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/README.i18n.yaml +13 -10
- package/README.md +31 -3
- package/README.zh.md +31 -3
- package/lib/index.js +270 -16
- package/lib/types/batch.d.ts +88 -0
- package/lib/types/batch.d.ts.map +1 -0
- package/lib/types/batch.js.map +1 -0
- package/lib/types/index.d.ts +25 -7
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/record-format.d.ts +11 -0
- package/lib/types/record-format.d.ts.map +1 -0
- package/lib/types/record-format.js.map +1 -0
- package/package.json +28 -28
- package/src/batch.ts +224 -0
- package/src/index.ts +159 -12
- package/src/record-format.ts +12 -0
package/src/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenTelemetry Service Provider for the DeepSeek Harness telemetry capability.
|
|
3
3
|
*
|
|
4
|
-
* Authorizes feedback-bounded capture and hands
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Authorizes feedback-bounded capture and hands records to the Session-log
|
|
5
|
+
* reporter, either one OTLP record per event or one per contiguous delivery
|
|
6
|
+
* batch. This plugin owns resource identity, optional batch assembly, and an
|
|
7
|
+
* outer shutdown deadline; the reporter owns byte-bounded SDK delivery.
|
|
7
8
|
*
|
|
8
9
|
* @module @deepseek-ai/dsh-session-telemetry-otel
|
|
9
10
|
*/
|
|
@@ -25,7 +26,22 @@ import {
|
|
|
25
26
|
import { APP_IDENTITY } from '@deepseek-ai/dsh-llm'
|
|
26
27
|
import { getOrCreateAnonymousUserId } from '@deepseek-ai/dsh-anonymous-user-id'
|
|
27
28
|
import type { LogExporterOptions, SessionLogProcessorOptions, SessionLogReporter } from '@deepseek-ai/dsh-otel'
|
|
29
|
+
import { DEFAULT_RECORD_FORMAT, type SessionLogRecordFormat } from './record-format.ts'
|
|
28
30
|
import { SeverityNumber } from '@opentelemetry/api-logs'
|
|
31
|
+
import {
|
|
32
|
+
DEFAULT_BATCH_DELAY_MILLIS,
|
|
33
|
+
DEFAULT_MAX_BATCH_BYTES,
|
|
34
|
+
DEFAULT_MAX_BATCH_EVENTS,
|
|
35
|
+
SessionLogBatcher,
|
|
36
|
+
type SessionLogBatchOptions,
|
|
37
|
+
} from './batch.ts'
|
|
38
|
+
|
|
39
|
+
/** Request ceiling the OTel channel enforces, mirrored here for load-time validation. */
|
|
40
|
+
const MAX_REQUEST_BYTES = 4_000_000
|
|
41
|
+
/** Two-way JSON escaping can double a batch body on the wire. */
|
|
42
|
+
const BATCH_BODY_ESCAPE_FACTOR = 2
|
|
43
|
+
/** Fixed OTLP resource/scope envelope allowance when a batch body is escaped. */
|
|
44
|
+
const BATCH_ENVELOPE_MARGIN_BYTES = 4_096
|
|
29
45
|
|
|
30
46
|
/** Session-sharing policy selected by {@link Config.mode}. */
|
|
31
47
|
export enum SessionTelemetryMode {
|
|
@@ -50,6 +66,39 @@ function isFeedback(session: Session, event: SessionEvent): boolean {
|
|
|
50
66
|
}
|
|
51
67
|
}
|
|
52
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Session format generation carried by a ledger record's identity attributes.
|
|
71
|
+
* Absence identifies generation 0, matching the accepted-upload watermark rule.
|
|
72
|
+
* @param attributes - the captured record's identity attributes.
|
|
73
|
+
* @returns the generation the event sequence belongs to.
|
|
74
|
+
*/
|
|
75
|
+
function formatVersionOf(attributes: Record<string, string | number>): number {
|
|
76
|
+
const version = attributes['session.format_version']
|
|
77
|
+
return typeof version === 'number' ? version : 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Session-level facts a batch reports once instead of per event. They are the
|
|
82
|
+
* same identity attributes the per-event path carries, and the coordinator
|
|
83
|
+
* derives them from the Session header.
|
|
84
|
+
* @param attributes - the captured record's identity attributes.
|
|
85
|
+
* @returns the present Session facts, omitted when the header has none.
|
|
86
|
+
*/
|
|
87
|
+
function sessionDimensionsOf(attributes: Record<string, string | number>): {
|
|
88
|
+
cwd?: string
|
|
89
|
+
parentSessionId?: string
|
|
90
|
+
seedLength?: number
|
|
91
|
+
} {
|
|
92
|
+
const cwd = attributes['session.cwd']
|
|
93
|
+
const parentSessionId = attributes['session.parent_id']
|
|
94
|
+
const seedLength = attributes['session.seed_length']
|
|
95
|
+
return {
|
|
96
|
+
...typeof cwd === 'string' ? { cwd } : {},
|
|
97
|
+
...typeof parentSessionId === 'string' ? { parentSessionId } : {},
|
|
98
|
+
...typeof seedLength === 'number' ? { seedLength } : {},
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
53
102
|
/** Resolve the default and reject unknown runtime values before transport setup. */
|
|
54
103
|
function resolveMode(mode: SessionTelemetryMode | undefined): SessionTelemetryMode {
|
|
55
104
|
const resolved = mode ?? DEFAULT_TELEMETRY_MODE
|
|
@@ -58,13 +107,59 @@ function resolveMode(mode: SessionTelemetryMode | undefined): SessionTelemetryMo
|
|
|
58
107
|
case SessionTelemetryMode.DISABLED:
|
|
59
108
|
return resolved
|
|
60
109
|
default:
|
|
61
|
-
return assertNever(resolved)
|
|
110
|
+
return assertNever(resolved, 'mode')
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Resolve the default record granularity and reject unknown runtime values before transport setup. */
|
|
115
|
+
function resolveRecordFormat(format: SessionLogRecordFormat | undefined): SessionLogRecordFormat {
|
|
116
|
+
const resolved = format ?? DEFAULT_RECORD_FORMAT
|
|
117
|
+
switch (resolved) {
|
|
118
|
+
case 'per-event':
|
|
119
|
+
case 'per-batch':
|
|
120
|
+
return resolved
|
|
121
|
+
default:
|
|
122
|
+
return assertNever(resolved, 'record format')
|
|
62
123
|
}
|
|
63
124
|
}
|
|
64
125
|
|
|
65
126
|
/** Fail closed when direct construction bypasses the runtime config schema. */
|
|
66
|
-
function assertNever(value: never): never {
|
|
67
|
-
throw new Error(`session-telemetry-otel: unsupported
|
|
127
|
+
function assertNever(value: never, what: string): never {
|
|
128
|
+
throw new Error(`session-telemetry-otel: unsupported ${what} ${JSON.stringify(value)}`)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Validate the batch ceilings against each other and against the request ceiling.
|
|
133
|
+
*
|
|
134
|
+
* A batch body travels as one JSON string value, so escaping can double its
|
|
135
|
+
* bytes; a batch that fits maxBatchBytes but not maxRequestBytes after escaping
|
|
136
|
+
* would be rejected whole, losing every event it carries. Rejecting that
|
|
137
|
+
* combination at load time keeps a legal configuration from discarding events
|
|
138
|
+
* at run time.
|
|
139
|
+
*/
|
|
140
|
+
function resolveBatchOptions(config: Config): SessionLogBatchOptions {
|
|
141
|
+
const maxBatchEvents = config.maxBatchEvents ?? DEFAULT_MAX_BATCH_EVENTS
|
|
142
|
+
const maxBatchBytes = config.maxBatchBytes ?? DEFAULT_MAX_BATCH_BYTES
|
|
143
|
+
const batchDelayMillis = config.batchDelayMillis ?? DEFAULT_BATCH_DELAY_MILLIS
|
|
144
|
+
if (!Number.isSafeInteger(maxBatchEvents) || maxBatchEvents < 1) {
|
|
145
|
+
throw new Error(`session-telemetry-otel: maxBatchEvents must be a positive integer, got ${String(maxBatchEvents)}`)
|
|
146
|
+
}
|
|
147
|
+
if (!Number.isSafeInteger(maxBatchBytes) || maxBatchBytes < 1) {
|
|
148
|
+
throw new Error(`session-telemetry-otel: maxBatchBytes must be a positive integer, got ${String(maxBatchBytes)}`)
|
|
149
|
+
}
|
|
150
|
+
if (!Number.isSafeInteger(batchDelayMillis) || batchDelayMillis < 1 || batchDelayMillis > MAX_TIMER_DELAY_MILLIS) {
|
|
151
|
+
throw new Error(`session-telemetry-otel: batchDelayMillis must be a positive integer no greater than ${MAX_TIMER_DELAY_MILLIS}, got ${String(batchDelayMillis)}`)
|
|
152
|
+
}
|
|
153
|
+
const maxRequestBytes = config.maxRequestBytes ?? MAX_REQUEST_BYTES
|
|
154
|
+
if (maxBatchBytes * BATCH_BODY_ESCAPE_FACTOR + BATCH_ENVELOPE_MARGIN_BYTES > maxRequestBytes) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
'session-telemetry-otel: maxBatchBytes ' + String(maxBatchBytes)
|
|
157
|
+
+ ' cannot fit maxRequestBytes ' + String(maxRequestBytes)
|
|
158
|
+
+ ' after JSON escaping (x' + String(BATCH_BODY_ESCAPE_FACTOR)
|
|
159
|
+
+ ' plus ' + String(BATCH_ENVELOPE_MARGIN_BYTES) + ' B envelope); lower maxBatchBytes',
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
return { maxBatchEvents, maxBatchBytes, batchDelayMillis }
|
|
68
163
|
}
|
|
69
164
|
|
|
70
165
|
/** Map the serialized mode onto the seam's backend-independent sharing vocabulary. */
|
|
@@ -73,7 +168,7 @@ function sharingStatusFor(mode: SessionTelemetryMode): SessionTelemetrySharingSt
|
|
|
73
168
|
case SessionTelemetryMode.FEEDBACK_ONLY: return 'feedback-only'
|
|
74
169
|
case SessionTelemetryMode.DISABLED: return 'disabled'
|
|
75
170
|
/* v8 ignore next 2 -- resolveMode already rejected unknown values before this switch; the closed enum cannot reach the default. */
|
|
76
|
-
default: return assertNever(mode)
|
|
171
|
+
default: return assertNever(mode, 'mode')
|
|
77
172
|
}
|
|
78
173
|
}
|
|
79
174
|
|
|
@@ -104,14 +199,27 @@ export interface Config {
|
|
|
104
199
|
shutdownTimeoutMillis?: number
|
|
105
200
|
/** Uncompressed OTLP request byte limit, at most 4,000,000. */
|
|
106
201
|
maxRequestBytes?: number
|
|
202
|
+
/**
|
|
203
|
+
* OTLP record granularity. `per-event` (default) reports one record per
|
|
204
|
+
* canonical Session event, preserving the existing collector format;
|
|
205
|
+
* `per-batch` reports one record per contiguous Session sequence range with
|
|
206
|
+
* a `delivery.*` attribute set and an `events` array body.
|
|
207
|
+
*/
|
|
208
|
+
recordFormat?: SessionLogRecordFormat
|
|
209
|
+
/** `per-batch` only: maximum events per record. Defaults to 128. */
|
|
210
|
+
maxBatchEvents?: number
|
|
211
|
+
/** `per-batch` only: maximum record body JSON bytes; a larger single event still travels as its own batch. Defaults to 1,048,576. */
|
|
212
|
+
maxBatchBytes?: number
|
|
213
|
+
/** `per-batch` only: maximum age of the oldest buffered event before a record is emitted. Defaults to 1,000. */
|
|
214
|
+
batchDelayMillis?: number
|
|
107
215
|
}
|
|
108
216
|
|
|
109
217
|
/**
|
|
110
218
|
* Schemastery validator for {@link Config}; cordis runs it before the plugin
|
|
111
|
-
* starts. The constructor validates endpoint
|
|
112
|
-
* reporter validates Session byte and queue limits. SDK
|
|
113
|
-
* processor settings use the reporter's accepted option types,
|
|
114
|
-
* self-observability metering.
|
|
219
|
+
* starts. The constructor validates endpoint, shutdown, and batch-ceiling
|
|
220
|
+
* requirements; the reporter validates Session byte and queue limits. SDK
|
|
221
|
+
* transport and processor settings use the reporter's accepted option types,
|
|
222
|
+
* which exclude self-observability metering.
|
|
115
223
|
*/
|
|
116
224
|
export const Config: z<Config> = z.object({
|
|
117
225
|
mode: z.union(Object.values(SessionTelemetryMode)).default(DEFAULT_TELEMETRY_MODE),
|
|
@@ -119,6 +227,10 @@ export const Config: z<Config> = z.object({
|
|
|
119
227
|
processor: z.any(),
|
|
120
228
|
shutdownTimeoutMillis: z.number(),
|
|
121
229
|
maxRequestBytes: z.number().step(1).min(1).max(4_000_000),
|
|
230
|
+
recordFormat: z.union(['per-event', 'per-batch'] as const).default(DEFAULT_RECORD_FORMAT),
|
|
231
|
+
maxBatchEvents: z.number().step(1).min(1),
|
|
232
|
+
maxBatchBytes: z.number().step(1).min(1),
|
|
233
|
+
batchDelayMillis: z.number().step(1).min(1).max(2_147_483_647),
|
|
122
234
|
})
|
|
123
235
|
|
|
124
236
|
/** Default outer allowance for the SDK's complete shutdown sequence. */
|
|
@@ -140,21 +252,26 @@ const SEVERITY: Record<SessionTelemetrySeverity, SeverityNumber> = {
|
|
|
140
252
|
* the `sessionTelemetry` service (duplicate load throws). `FEEDBACK_ONLY` wires the SDK
|
|
141
253
|
* pipeline and on-demand {@link SessionTelemetryCoordinator}; `DISABLED` constructs no
|
|
142
254
|
* SDK state and listens only to warn when recorded feedback stays local.
|
|
255
|
+
* `recordFormat: 'per-batch'` assembles contiguous delivery batches before
|
|
256
|
+
* the same byte-bounded channel.
|
|
143
257
|
*/
|
|
144
258
|
export class OpenTelemetrySessionBackend extends SessionTelemetryBackend {
|
|
145
259
|
static inject = ['sessions', 'otel']
|
|
146
260
|
static Config = Config
|
|
147
261
|
|
|
148
262
|
private readonly provider: SessionLogReporter | undefined
|
|
263
|
+
private readonly batcher: SessionLogBatcher | undefined
|
|
149
264
|
private readonly shutdownTimeoutMillis: number
|
|
150
265
|
override readonly sharing: SessionTelemetrySharingStatus
|
|
151
266
|
|
|
152
267
|
constructor(ctx: Context, config: Config) {
|
|
153
268
|
const mode = resolveMode(config.mode)
|
|
269
|
+
const recordFormat = resolveRecordFormat(config.recordFormat)
|
|
154
270
|
super(ctx)
|
|
155
271
|
this.sharing = sharingStatusFor(mode)
|
|
156
272
|
if (mode === SessionTelemetryMode.DISABLED) {
|
|
157
273
|
this.provider = undefined
|
|
274
|
+
this.batcher = undefined
|
|
158
275
|
this.shutdownTimeoutMillis = DEFAULT_SHUTDOWN_TIMEOUT_MILLIS
|
|
159
276
|
ctx.on('session/event', (session, event) => {
|
|
160
277
|
if (isFeedback(session, event)) ctx.logger.warn(DISABLED_FEEDBACK_WARNING)
|
|
@@ -186,6 +303,7 @@ export class OpenTelemetrySessionBackend extends SessionTelemetryBackend {
|
|
|
186
303
|
throw new Error(`session-telemetry-otel: shutdownTimeoutMillis must be a positive finite number no greater than ${MAX_TIMER_DELAY_MILLIS}, got ${String(shutdownTimeoutMillis)}`)
|
|
187
304
|
}
|
|
188
305
|
this.shutdownTimeoutMillis = shutdownTimeoutMillis
|
|
306
|
+
const batchOptions = recordFormat === 'per-batch' ? resolveBatchOptions(config) : undefined
|
|
189
307
|
const { version } = createRequire(import.meta.url)('../package.json') as { version: string }
|
|
190
308
|
const reporter = ctx.otel.createSessionLogReporter({
|
|
191
309
|
scope: { name: '@deepseek-ai/dsh-session-telemetry-otel', version },
|
|
@@ -200,14 +318,33 @@ export class OpenTelemetrySessionBackend extends SessionTelemetryBackend {
|
|
|
200
318
|
onFailure: (message, error) => { ctx.logger.warn(message, error) },
|
|
201
319
|
})
|
|
202
320
|
this.provider = reporter
|
|
321
|
+
const batcher = batchOptions === undefined
|
|
322
|
+
? undefined
|
|
323
|
+
: new SessionLogBatcher(
|
|
324
|
+
batchOptions,
|
|
325
|
+
(batch) => { reporter.reportSessionLogBatch(batch) },
|
|
326
|
+
(message) => { ctx.logger.warn(message) },
|
|
327
|
+
)
|
|
328
|
+
this.batcher = batcher
|
|
203
329
|
const enqueue: SessionTelemetrySink['emit'] = (record) => {
|
|
204
330
|
if (record.sourceEvent === undefined) {
|
|
205
331
|
ctx.logger.warn('Session log record withheld: redaction removed sourceEvent')
|
|
206
332
|
return
|
|
207
333
|
}
|
|
334
|
+
const event = { ...record.sourceEvent.envelope, data: record.body }
|
|
335
|
+
if (batcher !== undefined) {
|
|
336
|
+
batcher.push({
|
|
337
|
+
sessionId: record.sourceEvent.sessionId,
|
|
338
|
+
formatVersion: formatVersionOf(record.attributes),
|
|
339
|
+
...sessionDimensionsOf(record.attributes),
|
|
340
|
+
event,
|
|
341
|
+
severityNumber: SEVERITY[record.severity],
|
|
342
|
+
})
|
|
343
|
+
return
|
|
344
|
+
}
|
|
208
345
|
reporter.reportSessionLog({
|
|
209
346
|
sessionId: record.sourceEvent.sessionId,
|
|
210
|
-
event
|
|
347
|
+
event,
|
|
211
348
|
severityNumber: SEVERITY[record.severity],
|
|
212
349
|
attributes: record.attributes,
|
|
213
350
|
})
|
|
@@ -241,6 +378,15 @@ export class OpenTelemetrySessionBackend extends SessionTelemetryBackend {
|
|
|
241
378
|
// fromRestore appends a lifecycle marker that this submission did not commit.
|
|
242
379
|
if (isFeedback(session, committed)) coordinator.captureSession(session, committed.seq)
|
|
243
380
|
})
|
|
381
|
+
if (batcher !== undefined) {
|
|
382
|
+
// A finished turn is the Session's idle boundary; a disposal closes it for good.
|
|
383
|
+
ctx.on('session/event', (session, event) => {
|
|
384
|
+
if (event.type === 'turn/end') batcher.flushSession(session.id)
|
|
385
|
+
})
|
|
386
|
+
ctx.on('session/disposed', (session) => {
|
|
387
|
+
batcher.closeSession(session.id)
|
|
388
|
+
})
|
|
389
|
+
}
|
|
244
390
|
}
|
|
245
391
|
|
|
246
392
|
/**
|
|
@@ -258,6 +404,7 @@ export class OpenTelemetrySessionBackend extends SessionTelemetryBackend {
|
|
|
258
404
|
*/
|
|
259
405
|
async shutdown(): Promise<void> {
|
|
260
406
|
if (this.provider === undefined) return
|
|
407
|
+
this.batcher?.shutdown()
|
|
261
408
|
const providerShutdown = this.provider.shutdown()
|
|
262
409
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
263
410
|
const deadline = new Promise<never>((_resolve, reject) => {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OTLP record granularity for Session-log delivery, kept outside the plugin
|
|
3
|
+
* entry because a plugin entry exports only its plugin surface.
|
|
4
|
+
*
|
|
5
|
+
* @module @deepseek-ai/dsh-session-telemetry-otel/record-format
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** OTLP record granularity selected by the recordFormat Config field. */
|
|
9
|
+
export type SessionLogRecordFormat = 'per-event' | 'per-batch'
|
|
10
|
+
|
|
11
|
+
/** Default granularity: one OTLP record per canonical Session event. */
|
|
12
|
+
export const DEFAULT_RECORD_FORMAT: SessionLogRecordFormat = 'per-event'
|