@tinycloud/sdk-services 2.3.0-beta.6 → 2.3.0-beta.8

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.
@@ -205,6 +205,17 @@ declare const defaultRetryPolicy: RetryPolicy;
205
205
  * Event handler function type.
206
206
  */
207
207
  type EventHandler = (data: unknown) => void;
208
+ /**
209
+ * SDK telemetry event handler.
210
+ */
211
+ type TelemetryEventHandler = (event: string, data: unknown) => void;
212
+ /**
213
+ * Default-off telemetry configuration.
214
+ */
215
+ type TelemetryConfig = boolean | {
216
+ enabled?: boolean;
217
+ onEvent?: TelemetryEventHandler;
218
+ };
208
219
  /**
209
220
  * Service interface - base contract for all services.
210
221
  */
@@ -252,6 +263,7 @@ interface IServiceContext {
252
263
  interface ServiceRequestEvent {
253
264
  service: string;
254
265
  action: string;
266
+ span?: string;
255
267
  key?: string;
256
268
  timestamp: number;
257
269
  }
@@ -261,8 +273,10 @@ interface ServiceRequestEvent {
261
273
  interface ServiceResponseEvent {
262
274
  service: string;
263
275
  action: string;
276
+ span?: string;
264
277
  ok: boolean;
265
278
  duration: number;
279
+ durationMs?: number;
266
280
  status?: number;
267
281
  }
268
282
  /**
@@ -270,6 +284,7 @@ interface ServiceResponseEvent {
270
284
  */
271
285
  interface ServiceErrorEvent {
272
286
  service: string;
287
+ span?: string;
273
288
  error: ServiceError;
274
289
  }
275
290
  /**
@@ -281,10 +296,23 @@ interface ServiceRetryEvent {
281
296
  maxAttempts: number;
282
297
  error: ServiceError;
283
298
  }
299
+ /**
300
+ * Generic named span event for aggregating operation timings.
301
+ */
302
+ interface TelemetrySpanEvent {
303
+ span: string;
304
+ ok: boolean;
305
+ durationMs: number;
306
+ service?: string;
307
+ action?: string;
308
+ status?: number;
309
+ error?: ServiceError;
310
+ }
284
311
  /**
285
312
  * Telemetry event names.
286
313
  */
287
314
  declare const TelemetryEvents: {
315
+ readonly SPAN: "telemetry.span";
288
316
  readonly SERVICE_REQUEST: "service.request";
289
317
  readonly SERVICE_RESPONSE: "service.response";
290
318
  readonly SERVICE_ERROR: "service.error";
@@ -435,12 +463,16 @@ declare abstract class BaseService implements IService {
435
463
  *
436
464
  * @param error - The service error
437
465
  */
438
- protected emitError(error: ServiceError): void;
466
+ protected emitError(error: ServiceError, action?: string): void;
439
467
  /**
440
468
  * Get the service name from the static property.
441
469
  * Subclasses must define static serviceName.
442
470
  */
443
471
  protected getServiceName(): string;
472
+ /**
473
+ * Stable span name used by SDK telemetry sinks.
474
+ */
475
+ protected spanName(action: string): string;
444
476
  /**
445
477
  * Create a combined abort signal from multiple sources.
446
478
  *
@@ -459,4 +491,4 @@ declare abstract class BaseService implements IService {
459
491
  protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
460
492
  }
461
493
 
462
- export { BaseService as B, type ErrorCode as E, type FetchFunction as F, type IServiceContext as I, type RetryPolicy as R, type ServiceSession as S, TelemetryEvents as T, type InvokeFunction as a, type InvokeAnyFunction as b, type IService as c, type ServiceError as d, type Result as e, type StorageQuotaInfo as f, ErrorCodes as g, type EventHandler as h, type FetchRequestInit as i, type FetchResponse as j, type InvocationFact as k, type InvocationFacts as l, type InvokeAnyEntry as m, type ServiceErrorEvent as n, type ServiceHeaders as o, type ServiceRequestEvent as p, type ServiceResponseEvent as q, type ServiceRetryEvent as r, defaultRetryPolicy as s, err as t, ok as u, serviceError as v };
494
+ export { BaseService as B, type EventHandler as E, type FetchFunction as F, type IServiceContext as I, type RetryPolicy as R, type ServiceSession as S, type TelemetryConfig as T, type InvokeFunction as a, type InvokeAnyFunction as b, type IService as c, type ServiceError as d, type Result as e, type StorageQuotaInfo as f, type ErrorCode as g, ErrorCodes as h, type FetchRequestInit as i, type FetchResponse as j, type InvocationFact as k, type InvocationFacts as l, type InvokeAnyEntry as m, type ServiceErrorEvent as n, type ServiceHeaders as o, type ServiceRequestEvent as p, type ServiceResponseEvent as q, type ServiceRetryEvent as r, type TelemetryEventHandler as s, TelemetryEvents as t, type TelemetrySpanEvent as u, defaultRetryPolicy as v, err as w, ok as x, serviceError as y };
@@ -205,6 +205,17 @@ declare const defaultRetryPolicy: RetryPolicy;
205
205
  * Event handler function type.
206
206
  */
207
207
  type EventHandler = (data: unknown) => void;
208
+ /**
209
+ * SDK telemetry event handler.
210
+ */
211
+ type TelemetryEventHandler = (event: string, data: unknown) => void;
212
+ /**
213
+ * Default-off telemetry configuration.
214
+ */
215
+ type TelemetryConfig = boolean | {
216
+ enabled?: boolean;
217
+ onEvent?: TelemetryEventHandler;
218
+ };
208
219
  /**
209
220
  * Service interface - base contract for all services.
210
221
  */
@@ -252,6 +263,7 @@ interface IServiceContext {
252
263
  interface ServiceRequestEvent {
253
264
  service: string;
254
265
  action: string;
266
+ span?: string;
255
267
  key?: string;
256
268
  timestamp: number;
257
269
  }
@@ -261,8 +273,10 @@ interface ServiceRequestEvent {
261
273
  interface ServiceResponseEvent {
262
274
  service: string;
263
275
  action: string;
276
+ span?: string;
264
277
  ok: boolean;
265
278
  duration: number;
279
+ durationMs?: number;
266
280
  status?: number;
267
281
  }
268
282
  /**
@@ -270,6 +284,7 @@ interface ServiceResponseEvent {
270
284
  */
271
285
  interface ServiceErrorEvent {
272
286
  service: string;
287
+ span?: string;
273
288
  error: ServiceError;
274
289
  }
275
290
  /**
@@ -281,10 +296,23 @@ interface ServiceRetryEvent {
281
296
  maxAttempts: number;
282
297
  error: ServiceError;
283
298
  }
299
+ /**
300
+ * Generic named span event for aggregating operation timings.
301
+ */
302
+ interface TelemetrySpanEvent {
303
+ span: string;
304
+ ok: boolean;
305
+ durationMs: number;
306
+ service?: string;
307
+ action?: string;
308
+ status?: number;
309
+ error?: ServiceError;
310
+ }
284
311
  /**
285
312
  * Telemetry event names.
286
313
  */
287
314
  declare const TelemetryEvents: {
315
+ readonly SPAN: "telemetry.span";
288
316
  readonly SERVICE_REQUEST: "service.request";
289
317
  readonly SERVICE_RESPONSE: "service.response";
290
318
  readonly SERVICE_ERROR: "service.error";
@@ -435,12 +463,16 @@ declare abstract class BaseService implements IService {
435
463
  *
436
464
  * @param error - The service error
437
465
  */
438
- protected emitError(error: ServiceError): void;
466
+ protected emitError(error: ServiceError, action?: string): void;
439
467
  /**
440
468
  * Get the service name from the static property.
441
469
  * Subclasses must define static serviceName.
442
470
  */
443
471
  protected getServiceName(): string;
472
+ /**
473
+ * Stable span name used by SDK telemetry sinks.
474
+ */
475
+ protected spanName(action: string): string;
444
476
  /**
445
477
  * Create a combined abort signal from multiple sources.
446
478
  *
@@ -459,4 +491,4 @@ declare abstract class BaseService implements IService {
459
491
  protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
460
492
  }
461
493
 
462
- export { BaseService as B, type ErrorCode as E, type FetchFunction as F, type IServiceContext as I, type RetryPolicy as R, type ServiceSession as S, TelemetryEvents as T, type InvokeFunction as a, type InvokeAnyFunction as b, type IService as c, type ServiceError as d, type Result as e, type StorageQuotaInfo as f, ErrorCodes as g, type EventHandler as h, type FetchRequestInit as i, type FetchResponse as j, type InvocationFact as k, type InvocationFacts as l, type InvokeAnyEntry as m, type ServiceErrorEvent as n, type ServiceHeaders as o, type ServiceRequestEvent as p, type ServiceResponseEvent as q, type ServiceRetryEvent as r, defaultRetryPolicy as s, err as t, ok as u, serviceError as v };
494
+ export { BaseService as B, type EventHandler as E, type FetchFunction as F, type IServiceContext as I, type RetryPolicy as R, type ServiceSession as S, type TelemetryConfig as T, type InvokeFunction as a, type InvokeAnyFunction as b, type IService as c, type ServiceError as d, type Result as e, type StorageQuotaInfo as f, type ErrorCode as g, ErrorCodes as h, type FetchRequestInit as i, type FetchResponse as j, type InvocationFact as k, type InvocationFacts as l, type InvokeAnyEntry as m, type ServiceErrorEvent as n, type ServiceHeaders as o, type ServiceRequestEvent as p, type ServiceResponseEvent as q, type ServiceRetryEvent as r, type TelemetryEventHandler as s, TelemetryEvents as t, type TelemetrySpanEvent as u, defaultRetryPolicy as v, err as w, ok as x, serviceError as y };
@@ -108,6 +108,7 @@ var defaultRetryPolicy = {
108
108
  retryableErrors: [ErrorCodes.NETWORK_ERROR, ErrorCodes.TIMEOUT]
109
109
  };
110
110
  var TelemetryEvents = {
111
+ SPAN: "telemetry.span",
111
112
  SERVICE_REQUEST: "service.request",
112
113
  SERVICE_RESPONSE: "service.response",
113
114
  SERVICE_ERROR: "service.error",
@@ -248,9 +249,11 @@ var BaseService = class {
248
249
  * @param key - Optional key/path being accessed
249
250
  */
250
251
  emitRequest(action, key) {
252
+ const service = this.getServiceName();
251
253
  this.emit(TelemetryEvents.SERVICE_REQUEST, {
252
- service: this.getServiceName(),
254
+ service,
253
255
  action,
256
+ span: this.spanName(action),
254
257
  key,
255
258
  timestamp: Date.now()
256
259
  });
@@ -264,11 +267,24 @@ var BaseService = class {
264
267
  * @param status - Optional HTTP status code
265
268
  */
266
269
  emitResponse(action, ok, startTime, status) {
270
+ const service = this.getServiceName();
271
+ const durationMs = Date.now() - startTime;
272
+ const span = this.spanName(action);
267
273
  this.emit(TelemetryEvents.SERVICE_RESPONSE, {
268
- service: this.getServiceName(),
274
+ service,
275
+ action,
276
+ span,
277
+ ok,
278
+ duration: durationMs,
279
+ durationMs,
280
+ status
281
+ });
282
+ this.emit(TelemetryEvents.SPAN, {
283
+ span,
284
+ service,
269
285
  action,
270
286
  ok,
271
- duration: Date.now() - startTime,
287
+ durationMs,
272
288
  status
273
289
  });
274
290
  }
@@ -277,9 +293,11 @@ var BaseService = class {
277
293
  *
278
294
  * @param error - The service error
279
295
  */
280
- emitError(error) {
296
+ emitError(error, action) {
297
+ const span = action ? this.spanName(action) : void 0;
281
298
  this.emit(TelemetryEvents.SERVICE_ERROR, {
282
299
  service: this.getServiceName(),
300
+ ...span ? { span } : {},
283
301
  error
284
302
  });
285
303
  }
@@ -290,6 +308,12 @@ var BaseService = class {
290
308
  getServiceName() {
291
309
  return this.constructor.serviceName;
292
310
  }
311
+ /**
312
+ * Stable span name used by SDK telemetry sinks.
313
+ */
314
+ spanName(action) {
315
+ return `sdk.${this.getServiceName()}.${action}`;
316
+ }
293
317
  /**
294
318
  * Create a combined abort signal from multiple sources.
295
319
  *
@@ -327,13 +351,13 @@ var BaseService = class {
327
351
  this.emitResponse(action, true, startTime);
328
352
  } else {
329
353
  this.emitResponse(action, false, startTime);
330
- this.emitError(result.error);
354
+ this.emitError(result.error, action);
331
355
  }
332
356
  return result;
333
357
  } catch (error) {
334
358
  const serviceError2 = wrapError(this.getServiceName(), error);
335
359
  this.emitResponse(action, false, startTime);
336
- this.emitError(serviceError2);
360
+ this.emitError(serviceError2, action);
337
361
  return err(serviceError2);
338
362
  }
339
363
  }