@virtu3d/event-manager 0.2.7

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.
@@ -0,0 +1,568 @@
1
+ export { TraceContext, extractTraceContext, generateSpanId, generateTraceId, generateTraceparent, getTraceHeaders, getTraceHeadersSync, parseTraceparent, startSpan, withTraceContext } from './context/index.mjs';
2
+
3
+ /**
4
+ * Base telemetry event structure
5
+ */
6
+ interface TelemetryEvent {
7
+ /** Event name e.g., 'button_click', 'checkout', 'page_view' */
8
+ name: string;
9
+ /** Event payload - any key-value data */
10
+ payload: Record<string, unknown>;
11
+ /** ISO timestamp - auto-generated if not provided */
12
+ timestamp?: string;
13
+ /** Optional metadata */
14
+ meta?: {
15
+ sessionId?: string;
16
+ userId?: string;
17
+ orgId?: string;
18
+ traceId?: string;
19
+ spanId?: string;
20
+ [key: string]: unknown;
21
+ };
22
+ }
23
+ /**
24
+ * User identification event
25
+ */
26
+ interface IdentifyEvent {
27
+ userId: string;
28
+ traits?: Record<string, unknown>;
29
+ timestamp?: string;
30
+ }
31
+ /**
32
+ * Page view event
33
+ */
34
+ interface PageEvent {
35
+ name?: string;
36
+ properties?: Record<string, unknown>;
37
+ timestamp?: string;
38
+ }
39
+ /**
40
+ * Connector types
41
+ */
42
+ type ConnectorType = 'observability' | 'analytics' | 'metrics' | 'custom';
43
+ /**
44
+ * Connector interface - all connectors must implement this
45
+ */
46
+ interface Connector {
47
+ /** Unique connector name e.g., 'otel', 'mixpanel', 'cloudwatch-emf' */
48
+ name: string;
49
+ /** Connector category */
50
+ type: ConnectorType;
51
+ /** Initialize connector (load scripts, setup clients) */
52
+ init?(): Promise<void> | void;
53
+ /** Handle track events */
54
+ track(event: TelemetryEvent): Promise<void> | void;
55
+ /** Handle identify events */
56
+ identify?(event: IdentifyEvent): Promise<void> | void;
57
+ /** Handle page view events */
58
+ page?(event: PageEvent): Promise<void> | void;
59
+ /** Cleanup resources */
60
+ shutdown?(): Promise<void> | void;
61
+ /** Check if connector is ready */
62
+ isReady?(): boolean;
63
+ }
64
+ /**
65
+ * Connector configuration base
66
+ */
67
+ interface ConnectorConfig {
68
+ /** Enable/disable connector */
69
+ enabled?: boolean;
70
+ /** Debug mode */
71
+ debug?: boolean;
72
+ }
73
+ /**
74
+ * Factory interface for creating connectors
75
+ */
76
+ interface ConnectorFactory<TConfig extends ConnectorConfig = ConnectorConfig> {
77
+ /** Factory identifier */
78
+ name: string;
79
+ /** Factory type */
80
+ type: ConnectorType;
81
+ /** Create a connector instance */
82
+ create(config: TConfig): Connector;
83
+ }
84
+ /**
85
+ * Router configuration
86
+ */
87
+ interface RouterConfig {
88
+ /** Application name */
89
+ app?: string;
90
+ /** Debug mode - logs all events to console */
91
+ debug?: boolean;
92
+ /** Global metadata added to all events */
93
+ globalMeta?: Record<string, unknown>;
94
+ /** Plugins/middleware */
95
+ plugins?: RouterPlugin[];
96
+ }
97
+ /**
98
+ * Plugin interface for middleware functionality
99
+ */
100
+ interface RouterPlugin {
101
+ name: string;
102
+ /** Called before event is dispatched to connectors */
103
+ beforeDispatch?(event: TelemetryEvent): TelemetryEvent | null;
104
+ /** Called after all connectors have processed */
105
+ afterDispatch?(event: TelemetryEvent, results: DispatchResult[]): void;
106
+ }
107
+ /**
108
+ * Result of dispatching to a single connector
109
+ */
110
+ interface DispatchResult {
111
+ connector: string;
112
+ success: boolean;
113
+ error?: Error;
114
+ duration?: number;
115
+ }
116
+ /**
117
+ * Result of dispatching to all connectors
118
+ */
119
+ interface DispatchResults {
120
+ event: TelemetryEvent;
121
+ results: DispatchResult[];
122
+ timestamp: string;
123
+ }
124
+
125
+ /**
126
+ * Connector Factory Registry
127
+ *
128
+ * Maps connector type strings to their factory functions.
129
+ * This allows the router to auto-instantiate connectors from config.
130
+ */
131
+
132
+ /**
133
+ * Factory function type
134
+ */
135
+ type ConnectorFactoryFn<TConfig extends ConnectorConfig = ConnectorConfig> = (config: TConfig) => Connector;
136
+ /**
137
+ * Provider configuration for router
138
+ */
139
+ interface ProviderConfig {
140
+ /** Unique name for this provider instance */
141
+ name: string;
142
+ /** Provider type - maps to factory */
143
+ type: ConnectorTypeName;
144
+ /** Enable/disable this provider */
145
+ enabled?: boolean;
146
+ /** Provider-specific configuration */
147
+ config?: ConnectorConfig;
148
+ }
149
+ /**
150
+ * Built-in connector type names
151
+ */
152
+ type ConnectorTypeName = 'console' | 'otel' | 'otel-browser' | 'cloudwatch-emf' | 'mixpanel' | 'posthog';
153
+ /**
154
+ * Factory registry - maps type names to factory functions
155
+ */
156
+ declare const factoryRegistry: Map<string, ConnectorFactoryFn<any>>;
157
+ /**
158
+ * Register a connector factory
159
+ */
160
+ declare function registerFactory<TConfig extends ConnectorConfig>(typeName: string, factory: ConnectorFactoryFn<TConfig>): void;
161
+ /**
162
+ * Get a factory by type name
163
+ */
164
+ declare function getFactory(typeName: string): ConnectorFactoryFn | undefined;
165
+ /**
166
+ * Check if a factory is registered
167
+ */
168
+ declare function hasFactory(typeName: string): boolean;
169
+ /**
170
+ * Get all registered factory names
171
+ */
172
+ declare function getRegisteredFactories(): string[];
173
+ /**
174
+ * Create a connector from type and config
175
+ */
176
+ declare function createConnector(typeName: string, config: ConnectorConfig): Connector;
177
+ /**
178
+ * Create connectors from provider configs
179
+ */
180
+ declare function createConnectorsFromConfig(providers: ProviderConfig[]): Connector[];
181
+
182
+ /**
183
+ * Extended router config with providers support
184
+ */
185
+ interface TelemetryRouterConfig extends RouterConfig {
186
+ /** Provider configurations - auto-registered on construction */
187
+ providers?: ProviderConfig[];
188
+ }
189
+ declare class TelemetryRouter {
190
+ private connectors;
191
+ private plugins;
192
+ private config;
193
+ private initialized;
194
+ constructor(config?: TelemetryRouterConfig);
195
+ /**
196
+ * Initialize all registered connectors
197
+ */
198
+ init(): Promise<void>;
199
+ /**
200
+ * Register a connector instance directly
201
+ */
202
+ registerConnector(connector: Connector): this;
203
+ /**
204
+ * Register connector using a factory
205
+ */
206
+ use<TConfig extends ConnectorConfig>(factory: ConnectorFactory<TConfig>, config: TConfig): this;
207
+ /**
208
+ * Unregister a connector by name
209
+ */
210
+ unregisterConnector(name: string): this;
211
+ /**
212
+ * Add a plugin
213
+ */
214
+ addPlugin(plugin: RouterPlugin): this;
215
+ /**
216
+ * Track a custom event
217
+ */
218
+ track(eventName: string, payload?: Record<string, unknown>): Promise<DispatchResults>;
219
+ /**
220
+ * Identify a user
221
+ */
222
+ identify(userId: string, traits?: Record<string, unknown>): Promise<DispatchResults>;
223
+ /**
224
+ * Track a page view
225
+ */
226
+ page(name?: string, properties?: Record<string, unknown>): Promise<DispatchResults>;
227
+ /**
228
+ * Core dispatch method - sends to all connectors in parallel
229
+ */
230
+ private dispatch;
231
+ /**
232
+ * Get all registered connector names
233
+ */
234
+ getConnectors(): string[];
235
+ /**
236
+ * Get connectors by type
237
+ */
238
+ getConnectorsByType(type: Connector['type']): string[];
239
+ /**
240
+ * Shutdown all connectors
241
+ */
242
+ shutdown(): Promise<void>;
243
+ /**
244
+ * Internal logger
245
+ */
246
+ private log;
247
+ }
248
+ /**
249
+ * Create a new TelemetryRouter instance
250
+ */
251
+ declare function createTelemetryRouter(config?: RouterConfig): TelemetryRouter;
252
+
253
+ /**
254
+ * Analytics provider options
255
+ */
256
+ type AnalyticsProvider = 'mixpanel' | 'posthog' | 'segment' | 'none';
257
+ /**
258
+ * Observability provider options
259
+ */
260
+ type ObservabilityProvider = 'otel' | 'datadog' | 'newrelic' | 'none';
261
+ /**
262
+ * Metrics provider options (for custom business metrics)
263
+ */
264
+ type MetricsProvider = 'cloudwatch-emf' | 'none';
265
+ /**
266
+ * Metric mapping configuration for CloudWatch EMF
267
+ */
268
+ interface MetricMapping {
269
+ /** CloudWatch metric name */
270
+ metricName: string;
271
+ /** Metric unit */
272
+ unit: 'Count' | 'Milliseconds' | 'Bytes' | 'None' | 'Percent';
273
+ /** Dimension fields to extract from event payload */
274
+ dimensions?: string[];
275
+ /** Field to use as metric value (default: 1 for count) */
276
+ valueField?: string;
277
+ }
278
+ /**
279
+ * Provider-specific configurations
280
+ */
281
+ interface ProviderConfigs {
282
+ mixpanel?: {
283
+ token: string;
284
+ trackFields?: string[];
285
+ debug?: boolean;
286
+ };
287
+ posthog?: {
288
+ apiKey: string;
289
+ host?: string;
290
+ debug?: boolean;
291
+ };
292
+ segment?: {
293
+ writeKey: string;
294
+ debug?: boolean;
295
+ };
296
+ otel?: {
297
+ serviceName: string;
298
+ endpoint?: string;
299
+ debug?: boolean;
300
+ };
301
+ datadog?: {
302
+ clientToken: string;
303
+ site?: string;
304
+ service?: string;
305
+ debug?: boolean;
306
+ };
307
+ cloudwatchEmf?: {
308
+ namespace: string;
309
+ serviceName: string;
310
+ region?: string;
311
+ /** Map event names to metric configurations */
312
+ metricMappings?: Record<string, MetricMapping>;
313
+ debug?: boolean;
314
+ };
315
+ }
316
+ /**
317
+ * Main telemetry configuration
318
+ */
319
+ interface TelemetryConfig {
320
+ /** Application name */
321
+ app: string;
322
+ /** Environment (auto-detected if not provided) */
323
+ environment?: 'browser' | 'node';
324
+ /** Enable debug logging */
325
+ debug?: boolean;
326
+ /** Global metadata added to all events */
327
+ globalMeta?: Record<string, unknown>;
328
+ /** Analytics configuration */
329
+ analytics: {
330
+ /** Active provider - change this to switch */
331
+ provider: AnalyticsProvider;
332
+ } & ProviderConfigs;
333
+ /** Observability configuration */
334
+ observability: {
335
+ /** Active provider - change this to switch */
336
+ provider: ObservabilityProvider;
337
+ } & ProviderConfigs;
338
+ /** Custom metrics configuration */
339
+ metrics?: {
340
+ /** Active provider - change this to switch */
341
+ provider: MetricsProvider;
342
+ } & ProviderConfigs;
343
+ }
344
+ /**
345
+ * Validate configuration
346
+ */
347
+ declare function validateConfig(config: TelemetryConfig): void;
348
+
349
+ /**
350
+ * Telemetry Utilities
351
+ *
352
+ * Common helper functions for telemetry operations.
353
+ */
354
+ /**
355
+ * Detect current environment (browser or node)
356
+ */
357
+ declare function detectEnvironment(): 'browser' | 'node';
358
+ /**
359
+ * Generate a unique session ID
360
+ */
361
+ declare function generateSessionId(): string;
362
+ /**
363
+ * Generate a unique event ID
364
+ */
365
+ declare function generateEventId(): string;
366
+ /**
367
+ * Safely stringify an object (handles circular references)
368
+ */
369
+ declare function safeStringify(obj: unknown, space?: number): string;
370
+ /**
371
+ * Deep merge two objects
372
+ */
373
+ declare function deepMerge<T extends object>(target: T, source: Partial<T>): T;
374
+ /**
375
+ * Check if value is a plain object
376
+ */
377
+ declare function isObject(value: unknown): value is Record<string, unknown>;
378
+ /**
379
+ * Sanitize sensitive fields from payload
380
+ */
381
+ declare function sanitizePayload(payload: Record<string, unknown>, sensitiveFields?: string[]): Record<string, unknown>;
382
+ /**
383
+ * Extract specific fields from an object
384
+ */
385
+ declare function extractFields<T extends Record<string, unknown>>(obj: T, fields: (keyof T)[]): Partial<T>;
386
+ /**
387
+ * Flatten a nested object into dot-notation keys
388
+ */
389
+ declare function flattenObject(obj: Record<string, unknown>, prefix?: string): Record<string, unknown>;
390
+ /**
391
+ * Retry a function with exponential backoff
392
+ */
393
+ declare function retryWithBackoff<T>(fn: () => Promise<T>, options?: {
394
+ maxRetries?: number;
395
+ initialDelay?: number;
396
+ maxDelay?: number;
397
+ factor?: number;
398
+ }): Promise<T>;
399
+ /**
400
+ * Sleep for a specified duration
401
+ */
402
+ declare function sleep(ms: number): Promise<void>;
403
+ /**
404
+ * Debounce a function
405
+ */
406
+ declare function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void;
407
+ /**
408
+ * Batch items and process in chunks
409
+ */
410
+ declare function processBatch<T, R>(items: T[], processor: (batch: T[]) => Promise<R>, batchSize?: number): Promise<R[]>;
411
+ /**
412
+ * Create a simple in-memory cache with TTL
413
+ */
414
+ declare function createCache<T>(defaultTtl?: number): {
415
+ get(key: string): T | undefined;
416
+ set(key: string, value: T, ttl?: number): void;
417
+ delete(key: string): boolean;
418
+ clear(): void;
419
+ size(): number;
420
+ };
421
+
422
+ declare abstract class BaseConnector implements Connector {
423
+ abstract name: string;
424
+ abstract type: ConnectorType;
425
+ protected config: ConnectorConfig;
426
+ protected ready: boolean;
427
+ constructor(config?: ConnectorConfig);
428
+ init(): Promise<void>;
429
+ /**
430
+ * Override this to implement connector-specific setup
431
+ */
432
+ protected abstract setup(): Promise<void> | void;
433
+ abstract track(event: TelemetryEvent): Promise<void> | void;
434
+ identify?(event: IdentifyEvent): Promise<void> | void;
435
+ page?(event: PageEvent): Promise<void> | void;
436
+ shutdown(): Promise<void>;
437
+ isReady(): boolean;
438
+ protected log(message: string, data?: unknown): void;
439
+ }
440
+
441
+ interface ConsoleConnectorConfig extends ConnectorConfig {
442
+ prefix?: string;
443
+ }
444
+ /**
445
+ * Console connector for debugging during development
446
+ */
447
+ declare class ConsoleConnector extends BaseConnector {
448
+ name: string;
449
+ type: "custom";
450
+ private prefix;
451
+ constructor(config?: ConsoleConnectorConfig);
452
+ protected setup(): void;
453
+ track(event: TelemetryEvent): void;
454
+ identify(event: IdentifyEvent): void;
455
+ page(event: PageEvent): void;
456
+ }
457
+ declare function createConsoleConnector(config?: ConsoleConnectorConfig): ConsoleConnector;
458
+
459
+ interface OtelConnectorConfig extends ConnectorConfig {
460
+ serviceName: string;
461
+ endpoint?: string;
462
+ /** Custom span attributes extractor */
463
+ attributesExtractor?: (event: TelemetryEvent) => Record<string, string | number | boolean>;
464
+ }
465
+ /**
466
+ * OpenTelemetry connector for distributed tracing
467
+ */
468
+ declare class OtelConnector extends BaseConnector {
469
+ name: string;
470
+ type: "observability";
471
+ private tracer;
472
+ private context;
473
+ private serviceName;
474
+ private attributesExtractor?;
475
+ constructor(config: OtelConnectorConfig);
476
+ protected setup(): Promise<void>;
477
+ track(event: TelemetryEvent): void;
478
+ identify(event: IdentifyEvent): void;
479
+ page(event: PageEvent): void;
480
+ }
481
+ declare function createOtelConnector(config: OtelConnectorConfig): OtelConnector;
482
+
483
+ interface CloudWatchEmfConnectorConfig extends ConnectorConfig {
484
+ /** CloudWatch namespace e.g., 'Virtu3D/Application' */
485
+ namespace: string;
486
+ /** Service name */
487
+ serviceName: string;
488
+ /** AWS Region */
489
+ region?: string;
490
+ /** Map event names to metric configurations */
491
+ metricMappings?: Record<string, MetricMapping>;
492
+ }
493
+ /**
494
+ * CloudWatch Embedded Metric Format (EMF) Connector
495
+ *
496
+ * Outputs structured JSON logs that CloudWatch automatically parses into metrics.
497
+ * Works with existing Docker awslogs driver - no additional infrastructure needed.
498
+ *
499
+ * IMPORTANT: This is for CUSTOM BUSINESS METRICS only.
500
+ * Application logs already go to CloudWatch via Docker awslogs driver + Pino.
501
+ * DO NOT use this connector for application logging.
502
+ *
503
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html
504
+ */
505
+ declare class CloudWatchEmfConnector extends BaseConnector {
506
+ name: string;
507
+ type: "metrics";
508
+ private namespace;
509
+ private serviceName;
510
+ private region;
511
+ private metricMappings;
512
+ constructor(config: CloudWatchEmfConnectorConfig);
513
+ protected setup(): void;
514
+ track(event: TelemetryEvent): void;
515
+ identify(event: IdentifyEvent): void;
516
+ /**
517
+ * Convert event name to CloudWatch metric name
518
+ * e.g., 'ai_chat_completed' -> 'AiChatCompleted'
519
+ */
520
+ private toMetricName;
521
+ }
522
+ declare function createCloudWatchEmfConnector(config: CloudWatchEmfConnectorConfig): CloudWatchEmfConnector;
523
+
524
+ interface MixpanelConnectorConfig extends ConnectorConfig {
525
+ token: string;
526
+ /** Fields to extract from payload for Mixpanel */
527
+ trackFields?: string[];
528
+ }
529
+ /**
530
+ * Mixpanel connector for analytics
531
+ */
532
+ declare class MixpanelConnector extends BaseConnector {
533
+ name: string;
534
+ type: "analytics";
535
+ private client;
536
+ private token;
537
+ private trackFields?;
538
+ constructor(config: MixpanelConnectorConfig);
539
+ protected setup(): Promise<void>;
540
+ track(event: TelemetryEvent): void;
541
+ identify(event: IdentifyEvent): void;
542
+ page(event: PageEvent): void;
543
+ }
544
+ declare function createMixpanelConnector(config: MixpanelConnectorConfig): MixpanelConnector;
545
+
546
+ interface PostHogConnectorConfig extends ConnectorConfig {
547
+ apiKey: string;
548
+ host?: string;
549
+ }
550
+ /**
551
+ * PostHog connector for analytics
552
+ */
553
+ declare class PostHogConnector extends BaseConnector {
554
+ name: string;
555
+ type: "analytics";
556
+ private client;
557
+ private apiKey;
558
+ private host;
559
+ constructor(config: PostHogConnectorConfig);
560
+ protected setup(): Promise<void>;
561
+ track(event: TelemetryEvent): void;
562
+ identify(event: IdentifyEvent): void;
563
+ page(event: PageEvent): void;
564
+ shutdown(): Promise<void>;
565
+ }
566
+ declare function createPostHogConnector(config: PostHogConnectorConfig): PostHogConnector;
567
+
568
+ export { type AnalyticsProvider, BaseConnector, CloudWatchEmfConnector, type CloudWatchEmfConnectorConfig, type Connector, type ConnectorConfig, type ConnectorFactory, type ConnectorFactoryFn, type ConnectorType, type ConnectorTypeName, ConsoleConnector, type ConsoleConnectorConfig, type DispatchResult, type DispatchResults, type IdentifyEvent, type MetricMapping, type MetricsProvider, MixpanelConnector, type MixpanelConnectorConfig, type ObservabilityProvider, OtelConnector, type OtelConnectorConfig, type PageEvent, PostHogConnector, type PostHogConnectorConfig, type ProviderConfig, type ProviderConfigs, type RouterConfig, type RouterPlugin, type TelemetryConfig, type TelemetryEvent, TelemetryRouter, type TelemetryRouterConfig, createCache, createCloudWatchEmfConnector, createConnector, createConnectorsFromConfig, createConsoleConnector, createMixpanelConnector, createOtelConnector, createPostHogConnector, createTelemetryRouter, debounce, deepMerge, detectEnvironment, extractFields, factoryRegistry, flattenObject, generateEventId, generateSessionId, getFactory, getRegisteredFactories, hasFactory, isObject, processBatch, registerFactory, retryWithBackoff, safeStringify, sanitizePayload, sleep, validateConfig };