@tinycloud/sdk-services 2.3.0-beta.7 → 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.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, c as IService, d as ServiceError, e as Result, B as BaseService, f as StorageQuotaInfo } from './BaseService-DZ2hBJeD.cjs';
2
- export { E as ErrorCode, g as ErrorCodes, h as EventHandler, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, T as TelemetryEvents, s as defaultRetryPolicy, t as err, u as ok, v as serviceError } from './BaseService-DZ2hBJeD.cjs';
1
+ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as ServiceError, e as Result, B as BaseService, f as StorageQuotaInfo } from './BaseService-BdRvNm2s.cjs';
2
+ export { g as ErrorCode, h as ErrorCodes, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, s as TelemetryEventHandler, t as TelemetryEvents, u as TelemetrySpanEvent, v as defaultRetryPolicy, w as err, x as ok, y as serviceError } from './BaseService-BdRvNm2s.cjs';
3
3
  import { z } from 'zod';
4
4
  import { IKVService } from './kv/index.cjs';
5
5
  export { DEFAULT_SIGNED_READ_URL_EXPIRY_MS, IPrefixedKVService, KVAction, KVActionType, KVBatchPutItem, KVBatchPutOptions, KVBatchPutResponse, KVCreateSignedReadUrlOptions, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, KVSignedReadUrlResponse, PrefixedKVService } from './kv/index.cjs';
@@ -416,17 +416,20 @@ type KVListResultType = z.infer<typeof KVListResultSchema>;
416
416
  declare const ServiceRequestEventSchema: z.ZodObject<{
417
417
  service: z.ZodString;
418
418
  action: z.ZodString;
419
+ span: z.ZodOptional<z.ZodString>;
419
420
  key: z.ZodOptional<z.ZodString>;
420
421
  timestamp: z.ZodNumber;
421
422
  }, "strip", z.ZodTypeAny, {
422
423
  service: string;
423
424
  action: string;
424
425
  timestamp: number;
426
+ span?: string | undefined;
425
427
  key?: string | undefined;
426
428
  }, {
427
429
  service: string;
428
430
  action: string;
429
431
  timestamp: number;
432
+ span?: string | undefined;
430
433
  key?: string | undefined;
431
434
  }>;
432
435
  type ServiceRequestEventType = z.infer<typeof ServiceRequestEventSchema>;
@@ -436,8 +439,10 @@ type ServiceRequestEventType = z.infer<typeof ServiceRequestEventSchema>;
436
439
  declare const ServiceResponseEventSchema: z.ZodObject<{
437
440
  service: z.ZodString;
438
441
  action: z.ZodString;
442
+ span: z.ZodOptional<z.ZodString>;
439
443
  ok: z.ZodBoolean;
440
444
  duration: z.ZodNumber;
445
+ durationMs: z.ZodOptional<z.ZodNumber>;
441
446
  status: z.ZodOptional<z.ZodNumber>;
442
447
  }, "strip", z.ZodTypeAny, {
443
448
  service: string;
@@ -445,12 +450,16 @@ declare const ServiceResponseEventSchema: z.ZodObject<{
445
450
  action: string;
446
451
  duration: number;
447
452
  status?: number | undefined;
453
+ span?: string | undefined;
454
+ durationMs?: number | undefined;
448
455
  }, {
449
456
  service: string;
450
457
  ok: boolean;
451
458
  action: string;
452
459
  duration: number;
453
460
  status?: number | undefined;
461
+ span?: string | undefined;
462
+ durationMs?: number | undefined;
454
463
  }>;
455
464
  type ServiceResponseEventType = z.infer<typeof ServiceResponseEventSchema>;
456
465
  /**
@@ -458,6 +467,7 @@ type ServiceResponseEventType = z.infer<typeof ServiceResponseEventSchema>;
458
467
  */
459
468
  declare const ServiceErrorEventSchema: z.ZodObject<{
460
469
  service: z.ZodString;
470
+ span: z.ZodOptional<z.ZodString>;
461
471
  error: z.ZodObject<{
462
472
  /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
463
473
  code: z.ZodString;
@@ -491,6 +501,7 @@ declare const ServiceErrorEventSchema: z.ZodObject<{
491
501
  meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
492
502
  };
493
503
  service: string;
504
+ span?: string | undefined;
494
505
  }, {
495
506
  error: {
496
507
  code: string;
@@ -500,6 +511,7 @@ declare const ServiceErrorEventSchema: z.ZodObject<{
500
511
  meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
501
512
  };
502
513
  service: string;
514
+ span?: string | undefined;
503
515
  }>;
504
516
  type ServiceErrorEventType = z.infer<typeof ServiceErrorEventSchema>;
505
517
  /**
@@ -557,6 +569,70 @@ declare const ServiceRetryEventSchema: z.ZodObject<{
557
569
  maxAttempts: number;
558
570
  }>;
559
571
  type ServiceRetryEventType = z.infer<typeof ServiceRetryEventSchema>;
572
+ /**
573
+ * Schema for generic named span event.
574
+ */
575
+ declare const TelemetrySpanEventSchema: z.ZodObject<{
576
+ span: z.ZodString;
577
+ ok: z.ZodBoolean;
578
+ durationMs: z.ZodNumber;
579
+ service: z.ZodOptional<z.ZodString>;
580
+ action: z.ZodOptional<z.ZodString>;
581
+ status: z.ZodOptional<z.ZodNumber>;
582
+ error: z.ZodOptional<z.ZodObject<{
583
+ /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
584
+ code: z.ZodString;
585
+ /** Human-readable error message */
586
+ message: z.ZodString;
587
+ /** Service that produced the error (e.g., 'kv', 'sql') */
588
+ service: z.ZodString;
589
+ /** Original error if this wraps another error - not validated since Error is a class */
590
+ cause: z.ZodOptional<z.ZodUnknown>;
591
+ /** Additional metadata about the error - passthrough allows any object properties */
592
+ meta: z.ZodOptional<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>>;
593
+ }, "strip", z.ZodTypeAny, {
594
+ code: string;
595
+ message: string;
596
+ service: string;
597
+ cause?: unknown;
598
+ meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
599
+ }, {
600
+ code: string;
601
+ message: string;
602
+ service: string;
603
+ cause?: unknown;
604
+ meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
605
+ }>>;
606
+ }, "strip", z.ZodTypeAny, {
607
+ ok: boolean;
608
+ span: string;
609
+ durationMs: number;
610
+ error?: {
611
+ code: string;
612
+ message: string;
613
+ service: string;
614
+ cause?: unknown;
615
+ meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
616
+ } | undefined;
617
+ service?: string | undefined;
618
+ status?: number | undefined;
619
+ action?: string | undefined;
620
+ }, {
621
+ ok: boolean;
622
+ span: string;
623
+ durationMs: number;
624
+ error?: {
625
+ code: string;
626
+ message: string;
627
+ service: string;
628
+ cause?: unknown;
629
+ meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
630
+ } | undefined;
631
+ service?: string | undefined;
632
+ status?: number | undefined;
633
+ action?: string | undefined;
634
+ }>;
635
+ type TelemetrySpanEventType = z.infer<typeof TelemetrySpanEventSchema>;
560
636
  /**
561
637
  * Schema for retry policy configuration.
562
638
  */
@@ -718,16 +794,25 @@ declare function validateServiceResponseEvent(data: unknown): {
718
794
  ok: false;
719
795
  error: ValidationError;
720
796
  };
797
+ /**
798
+ * Validate named telemetry span event against the schema.
799
+ *
800
+ * @param data - Unknown data to validate
801
+ * @returns Result with validated data or validation error
802
+ */
803
+ declare function validateTelemetrySpanEvent(data: unknown): {
804
+ ok: true;
805
+ data: TelemetrySpanEventType;
806
+ } | {
807
+ ok: false;
808
+ error: ValidationError;
809
+ };
721
810
 
722
811
  /**
723
812
  * ServiceContext implementation for TinyCloud SDK Services
724
813
  * @module @tinycloud/sdk-services
725
814
  */
726
815
 
727
- /**
728
- * Event handler type for telemetry events.
729
- */
730
- type EventHandler = (data: unknown) => void;
731
816
  /**
732
817
  * Configuration options for ServiceContext.
733
818
  */
@@ -744,6 +829,8 @@ interface ServiceContextConfig {
744
829
  session?: ServiceSession | null;
745
830
  /** Retry policy configuration */
746
831
  retryPolicy?: Partial<RetryPolicy>;
832
+ /** Default-off telemetry event delivery. */
833
+ telemetry?: TelemetryConfig;
747
834
  }
748
835
  /**
749
836
  * ServiceContext provides platform dependencies and cross-service access to services.
@@ -776,6 +863,8 @@ declare class ServiceContext implements IServiceContext {
776
863
  private readonly _fetch;
777
864
  private readonly _hosts;
778
865
  private readonly _retryPolicy;
866
+ private readonly _telemetryEnabled;
867
+ private readonly _telemetryHandler?;
779
868
  constructor(config: ServiceContextConfig);
780
869
  /**
781
870
  * Get the current session.
@@ -2010,4 +2099,4 @@ declare class SecretsService implements ISecretsService {
2010
2099
  list(options?: SecretScopeOptions): Promise<Result<string[], SecretsError>>;
2011
2100
  }
2012
2101
 
2013
- export { BaseService, type BaseServiceOptions, type ColumnInfo, type DataVaultConfig, DataVaultService, DecryptCapabilityProof, DuckDbAction, type DuckDbActionType, type DuckDbBatchOptions, type BatchResponse as DuckDbBatchResponse, DuckDbDatabaseHandle, type DuckDbExecuteOptions, type ExecuteResponse as DuckDbExecuteResponse, type DuckDbOptions, type DuckDbQueryOptions, type QueryResponse as DuckDbQueryResponse, DuckDbService, type DuckDbServiceConfig, type DuckDbStatement, type DuckDbValue, FetchFunction, GenericKVResponseSchema, type GenericKVResponseType, GenericResultSchema, type HookEvent, type HookServiceName, type HookStreamEvent, type HookSubscription, type HookWebhookListOptions, type HookWebhookRecord, type HookWebhookRegistration, type HookWebhookScope, type HookWebhookUnregisterOptions, HooksService, type HooksServiceConfig, type IDataVaultService, type IDuckDbDatabaseHandle, type IDuckDbService, IEncryptionService, type IHooksService, IKVService, type ISecretsService, IService, IServiceContext, InvokeAnyFunction, InvokeFunction, KVListResponseSchema, type KVListResponseType, KVListResultSchema, type KVListResultType, KVResponseHeadersSchema, type KVResponseHeadersType, type QuotaConfig, type QuotaStatus, type ResolvedSecretPath, Result, RetryPolicy, RetryPolicySchema, type RetryPolicyType, SECRET_NAME_RE, type SchemaInfo, type SecretPayload, type SecretScopeOptions, type SecretsError, SecretsService, type ServiceConstructor, ServiceContext, type ServiceContextConfig, ServiceError, ServiceErrorEventSchema, type ServiceErrorEventType, ServiceErrorSchema, type ServiceErrorType, type ServiceRegistration, ServiceRequestEventSchema, type ServiceRequestEventType, ServiceResponseEventSchema, type ServiceResponseEventType, ServiceRetryEventSchema, type ServiceRetryEventType, ServiceSession, ServiceSessionSchema, type ServiceSessionType, StorageQuotaInfo, type SubscribeOptions, type TableInfo, TinyCloudQuota, type ValidationError, type VaultCrypto, type VaultEntry, type VaultError, type VaultGetOptions, type VaultGrantOptions, VaultHeaders, type VaultListOptions, VaultPublicSpaceKVActions, type VaultPutOptions, type ViewInfo, type WasmVaultFunctions, abortedError, authExpiredError, authRequiredError, authUnauthorizedError, canonicalizeSecretScope, createKVResponseSchema, createResultSchema, createVaultCrypto, errorResult, networkError, notFoundError, parseAuthError, permissionDeniedError, resolveSecretListPrefix, resolveSecretPath, storageLimitReachedError, storageQuotaExceededError, timeoutError, validateKVListResponse, validateKVResponseHeaders, validateRetryPolicy, validateServiceError, validateServiceRequestEvent, validateServiceResponseEvent, validateServiceSession, wrapError };
2102
+ export { BaseService, type BaseServiceOptions, type ColumnInfo, type DataVaultConfig, DataVaultService, DecryptCapabilityProof, DuckDbAction, type DuckDbActionType, type DuckDbBatchOptions, type BatchResponse as DuckDbBatchResponse, DuckDbDatabaseHandle, type DuckDbExecuteOptions, type ExecuteResponse as DuckDbExecuteResponse, type DuckDbOptions, type DuckDbQueryOptions, type QueryResponse as DuckDbQueryResponse, DuckDbService, type DuckDbServiceConfig, type DuckDbStatement, type DuckDbValue, EventHandler, FetchFunction, GenericKVResponseSchema, type GenericKVResponseType, GenericResultSchema, type HookEvent, type HookServiceName, type HookStreamEvent, type HookSubscription, type HookWebhookListOptions, type HookWebhookRecord, type HookWebhookRegistration, type HookWebhookScope, type HookWebhookUnregisterOptions, HooksService, type HooksServiceConfig, type IDataVaultService, type IDuckDbDatabaseHandle, type IDuckDbService, IEncryptionService, type IHooksService, IKVService, type ISecretsService, IService, IServiceContext, InvokeAnyFunction, InvokeFunction, KVListResponseSchema, type KVListResponseType, KVListResultSchema, type KVListResultType, KVResponseHeadersSchema, type KVResponseHeadersType, type QuotaConfig, type QuotaStatus, type ResolvedSecretPath, Result, RetryPolicy, RetryPolicySchema, type RetryPolicyType, SECRET_NAME_RE, type SchemaInfo, type SecretPayload, type SecretScopeOptions, type SecretsError, SecretsService, type ServiceConstructor, ServiceContext, type ServiceContextConfig, ServiceError, ServiceErrorEventSchema, type ServiceErrorEventType, ServiceErrorSchema, type ServiceErrorType, type ServiceRegistration, ServiceRequestEventSchema, type ServiceRequestEventType, ServiceResponseEventSchema, type ServiceResponseEventType, ServiceRetryEventSchema, type ServiceRetryEventType, ServiceSession, ServiceSessionSchema, type ServiceSessionType, StorageQuotaInfo, type SubscribeOptions, type TableInfo, TelemetryConfig, TelemetrySpanEventSchema, type TelemetrySpanEventType, TinyCloudQuota, type ValidationError, type VaultCrypto, type VaultEntry, type VaultError, type VaultGetOptions, type VaultGrantOptions, VaultHeaders, type VaultListOptions, VaultPublicSpaceKVActions, type VaultPutOptions, type ViewInfo, type WasmVaultFunctions, abortedError, authExpiredError, authRequiredError, authUnauthorizedError, canonicalizeSecretScope, createKVResponseSchema, createResultSchema, createVaultCrypto, errorResult, networkError, notFoundError, parseAuthError, permissionDeniedError, resolveSecretListPrefix, resolveSecretPath, storageLimitReachedError, storageQuotaExceededError, timeoutError, validateKVListResponse, validateKVResponseHeaders, validateRetryPolicy, validateServiceError, validateServiceRequestEvent, validateServiceResponseEvent, validateServiceSession, validateTelemetrySpanEvent, wrapError };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, c as IService, d as ServiceError, e as Result, B as BaseService, f as StorageQuotaInfo } from './BaseService-DZ2hBJeD.js';
2
- export { E as ErrorCode, g as ErrorCodes, h as EventHandler, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, T as TelemetryEvents, s as defaultRetryPolicy, t as err, u as ok, v as serviceError } from './BaseService-DZ2hBJeD.js';
1
+ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as FetchFunction, S as ServiceSession, R as RetryPolicy, T as TelemetryConfig, c as IService, E as EventHandler, d as ServiceError, e as Result, B as BaseService, f as StorageQuotaInfo } from './BaseService-BdRvNm2s.js';
2
+ export { g as ErrorCode, h as ErrorCodes, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, s as TelemetryEventHandler, t as TelemetryEvents, u as TelemetrySpanEvent, v as defaultRetryPolicy, w as err, x as ok, y as serviceError } from './BaseService-BdRvNm2s.js';
3
3
  import { z } from 'zod';
4
4
  import { IKVService } from './kv/index.js';
5
5
  export { DEFAULT_SIGNED_READ_URL_EXPIRY_MS, IPrefixedKVService, KVAction, KVActionType, KVBatchPutItem, KVBatchPutOptions, KVBatchPutResponse, KVCreateSignedReadUrlOptions, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, KVSignedReadUrlResponse, PrefixedKVService } from './kv/index.js';
@@ -416,17 +416,20 @@ type KVListResultType = z.infer<typeof KVListResultSchema>;
416
416
  declare const ServiceRequestEventSchema: z.ZodObject<{
417
417
  service: z.ZodString;
418
418
  action: z.ZodString;
419
+ span: z.ZodOptional<z.ZodString>;
419
420
  key: z.ZodOptional<z.ZodString>;
420
421
  timestamp: z.ZodNumber;
421
422
  }, "strip", z.ZodTypeAny, {
422
423
  service: string;
423
424
  action: string;
424
425
  timestamp: number;
426
+ span?: string | undefined;
425
427
  key?: string | undefined;
426
428
  }, {
427
429
  service: string;
428
430
  action: string;
429
431
  timestamp: number;
432
+ span?: string | undefined;
430
433
  key?: string | undefined;
431
434
  }>;
432
435
  type ServiceRequestEventType = z.infer<typeof ServiceRequestEventSchema>;
@@ -436,8 +439,10 @@ type ServiceRequestEventType = z.infer<typeof ServiceRequestEventSchema>;
436
439
  declare const ServiceResponseEventSchema: z.ZodObject<{
437
440
  service: z.ZodString;
438
441
  action: z.ZodString;
442
+ span: z.ZodOptional<z.ZodString>;
439
443
  ok: z.ZodBoolean;
440
444
  duration: z.ZodNumber;
445
+ durationMs: z.ZodOptional<z.ZodNumber>;
441
446
  status: z.ZodOptional<z.ZodNumber>;
442
447
  }, "strip", z.ZodTypeAny, {
443
448
  service: string;
@@ -445,12 +450,16 @@ declare const ServiceResponseEventSchema: z.ZodObject<{
445
450
  action: string;
446
451
  duration: number;
447
452
  status?: number | undefined;
453
+ span?: string | undefined;
454
+ durationMs?: number | undefined;
448
455
  }, {
449
456
  service: string;
450
457
  ok: boolean;
451
458
  action: string;
452
459
  duration: number;
453
460
  status?: number | undefined;
461
+ span?: string | undefined;
462
+ durationMs?: number | undefined;
454
463
  }>;
455
464
  type ServiceResponseEventType = z.infer<typeof ServiceResponseEventSchema>;
456
465
  /**
@@ -458,6 +467,7 @@ type ServiceResponseEventType = z.infer<typeof ServiceResponseEventSchema>;
458
467
  */
459
468
  declare const ServiceErrorEventSchema: z.ZodObject<{
460
469
  service: z.ZodString;
470
+ span: z.ZodOptional<z.ZodString>;
461
471
  error: z.ZodObject<{
462
472
  /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
463
473
  code: z.ZodString;
@@ -491,6 +501,7 @@ declare const ServiceErrorEventSchema: z.ZodObject<{
491
501
  meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
492
502
  };
493
503
  service: string;
504
+ span?: string | undefined;
494
505
  }, {
495
506
  error: {
496
507
  code: string;
@@ -500,6 +511,7 @@ declare const ServiceErrorEventSchema: z.ZodObject<{
500
511
  meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
501
512
  };
502
513
  service: string;
514
+ span?: string | undefined;
503
515
  }>;
504
516
  type ServiceErrorEventType = z.infer<typeof ServiceErrorEventSchema>;
505
517
  /**
@@ -557,6 +569,70 @@ declare const ServiceRetryEventSchema: z.ZodObject<{
557
569
  maxAttempts: number;
558
570
  }>;
559
571
  type ServiceRetryEventType = z.infer<typeof ServiceRetryEventSchema>;
572
+ /**
573
+ * Schema for generic named span event.
574
+ */
575
+ declare const TelemetrySpanEventSchema: z.ZodObject<{
576
+ span: z.ZodString;
577
+ ok: z.ZodBoolean;
578
+ durationMs: z.ZodNumber;
579
+ service: z.ZodOptional<z.ZodString>;
580
+ action: z.ZodOptional<z.ZodString>;
581
+ status: z.ZodOptional<z.ZodNumber>;
582
+ error: z.ZodOptional<z.ZodObject<{
583
+ /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
584
+ code: z.ZodString;
585
+ /** Human-readable error message */
586
+ message: z.ZodString;
587
+ /** Service that produced the error (e.g., 'kv', 'sql') */
588
+ service: z.ZodString;
589
+ /** Original error if this wraps another error - not validated since Error is a class */
590
+ cause: z.ZodOptional<z.ZodUnknown>;
591
+ /** Additional metadata about the error - passthrough allows any object properties */
592
+ meta: z.ZodOptional<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>>;
593
+ }, "strip", z.ZodTypeAny, {
594
+ code: string;
595
+ message: string;
596
+ service: string;
597
+ cause?: unknown;
598
+ meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
599
+ }, {
600
+ code: string;
601
+ message: string;
602
+ service: string;
603
+ cause?: unknown;
604
+ meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
605
+ }>>;
606
+ }, "strip", z.ZodTypeAny, {
607
+ ok: boolean;
608
+ span: string;
609
+ durationMs: number;
610
+ error?: {
611
+ code: string;
612
+ message: string;
613
+ service: string;
614
+ cause?: unknown;
615
+ meta?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
616
+ } | undefined;
617
+ service?: string | undefined;
618
+ status?: number | undefined;
619
+ action?: string | undefined;
620
+ }, {
621
+ ok: boolean;
622
+ span: string;
623
+ durationMs: number;
624
+ error?: {
625
+ code: string;
626
+ message: string;
627
+ service: string;
628
+ cause?: unknown;
629
+ meta?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
630
+ } | undefined;
631
+ service?: string | undefined;
632
+ status?: number | undefined;
633
+ action?: string | undefined;
634
+ }>;
635
+ type TelemetrySpanEventType = z.infer<typeof TelemetrySpanEventSchema>;
560
636
  /**
561
637
  * Schema for retry policy configuration.
562
638
  */
@@ -718,16 +794,25 @@ declare function validateServiceResponseEvent(data: unknown): {
718
794
  ok: false;
719
795
  error: ValidationError;
720
796
  };
797
+ /**
798
+ * Validate named telemetry span event against the schema.
799
+ *
800
+ * @param data - Unknown data to validate
801
+ * @returns Result with validated data or validation error
802
+ */
803
+ declare function validateTelemetrySpanEvent(data: unknown): {
804
+ ok: true;
805
+ data: TelemetrySpanEventType;
806
+ } | {
807
+ ok: false;
808
+ error: ValidationError;
809
+ };
721
810
 
722
811
  /**
723
812
  * ServiceContext implementation for TinyCloud SDK Services
724
813
  * @module @tinycloud/sdk-services
725
814
  */
726
815
 
727
- /**
728
- * Event handler type for telemetry events.
729
- */
730
- type EventHandler = (data: unknown) => void;
731
816
  /**
732
817
  * Configuration options for ServiceContext.
733
818
  */
@@ -744,6 +829,8 @@ interface ServiceContextConfig {
744
829
  session?: ServiceSession | null;
745
830
  /** Retry policy configuration */
746
831
  retryPolicy?: Partial<RetryPolicy>;
832
+ /** Default-off telemetry event delivery. */
833
+ telemetry?: TelemetryConfig;
747
834
  }
748
835
  /**
749
836
  * ServiceContext provides platform dependencies and cross-service access to services.
@@ -776,6 +863,8 @@ declare class ServiceContext implements IServiceContext {
776
863
  private readonly _fetch;
777
864
  private readonly _hosts;
778
865
  private readonly _retryPolicy;
866
+ private readonly _telemetryEnabled;
867
+ private readonly _telemetryHandler?;
779
868
  constructor(config: ServiceContextConfig);
780
869
  /**
781
870
  * Get the current session.
@@ -2010,4 +2099,4 @@ declare class SecretsService implements ISecretsService {
2010
2099
  list(options?: SecretScopeOptions): Promise<Result<string[], SecretsError>>;
2011
2100
  }
2012
2101
 
2013
- export { BaseService, type BaseServiceOptions, type ColumnInfo, type DataVaultConfig, DataVaultService, DecryptCapabilityProof, DuckDbAction, type DuckDbActionType, type DuckDbBatchOptions, type BatchResponse as DuckDbBatchResponse, DuckDbDatabaseHandle, type DuckDbExecuteOptions, type ExecuteResponse as DuckDbExecuteResponse, type DuckDbOptions, type DuckDbQueryOptions, type QueryResponse as DuckDbQueryResponse, DuckDbService, type DuckDbServiceConfig, type DuckDbStatement, type DuckDbValue, FetchFunction, GenericKVResponseSchema, type GenericKVResponseType, GenericResultSchema, type HookEvent, type HookServiceName, type HookStreamEvent, type HookSubscription, type HookWebhookListOptions, type HookWebhookRecord, type HookWebhookRegistration, type HookWebhookScope, type HookWebhookUnregisterOptions, HooksService, type HooksServiceConfig, type IDataVaultService, type IDuckDbDatabaseHandle, type IDuckDbService, IEncryptionService, type IHooksService, IKVService, type ISecretsService, IService, IServiceContext, InvokeAnyFunction, InvokeFunction, KVListResponseSchema, type KVListResponseType, KVListResultSchema, type KVListResultType, KVResponseHeadersSchema, type KVResponseHeadersType, type QuotaConfig, type QuotaStatus, type ResolvedSecretPath, Result, RetryPolicy, RetryPolicySchema, type RetryPolicyType, SECRET_NAME_RE, type SchemaInfo, type SecretPayload, type SecretScopeOptions, type SecretsError, SecretsService, type ServiceConstructor, ServiceContext, type ServiceContextConfig, ServiceError, ServiceErrorEventSchema, type ServiceErrorEventType, ServiceErrorSchema, type ServiceErrorType, type ServiceRegistration, ServiceRequestEventSchema, type ServiceRequestEventType, ServiceResponseEventSchema, type ServiceResponseEventType, ServiceRetryEventSchema, type ServiceRetryEventType, ServiceSession, ServiceSessionSchema, type ServiceSessionType, StorageQuotaInfo, type SubscribeOptions, type TableInfo, TinyCloudQuota, type ValidationError, type VaultCrypto, type VaultEntry, type VaultError, type VaultGetOptions, type VaultGrantOptions, VaultHeaders, type VaultListOptions, VaultPublicSpaceKVActions, type VaultPutOptions, type ViewInfo, type WasmVaultFunctions, abortedError, authExpiredError, authRequiredError, authUnauthorizedError, canonicalizeSecretScope, createKVResponseSchema, createResultSchema, createVaultCrypto, errorResult, networkError, notFoundError, parseAuthError, permissionDeniedError, resolveSecretListPrefix, resolveSecretPath, storageLimitReachedError, storageQuotaExceededError, timeoutError, validateKVListResponse, validateKVResponseHeaders, validateRetryPolicy, validateServiceError, validateServiceRequestEvent, validateServiceResponseEvent, validateServiceSession, wrapError };
2102
+ export { BaseService, type BaseServiceOptions, type ColumnInfo, type DataVaultConfig, DataVaultService, DecryptCapabilityProof, DuckDbAction, type DuckDbActionType, type DuckDbBatchOptions, type BatchResponse as DuckDbBatchResponse, DuckDbDatabaseHandle, type DuckDbExecuteOptions, type ExecuteResponse as DuckDbExecuteResponse, type DuckDbOptions, type DuckDbQueryOptions, type QueryResponse as DuckDbQueryResponse, DuckDbService, type DuckDbServiceConfig, type DuckDbStatement, type DuckDbValue, EventHandler, FetchFunction, GenericKVResponseSchema, type GenericKVResponseType, GenericResultSchema, type HookEvent, type HookServiceName, type HookStreamEvent, type HookSubscription, type HookWebhookListOptions, type HookWebhookRecord, type HookWebhookRegistration, type HookWebhookScope, type HookWebhookUnregisterOptions, HooksService, type HooksServiceConfig, type IDataVaultService, type IDuckDbDatabaseHandle, type IDuckDbService, IEncryptionService, type IHooksService, IKVService, type ISecretsService, IService, IServiceContext, InvokeAnyFunction, InvokeFunction, KVListResponseSchema, type KVListResponseType, KVListResultSchema, type KVListResultType, KVResponseHeadersSchema, type KVResponseHeadersType, type QuotaConfig, type QuotaStatus, type ResolvedSecretPath, Result, RetryPolicy, RetryPolicySchema, type RetryPolicyType, SECRET_NAME_RE, type SchemaInfo, type SecretPayload, type SecretScopeOptions, type SecretsError, SecretsService, type ServiceConstructor, ServiceContext, type ServiceContextConfig, ServiceError, ServiceErrorEventSchema, type ServiceErrorEventType, ServiceErrorSchema, type ServiceErrorType, type ServiceRegistration, ServiceRequestEventSchema, type ServiceRequestEventType, ServiceResponseEventSchema, type ServiceResponseEventType, ServiceRetryEventSchema, type ServiceRetryEventType, ServiceSession, ServiceSessionSchema, type ServiceSessionType, StorageQuotaInfo, type SubscribeOptions, type TableInfo, TelemetryConfig, TelemetrySpanEventSchema, type TelemetrySpanEventType, TinyCloudQuota, type ValidationError, type VaultCrypto, type VaultEntry, type VaultError, type VaultGetOptions, type VaultGrantOptions, VaultHeaders, type VaultListOptions, VaultPublicSpaceKVActions, type VaultPutOptions, type ViewInfo, type WasmVaultFunctions, abortedError, authExpiredError, authRequiredError, authUnauthorizedError, canonicalizeSecretScope, createKVResponseSchema, createResultSchema, createVaultCrypto, errorResult, networkError, notFoundError, parseAuthError, permissionDeniedError, resolveSecretListPrefix, resolveSecretPath, storageLimitReachedError, storageQuotaExceededError, timeoutError, validateKVListResponse, validateKVResponseHeaders, validateRetryPolicy, validateServiceError, validateServiceRequestEvent, validateServiceResponseEvent, validateServiceSession, validateTelemetrySpanEvent, wrapError };
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ var defaultRetryPolicy = {
43
43
  retryableErrors: [ErrorCodes.NETWORK_ERROR, ErrorCodes.TIMEOUT]
44
44
  };
45
45
  var TelemetryEvents = {
46
+ SPAN: "telemetry.span",
46
47
  SERVICE_REQUEST: "service.request",
47
48
  SERVICE_RESPONSE: "service.response",
48
49
  SERVICE_ERROR: "service.error",
@@ -120,18 +121,22 @@ var KVListResultSchema = createResultSchema(KVListResponseSchema);
120
121
  var ServiceRequestEventSchema = z.object({
121
122
  service: z.string(),
122
123
  action: z.string(),
124
+ span: z.string().optional(),
123
125
  key: z.string().optional(),
124
126
  timestamp: z.number()
125
127
  });
126
128
  var ServiceResponseEventSchema = z.object({
127
129
  service: z.string(),
128
130
  action: z.string(),
131
+ span: z.string().optional(),
129
132
  ok: z.boolean(),
130
133
  duration: z.number(),
134
+ durationMs: z.number().optional(),
131
135
  status: z.number().optional()
132
136
  });
133
137
  var ServiceErrorEventSchema = z.object({
134
138
  service: z.string(),
139
+ span: z.string().optional(),
135
140
  error: ServiceErrorSchema
136
141
  });
137
142
  var ServiceRetryEventSchema = z.object({
@@ -140,6 +145,15 @@ var ServiceRetryEventSchema = z.object({
140
145
  maxAttempts: z.number().int().positive(),
141
146
  error: ServiceErrorSchema
142
147
  });
148
+ var TelemetrySpanEventSchema = z.object({
149
+ span: z.string(),
150
+ ok: z.boolean(),
151
+ durationMs: z.number(),
152
+ service: z.string().optional(),
153
+ action: z.string().optional(),
154
+ status: z.number().optional(),
155
+ error: ServiceErrorSchema.optional()
156
+ });
143
157
  var RetryPolicySchema = z.object({
144
158
  /** Maximum number of attempts (including initial) */
145
159
  maxAttempts: z.number().int().positive(),
@@ -271,6 +285,21 @@ function validateServiceResponseEvent(data) {
271
285
  }
272
286
  return { ok: true, data: result.data };
273
287
  }
288
+ function validateTelemetrySpanEvent(data) {
289
+ const result = TelemetrySpanEventSchema.safeParse(data);
290
+ if (!result.success) {
291
+ return {
292
+ ok: false,
293
+ error: {
294
+ code: "VALIDATION_ERROR",
295
+ message: result.error.message,
296
+ service: "telemetry",
297
+ meta: { issues: result.error.issues }
298
+ }
299
+ };
300
+ }
301
+ return { ok: true, data: result.data };
302
+ }
274
303
 
275
304
  // src/context.ts
276
305
  var ServiceContext = class {
@@ -288,6 +317,8 @@ var ServiceContext = class {
288
317
  ...defaultRetryPolicy,
289
318
  ...config.retryPolicy
290
319
  };
320
+ this._telemetryEnabled = typeof config.telemetry === "boolean" ? config.telemetry : config.telemetry?.enabled === true;
321
+ this._telemetryHandler = typeof config.telemetry === "object" ? config.telemetry.onEvent : void 0;
291
322
  }
292
323
  // ============================================================
293
324
  // Session Management
@@ -382,6 +413,13 @@ var ServiceContext = class {
382
413
  * @param data - Event data
383
414
  */
384
415
  emit(event, data) {
416
+ if (this._telemetryEnabled && this._telemetryHandler) {
417
+ try {
418
+ this._telemetryHandler(event, data);
419
+ } catch (error) {
420
+ console.error(`Error in telemetry handler for "${event}":`, error);
421
+ }
422
+ }
385
423
  const handlers = this._eventHandlers.get(event);
386
424
  if (handlers) {
387
425
  for (const handler of handlers) {
@@ -660,9 +698,11 @@ var BaseService = class {
660
698
  * @param key - Optional key/path being accessed
661
699
  */
662
700
  emitRequest(action, key) {
701
+ const service = this.getServiceName();
663
702
  this.emit(TelemetryEvents.SERVICE_REQUEST, {
664
- service: this.getServiceName(),
703
+ service,
665
704
  action,
705
+ span: this.spanName(action),
666
706
  key,
667
707
  timestamp: Date.now()
668
708
  });
@@ -676,11 +716,24 @@ var BaseService = class {
676
716
  * @param status - Optional HTTP status code
677
717
  */
678
718
  emitResponse(action, ok2, startTime, status) {
719
+ const service = this.getServiceName();
720
+ const durationMs = Date.now() - startTime;
721
+ const span = this.spanName(action);
679
722
  this.emit(TelemetryEvents.SERVICE_RESPONSE, {
680
- service: this.getServiceName(),
723
+ service,
681
724
  action,
725
+ span,
682
726
  ok: ok2,
683
- duration: Date.now() - startTime,
727
+ duration: durationMs,
728
+ durationMs,
729
+ status
730
+ });
731
+ this.emit(TelemetryEvents.SPAN, {
732
+ span,
733
+ service,
734
+ action,
735
+ ok: ok2,
736
+ durationMs,
684
737
  status
685
738
  });
686
739
  }
@@ -689,9 +742,11 @@ var BaseService = class {
689
742
  *
690
743
  * @param error - The service error
691
744
  */
692
- emitError(error) {
745
+ emitError(error, action) {
746
+ const span = action ? this.spanName(action) : void 0;
693
747
  this.emit(TelemetryEvents.SERVICE_ERROR, {
694
748
  service: this.getServiceName(),
749
+ ...span ? { span } : {},
695
750
  error
696
751
  });
697
752
  }
@@ -702,6 +757,12 @@ var BaseService = class {
702
757
  getServiceName() {
703
758
  return this.constructor.serviceName;
704
759
  }
760
+ /**
761
+ * Stable span name used by SDK telemetry sinks.
762
+ */
763
+ spanName(action) {
764
+ return `sdk.${this.getServiceName()}.${action}`;
765
+ }
705
766
  /**
706
767
  * Create a combined abort signal from multiple sources.
707
768
  *
@@ -739,13 +800,13 @@ var BaseService = class {
739
800
  this.emitResponse(action, true, startTime);
740
801
  } else {
741
802
  this.emitResponse(action, false, startTime);
742
- this.emitError(result.error);
803
+ this.emitError(result.error, action);
743
804
  }
744
805
  return result;
745
806
  } catch (error) {
746
807
  const serviceError3 = wrapError(this.getServiceName(), error);
747
808
  this.emitResponse(action, false, startTime);
748
- this.emitError(serviceError3);
809
+ this.emitError(serviceError3, action);
749
810
  return err(serviceError3);
750
811
  }
751
812
  }
@@ -5556,6 +5617,7 @@ export {
5556
5617
  ServiceRetryEventSchema,
5557
5618
  ServiceSessionSchema,
5558
5619
  TelemetryEvents,
5620
+ TelemetrySpanEventSchema,
5559
5621
  TinyCloudQuota,
5560
5622
  VaultHeaders,
5561
5623
  VaultPublicSpaceKVActions,
@@ -5615,6 +5677,7 @@ export {
5615
5677
  validateServiceRequestEvent,
5616
5678
  validateServiceResponseEvent,
5617
5679
  validateServiceSession,
5680
+ validateTelemetrySpanEvent,
5618
5681
  verifyDecryptResponse,
5619
5682
  wrapError
5620
5683
  };