autotel-subscribers 32.0.0 → 32.1.0
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/architecture-snapshot.cjs +356 -0
- package/dist/architecture-snapshot.cjs.map +1 -0
- package/dist/architecture-snapshot.d.cts +96 -0
- package/dist/architecture-snapshot.d.ts +96 -0
- package/dist/architecture-snapshot.js +347 -0
- package/dist/architecture-snapshot.js.map +1 -0
- package/dist/{event-subscriber-base-CklwQ8C3.d.cts → event-subscriber-base-h285lBsH.d.cts} +1 -1
- package/dist/{event-subscriber-base-CklwQ8C3.d.ts → event-subscriber-base-h285lBsH.d.ts} +1 -1
- package/dist/factories.d.cts +1 -1
- package/dist/factories.d.ts +1 -1
- package/dist/index.cjs +167 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +163 -25
- package/dist/index.js.map +1 -1
- package/dist/posthog.d.cts +1 -1
- package/dist/posthog.d.ts +1 -1
- package/dist/slack.d.cts +1 -1
- package/dist/slack.d.ts +1 -1
- package/package.json +8 -3
- package/src/architecture-snapshot.test.ts +180 -0
- package/src/architecture-snapshot.ts +251 -0
- package/src/index.ts +8 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
// src/architecture-snapshot.ts
|
|
5
|
+
|
|
6
|
+
// src/event-subscriber-base.ts
|
|
7
|
+
var EventSubscriber = class {
|
|
8
|
+
/**
|
|
9
|
+
* Subscriber version (optional)
|
|
10
|
+
*/
|
|
11
|
+
version;
|
|
12
|
+
/**
|
|
13
|
+
* Enable/disable the subscriber (default: true)
|
|
14
|
+
*/
|
|
15
|
+
enabled = true;
|
|
16
|
+
/**
|
|
17
|
+
* Track pending requests for graceful shutdown
|
|
18
|
+
*/
|
|
19
|
+
pendingRequests = /* @__PURE__ */ new Set();
|
|
20
|
+
/**
|
|
21
|
+
* Optional: Handle errors
|
|
22
|
+
*
|
|
23
|
+
* Override this to customize error handling (logging, retries, etc.).
|
|
24
|
+
* Default behavior: log to console.error
|
|
25
|
+
*
|
|
26
|
+
* @param error - Error that occurred
|
|
27
|
+
* @param payload - Event payload that failed
|
|
28
|
+
*/
|
|
29
|
+
handleError(error, payload) {
|
|
30
|
+
console.error(
|
|
31
|
+
`[${this.name}] Failed to send ${payload.type}:`,
|
|
32
|
+
error,
|
|
33
|
+
payload
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Filter out undefined and null values from attributes
|
|
38
|
+
*
|
|
39
|
+
* This improves DX by allowing callers to pass objects with optional properties
|
|
40
|
+
* without having to manually filter them first.
|
|
41
|
+
*
|
|
42
|
+
* @param attributes - Input attributes (may contain undefined/null)
|
|
43
|
+
* @returns Filtered attributes with only defined values, or undefined if empty
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const filtered = this.filterAttributes({
|
|
48
|
+
* userId: user.id,
|
|
49
|
+
* email: user.email, // might be undefined
|
|
50
|
+
* plan: null, // will be filtered out
|
|
51
|
+
* });
|
|
52
|
+
* // Result: { userId: 'abc', email: 'test@example.com' } or { userId: 'abc' }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
filterAttributes(attributes) {
|
|
56
|
+
if (!attributes) return void 0;
|
|
57
|
+
const filtered = {};
|
|
58
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
59
|
+
if (value !== void 0 && value !== null) {
|
|
60
|
+
filtered[key] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return Object.keys(filtered).length > 0 ? filtered : void 0;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Track an event
|
|
67
|
+
*/
|
|
68
|
+
async trackEvent(name, attributes, options) {
|
|
69
|
+
if (!this.enabled) return;
|
|
70
|
+
const payload = {
|
|
71
|
+
type: "event",
|
|
72
|
+
name,
|
|
73
|
+
attributes,
|
|
74
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
75
|
+
autotel: options?.autotel
|
|
76
|
+
};
|
|
77
|
+
await this.send(payload);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Track a funnel step
|
|
81
|
+
*/
|
|
82
|
+
async trackFunnelStep(funnelName, step, attributes, options) {
|
|
83
|
+
if (!this.enabled) return;
|
|
84
|
+
const payload = {
|
|
85
|
+
type: "funnel",
|
|
86
|
+
name: `${funnelName}.${step}`,
|
|
87
|
+
funnel: funnelName,
|
|
88
|
+
step,
|
|
89
|
+
attributes,
|
|
90
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
91
|
+
autotel: options?.autotel
|
|
92
|
+
};
|
|
93
|
+
await this.send(payload);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Track an outcome
|
|
97
|
+
*/
|
|
98
|
+
async trackOutcome(operationName, outcome, attributes, options) {
|
|
99
|
+
if (!this.enabled) return;
|
|
100
|
+
const payload = {
|
|
101
|
+
type: "outcome",
|
|
102
|
+
name: `${operationName}.${outcome}`,
|
|
103
|
+
operation: operationName,
|
|
104
|
+
outcome,
|
|
105
|
+
attributes,
|
|
106
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
107
|
+
autotel: options?.autotel
|
|
108
|
+
};
|
|
109
|
+
await this.send(payload);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Track a value/metric
|
|
113
|
+
*/
|
|
114
|
+
async trackValue(name, value, attributes, options) {
|
|
115
|
+
if (!this.enabled) return;
|
|
116
|
+
const payload = {
|
|
117
|
+
type: "value",
|
|
118
|
+
name,
|
|
119
|
+
value,
|
|
120
|
+
attributes,
|
|
121
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
122
|
+
autotel: options?.autotel
|
|
123
|
+
};
|
|
124
|
+
await this.send(payload);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Track funnel progression with custom step names
|
|
128
|
+
*
|
|
129
|
+
* Unlike trackFunnelStep which uses FunnelStatus enum values,
|
|
130
|
+
* this method allows any string as the step name for flexible funnel tracking.
|
|
131
|
+
*
|
|
132
|
+
* @param funnelName - Name of the funnel (e.g., "checkout", "onboarding")
|
|
133
|
+
* @param stepName - Custom step name (e.g., "cart_viewed", "payment_entered")
|
|
134
|
+
* @param stepNumber - Optional numeric position in the funnel
|
|
135
|
+
* @param attributes - Optional event attributes
|
|
136
|
+
* @param options - Optional tracking options including autotel context
|
|
137
|
+
*/
|
|
138
|
+
async trackFunnelProgression(funnelName, stepName, stepNumber, attributes, options) {
|
|
139
|
+
if (!this.enabled) return;
|
|
140
|
+
const payload = {
|
|
141
|
+
type: "funnel",
|
|
142
|
+
name: `${funnelName}.${stepName}`,
|
|
143
|
+
funnel: funnelName,
|
|
144
|
+
step: stepName,
|
|
145
|
+
stepName,
|
|
146
|
+
stepNumber,
|
|
147
|
+
attributes,
|
|
148
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
149
|
+
autotel: options?.autotel
|
|
150
|
+
};
|
|
151
|
+
await this.send(payload);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Flush pending requests and clean up
|
|
155
|
+
*
|
|
156
|
+
* CRITICAL: Prevents race condition during shutdown
|
|
157
|
+
* 1. Disables subscriber to stop new events
|
|
158
|
+
* 2. Drains all pending requests (with retry logic)
|
|
159
|
+
* 3. Ensures flush guarantee
|
|
160
|
+
*
|
|
161
|
+
* Override this if you need custom cleanup logic (close connections, flush buffers, etc.),
|
|
162
|
+
* but ALWAYS call super.shutdown() first to drain pending requests.
|
|
163
|
+
*/
|
|
164
|
+
async shutdown() {
|
|
165
|
+
this.enabled = false;
|
|
166
|
+
const maxDrainAttempts = 10;
|
|
167
|
+
const drainIntervalMs = 50;
|
|
168
|
+
for (let attempt = 0; attempt < maxDrainAttempts; attempt++) {
|
|
169
|
+
if (this.pendingRequests.size === 0) {
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
await Promise.allSettled(this.pendingRequests);
|
|
173
|
+
if (this.pendingRequests.size > 0 && attempt < maxDrainAttempts - 1) {
|
|
174
|
+
await new Promise((resolve) => setTimeout(resolve, drainIntervalMs));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (this.pendingRequests.size > 0) {
|
|
178
|
+
console.warn(
|
|
179
|
+
`[${this.name}] Shutdown completed with ${this.pendingRequests.size} pending requests still in-flight. This may indicate a bug in the subscriber or extremely slow destination.`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Internal: Send payload and track request
|
|
185
|
+
*/
|
|
186
|
+
async send(payload) {
|
|
187
|
+
const request = this.sendWithErrorHandling(payload);
|
|
188
|
+
this.pendingRequests.add(request);
|
|
189
|
+
void request.finally(() => {
|
|
190
|
+
this.pendingRequests.delete(request);
|
|
191
|
+
});
|
|
192
|
+
return request;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Internal: Send with error handling
|
|
196
|
+
*/
|
|
197
|
+
async sendWithErrorHandling(payload) {
|
|
198
|
+
try {
|
|
199
|
+
await this.sendToDestination(payload);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
this.handleError(error, payload);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// src/architecture-snapshot.ts
|
|
207
|
+
var ARCHITECTURE_SNAPSHOT_SPEC = "autotel-architecture/v0.1.0";
|
|
208
|
+
var DEFAULT_MAX_SAMPLES = 3;
|
|
209
|
+
var ArchitectureSnapshotSubscriber = class extends EventSubscriber {
|
|
210
|
+
name = "ArchitectureSnapshotSubscriber";
|
|
211
|
+
service;
|
|
212
|
+
maxSampleTraceIds;
|
|
213
|
+
observations = /* @__PURE__ */ new Map();
|
|
214
|
+
constructor(config) {
|
|
215
|
+
super();
|
|
216
|
+
this.service = config.service;
|
|
217
|
+
this.maxSampleTraceIds = config.maxSampleTraceIds ?? DEFAULT_MAX_SAMPLES;
|
|
218
|
+
}
|
|
219
|
+
async sendToDestination(payload) {
|
|
220
|
+
if (payload.type !== "event") return;
|
|
221
|
+
const existing = this.observations.get(payload.name);
|
|
222
|
+
const now = payload.timestamp;
|
|
223
|
+
const traceId = payload.autotel?.trace_id;
|
|
224
|
+
const attrs = payload.attributes ?? {};
|
|
225
|
+
const autotelMeta = readAutotelMeta(attrs);
|
|
226
|
+
const fieldPaths = extractFieldPaths(stripAutotelMeta(attrs));
|
|
227
|
+
if (!existing) {
|
|
228
|
+
this.observations.set(payload.name, {
|
|
229
|
+
name: payload.name,
|
|
230
|
+
observedCount: 1,
|
|
231
|
+
firstSeen: now,
|
|
232
|
+
lastSeen: now,
|
|
233
|
+
fieldPaths,
|
|
234
|
+
sampleTraceIds: traceId ? [traceId] : [],
|
|
235
|
+
channel: autotelMeta.channel,
|
|
236
|
+
producer: autotelMeta.producer
|
|
237
|
+
});
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
existing.observedCount += 1;
|
|
241
|
+
existing.lastSeen = now;
|
|
242
|
+
existing.fieldPaths = mergeUnique(existing.fieldPaths, fieldPaths);
|
|
243
|
+
if (traceId && !existing.sampleTraceIds.includes(traceId) && existing.sampleTraceIds.length < this.maxSampleTraceIds) {
|
|
244
|
+
existing.sampleTraceIds.push(traceId);
|
|
245
|
+
}
|
|
246
|
+
existing.channel ??= autotelMeta.channel;
|
|
247
|
+
existing.producer ??= autotelMeta.producer;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Build the snapshot in memory. Use this in tests or when you want to
|
|
251
|
+
* inspect the result before writing it. Field paths and trace IDs are
|
|
252
|
+
* sorted so equal inputs always produce byte-identical snapshots.
|
|
253
|
+
*/
|
|
254
|
+
toSnapshot(now = () => /* @__PURE__ */ new Date()) {
|
|
255
|
+
const events = {};
|
|
256
|
+
const names = [...this.observations.keys()].toSorted();
|
|
257
|
+
for (const name of names) {
|
|
258
|
+
const obs = this.observations.get(name);
|
|
259
|
+
if (!obs) continue;
|
|
260
|
+
events[name] = {
|
|
261
|
+
...obs,
|
|
262
|
+
fieldPaths: obs.fieldPaths.toSorted(),
|
|
263
|
+
sampleTraceIds: obs.sampleTraceIds.toSorted()
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
spec: ARCHITECTURE_SNAPSHOT_SPEC,
|
|
268
|
+
generatedAt: now().toISOString(),
|
|
269
|
+
service: this.service,
|
|
270
|
+
events
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Write the snapshot to disk. Creates parent directories as needed.
|
|
275
|
+
* Files are written with a trailing newline so they diff cleanly in git.
|
|
276
|
+
*/
|
|
277
|
+
async writeToFile(filePath, options = {}) {
|
|
278
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
279
|
+
const json = JSON.stringify(this.toSnapshot(options.now), null, 2);
|
|
280
|
+
await fs.writeFile(filePath, json + "\n", "utf8");
|
|
281
|
+
}
|
|
282
|
+
/** Reset all accumulated state. Useful between test cases. */
|
|
283
|
+
reset() {
|
|
284
|
+
this.observations.clear();
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
function readAutotelMeta(attrs) {
|
|
288
|
+
const meta = attrs._autotel;
|
|
289
|
+
if (!meta || typeof meta !== "object") return {};
|
|
290
|
+
const m = meta;
|
|
291
|
+
return {
|
|
292
|
+
channel: typeof m.channel === "string" ? m.channel : void 0,
|
|
293
|
+
producer: typeof m.producer === "string" ? m.producer : void 0
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
var AUTOTEL_INJECTED_KEYS = /* @__PURE__ */ new Set([
|
|
297
|
+
"_autotel",
|
|
298
|
+
"traceId",
|
|
299
|
+
"trace_id",
|
|
300
|
+
"spanId",
|
|
301
|
+
"span_id",
|
|
302
|
+
"parentSpanId",
|
|
303
|
+
"parent_span_id",
|
|
304
|
+
"correlationId",
|
|
305
|
+
"correlation_id",
|
|
306
|
+
"service",
|
|
307
|
+
"service.name"
|
|
308
|
+
]);
|
|
309
|
+
function stripAutotelMeta(attrs) {
|
|
310
|
+
const out = {};
|
|
311
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
312
|
+
if (AUTOTEL_INJECTED_KEYS.has(key)) continue;
|
|
313
|
+
out[key] = value;
|
|
314
|
+
}
|
|
315
|
+
return out;
|
|
316
|
+
}
|
|
317
|
+
function extractFieldPaths(value, prefix = "") {
|
|
318
|
+
const paths = /* @__PURE__ */ new Set();
|
|
319
|
+
walk(value, prefix, paths);
|
|
320
|
+
return [...paths].toSorted();
|
|
321
|
+
}
|
|
322
|
+
function walk(value, prefix, out) {
|
|
323
|
+
if (value === null || value === void 0) return;
|
|
324
|
+
if (Array.isArray(value)) {
|
|
325
|
+
const arrayPrefix = prefix + "[]";
|
|
326
|
+
for (const item of value) walk(item, arrayPrefix, out);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (typeof value === "object") {
|
|
330
|
+
for (const [key, v] of Object.entries(value)) {
|
|
331
|
+
const path2 = prefix === "" ? key : `${prefix}.${key}`;
|
|
332
|
+
out.add(path2);
|
|
333
|
+
walk(v, path2, out);
|
|
334
|
+
}
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function mergeUnique(a, b) {
|
|
339
|
+
if (b.length === 0) return a;
|
|
340
|
+
const set = new Set(a);
|
|
341
|
+
for (const v of b) set.add(v);
|
|
342
|
+
return [...set];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export { ARCHITECTURE_SNAPSHOT_SPEC, ArchitectureSnapshotSubscriber, extractFieldPaths };
|
|
346
|
+
//# sourceMappingURL=architecture-snapshot.js.map
|
|
347
|
+
//# sourceMappingURL=architecture-snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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GAAG,CAAA;AAAA,IACnB;AACA,IAAA;AAAA,EACF;AAEF;AAEA,SAAS,WAAA,CAAY,GAAa,CAAA,EAAuB;AACvD,EAAA,IAAI,CAAA,CAAE,MAAA,KAAW,CAAA,EAAG,OAAO,CAAA;AAC3B,EAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,CAAC,CAAA;AACrB,EAAA,KAAA,MAAW,CAAA,IAAK,CAAA,EAAG,GAAA,CAAI,GAAA,CAAI,CAAC,CAAA;AAC5B,EAAA,OAAO,CAAC,GAAG,GAAG,CAAA;AAChB","file":"architecture-snapshot.js","sourcesContent":["/**\n * EventSubscriber - Standard base class for building custom subscribers\n *\n * This is the recommended base class for creating custom events subscribers.\n * It provides production-ready features out of the box:\n *\n * **Built-in Features:**\n * - **Error Handling**: Automatic error catching with customizable handlers\n * - **Pending Request Tracking**: Ensures all requests complete during shutdown\n * - **Graceful Shutdown**: Drains pending requests before closing\n * - **Enable/Disable**: Runtime control to turn subscriber on/off\n * - **Normalized Payload**: Consistent event structure across all event types\n *\n * **When to use:**\n * - Building custom subscribers for any platform\n * - Production deployments requiring reliability\n * - Need graceful shutdown and error handling\n *\n * @example Basic usage\n * ```typescript\n * import { EventSubscriber, EventPayload } from 'autotel-subscribers';\n *\n * class SnowflakeSubscriber extends EventSubscriber {\n * name = 'SnowflakeSubscriber';\n * version = '1.0.0';\n *\n * protected async sendToDestination(payload: EventPayload): Promise<void> {\n * await snowflakeClient.execute(\n * `INSERT INTO events VALUES (?, ?, ?)`,\n * [payload.type, payload.name, JSON.stringify(payload.attributes)]\n * );\n * }\n * }\n * ```\n *\n * @example With buffering\n * ```typescript\n * class BufferedSubscriber extends EventSubscriber {\n * name = 'BufferedSubscriber';\n * private buffer: EventPayload[] = [];\n *\n * protected async sendToDestination(payload: EventPayload): Promise<void> {\n * this.buffer.push(payload);\n *\n * if (this.buffer.length >= 100) {\n * await this.flush();\n * }\n * }\n *\n * async shutdown(): Promise<void> {\n * await super.shutdown(); // Drain pending requests first\n * await this.flush(); // Then flush buffer\n * }\n *\n * private async flush(): Promise<void> {\n * if (this.buffer.length === 0) return;\n *\n * const batch = [...this.buffer];\n * this.buffer = [];\n *\n * await apiClient.sendBatch(batch);\n * }\n * }\n * ```\n */\n\nimport type {\n EventSubscriber as IEventSubscriber,\n EventAttributes,\n EventAttributesInput,\n FunnelStatus,\n OutcomeStatus,\n AutotelEventContext,\n EventTrackingOptions,\n} from 'autotel/event-subscriber';\n\n// Re-export types for convenience\nexport type { AutotelEventContext, EventTrackingOptions } from 'autotel/event-subscriber';\n\n/**\n * Payload sent to destination\n */\nexport interface EventPayload {\n /** Event type: 'event', 'funnel', 'outcome', or 'value' */\n type: 'event' | 'funnel' | 'outcome' | 'value';\n\n /** Event name or metric name */\n name: string;\n\n /** Optional attributes */\n attributes?: EventAttributes;\n\n /** For funnel events: funnel name */\n funnel?: string;\n\n /** For funnel events: step status (from FunnelStatus enum) */\n step?: FunnelStatus | string;\n\n /** For funnel events: custom step name (from trackFunnelProgression) */\n stepName?: string;\n\n /** For funnel events: numeric position in funnel */\n stepNumber?: number;\n\n /** For outcome events: operation name */\n operation?: string;\n\n /** For outcome events: outcome status */\n outcome?: OutcomeStatus;\n\n /** For value events: numeric value */\n value?: number;\n\n /** Timestamp (ISO 8601) */\n timestamp: string;\n\n /**\n * Autotel trace context (present when events.includeTraceContext is enabled)\n *\n * Subscribers should map these to platform-specific field names:\n * - PostHog: autotel.trace_id → $trace_id\n * - Mixpanel: autotel.trace_id → trace_id\n */\n autotel?: AutotelEventContext;\n}\n\n/**\n * Standard base class for building custom events subscribers\n *\n * **What it provides:**\n * - Consistent payload structure (normalized across all event types)\n * - Enable/disable flag (runtime control)\n * - Automatic error handling (with customizable error handlers)\n * - Pending requests tracking (ensures no lost events during shutdown)\n * - Graceful shutdown (drains pending requests before closing)\n *\n * **Usage:**\n * Extend this class and implement `sendToDestination()`. All other methods\n * (trackEvent, trackFunnelStep, trackOutcome, trackValue, shutdown) are handled automatically.\n *\n * For high-throughput streaming platforms (Kafka, Kinesis, Pub/Sub), use `StreamingEventSubscriber` instead.\n */\nexport abstract class EventSubscriber implements IEventSubscriber {\n /**\n * Subscriber name (required for debugging)\n */\n abstract readonly name: string;\n\n /**\n * Subscriber version (optional)\n */\n readonly version?: string;\n\n /**\n * Enable/disable the subscriber (default: true)\n */\n protected enabled: boolean = true;\n\n /**\n * Track pending requests for graceful shutdown\n */\n private pendingRequests: Set<Promise<void>> = new Set();\n\n /**\n * Send payload to destination\n *\n * Override this method to implement your destination-specific logic.\n * This is called for all event types (event, funnel, outcome, value).\n *\n * @param payload - Normalized event payload\n */\n protected abstract sendToDestination(payload: EventPayload): Promise<void>;\n\n /**\n * Optional: Handle errors\n *\n * Override this to customize error handling (logging, retries, etc.).\n * Default behavior: log to console.error\n *\n * @param error - Error that occurred\n * @param payload - Event payload that failed\n */\n protected handleError(error: Error, payload: EventPayload): void {\n console.error(\n `[${this.name}] Failed to send ${payload.type}:`,\n error,\n payload,\n );\n }\n\n /**\n * Filter out undefined and null values from attributes\n *\n * This improves DX by allowing callers to pass objects with optional properties\n * without having to manually filter them first.\n *\n * @param attributes - Input attributes (may contain undefined/null)\n * @returns Filtered attributes with only defined values, or undefined if empty\n *\n * @example\n * ```typescript\n * const filtered = this.filterAttributes({\n * userId: user.id,\n * email: user.email, // might be undefined\n * plan: null, // will be filtered out\n * });\n * // Result: { userId: 'abc', email: 'test@example.com' } or { userId: 'abc' }\n * ```\n */\n protected filterAttributes(\n attributes?: EventAttributesInput,\n ): EventAttributes | undefined {\n if (!attributes) return undefined;\n\n const filtered: EventAttributes = {};\n for (const [key, value] of Object.entries(attributes)) {\n if (value !== undefined && value !== null) {\n filtered[key] = value;\n }\n }\n\n // Return undefined if no attributes remain after filtering\n return Object.keys(filtered).length > 0 ? filtered : undefined;\n }\n\n /**\n * Track an event\n */\n async trackEvent(\n name: string,\n attributes?: EventAttributes,\n options?: EventTrackingOptions,\n ): Promise<void> {\n if (!this.enabled) return;\n\n const payload: EventPayload = {\n type: 'event',\n name,\n attributes,\n timestamp: new Date().toISOString(),\n autotel: options?.autotel,\n };\n\n await this.send(payload);\n }\n\n /**\n * Track a funnel step\n */\n async trackFunnelStep(\n funnelName: string,\n step: FunnelStatus,\n attributes?: EventAttributes,\n options?: EventTrackingOptions,\n ): Promise<void> {\n if (!this.enabled) return;\n\n const payload: EventPayload = {\n type: 'funnel',\n name: `${funnelName}.${step}`,\n funnel: funnelName,\n step,\n attributes,\n timestamp: new Date().toISOString(),\n autotel: options?.autotel,\n };\n\n await this.send(payload);\n }\n\n /**\n * Track an outcome\n */\n async trackOutcome(\n operationName: string,\n outcome: OutcomeStatus,\n attributes?: EventAttributes,\n options?: EventTrackingOptions,\n ): Promise<void> {\n if (!this.enabled) return;\n\n const payload: EventPayload = {\n type: 'outcome',\n name: `${operationName}.${outcome}`,\n operation: operationName,\n outcome,\n attributes,\n timestamp: new Date().toISOString(),\n autotel: options?.autotel,\n };\n\n await this.send(payload);\n }\n\n /**\n * Track a value/metric\n */\n async trackValue(\n name: string,\n value: number,\n attributes?: EventAttributes,\n options?: EventTrackingOptions,\n ): Promise<void> {\n if (!this.enabled) return;\n\n const payload: EventPayload = {\n type: 'value',\n name,\n value,\n attributes,\n timestamp: new Date().toISOString(),\n autotel: options?.autotel,\n };\n\n await this.send(payload);\n }\n\n /**\n * Track funnel progression with custom step names\n *\n * Unlike trackFunnelStep which uses FunnelStatus enum values,\n * this method allows any string as the step name for flexible funnel tracking.\n *\n * @param funnelName - Name of the funnel (e.g., \"checkout\", \"onboarding\")\n * @param stepName - Custom step name (e.g., \"cart_viewed\", \"payment_entered\")\n * @param stepNumber - Optional numeric position in the funnel\n * @param attributes - Optional event attributes\n * @param options - Optional tracking options including autotel context\n */\n async trackFunnelProgression(\n funnelName: string,\n stepName: string,\n stepNumber?: number,\n attributes?: EventAttributes,\n options?: EventTrackingOptions,\n ): Promise<void> {\n if (!this.enabled) return;\n\n const payload: EventPayload = {\n type: 'funnel',\n name: `${funnelName}.${stepName}`,\n funnel: funnelName,\n step: stepName,\n stepName,\n stepNumber,\n attributes,\n timestamp: new Date().toISOString(),\n autotel: options?.autotel,\n };\n\n await this.send(payload);\n }\n\n /**\n * Flush pending requests and clean up\n *\n * CRITICAL: Prevents race condition during shutdown\n * 1. Disables subscriber to stop new events\n * 2. Drains all pending requests (with retry logic)\n * 3. Ensures flush guarantee\n *\n * Override this if you need custom cleanup logic (close connections, flush buffers, etc.),\n * but ALWAYS call super.shutdown() first to drain pending requests.\n */\n async shutdown(): Promise<void> {\n // 1. Stop accepting new events (prevents race condition)\n this.enabled = false;\n\n // 2. Drain pending requests with retry logic\n // Loop until empty to handle race where new requests added during Promise.allSettled\n const maxDrainAttempts = 10;\n const drainIntervalMs = 50;\n\n for (let attempt = 0; attempt < maxDrainAttempts; attempt++) {\n if (this.pendingRequests.size === 0) {\n break;\n }\n\n // Wait for current batch\n await Promise.allSettled(this.pendingRequests);\n\n // Small delay to catch any stragglers added during allSettled\n if (this.pendingRequests.size > 0 && attempt < maxDrainAttempts - 1) {\n await new Promise((resolve) => setTimeout(resolve, drainIntervalMs));\n }\n }\n\n // 3. Warn if we still have pending requests (shouldn't happen, but be defensive)\n if (this.pendingRequests.size > 0) {\n console.warn(\n `[${this.name}] Shutdown completed with ${this.pendingRequests.size} pending requests still in-flight. ` +\n `This may indicate a bug in the subscriber or extremely slow destination.`\n );\n }\n }\n\n /**\n * Internal: Send payload and track request\n */\n private async send(payload: EventPayload): Promise<void> {\n const request = this.sendWithErrorHandling(payload);\n this.pendingRequests.add(request);\n\n void request.finally(() => {\n this.pendingRequests.delete(request);\n });\n\n return request;\n }\n\n /**\n * Internal: Send with error handling\n */\n private async sendWithErrorHandling(\n payload: EventPayload,\n ): Promise<void> {\n try {\n await this.sendToDestination(payload);\n } catch (error) {\n this.handleError(error as Error, payload);\n }\n }\n}\n\nexport {\n type EventAttributes,\n type EventAttributesInput,\n type FunnelStatus,\n type OutcomeStatus,\n} from 'autotel/event-subscriber';","/**\n * ArchitectureSnapshotSubscriber\n *\n * Captures `track()` events into an in-memory architecture snapshot, then\n * writes it to disk. The snapshot is the input to `autotel-eventcatalog`'s\n * generator and is designed to be deterministic, reviewable, and committable.\n *\n * v0 scope: capture event names, observation counts, first/last-seen, sample\n * trace IDs, and the dotted field paths present in payloads. Producer /\n * consumer / channel attribution is read from a small `_autotel.*` convention\n * inside event attributes — that convention is documented in\n * `apps/example-eventcatalog`.\n *\n * @example\n * ```typescript\n * import { init, track } from 'autotel';\n * import { ArchitectureSnapshotSubscriber } from 'autotel-subscribers/architecture';\n *\n * const snapshot = new ArchitectureSnapshotSubscriber({ service: 'orders' });\n *\n * init({\n * service: 'orders',\n * subscribers: [snapshot],\n * });\n *\n * // ... exercise the system (run integration tests, hit endpoints, etc.) ...\n *\n * await snapshot.writeToFile('./.autotel/snapshot.json');\n * ```\n */\n\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { EventSubscriber, type EventPayload } from './event-subscriber-base';\n\n/**\n * Public, versioned snapshot format. The generator and any downstream tooling\n * target this spec. Bumping the spec version is a breaking change for\n * downstream consumers, so add fields rather than rename existing ones.\n */\nexport const ARCHITECTURE_SNAPSHOT_SPEC = 'autotel-architecture/v0.1.0' as const;\n\nexport type ArchitectureSnapshot = {\n spec: typeof ARCHITECTURE_SNAPSHOT_SPEC;\n generatedAt: string;\n service: string;\n events: Record<string, EventObservation>;\n};\n\nexport type EventObservation = {\n name: string;\n observedCount: number;\n firstSeen: string;\n lastSeen: string;\n /** Dotted field paths observed in any payload (e.g. `items[].sku`). */\n fieldPaths: string[];\n /** Up to 3 trace IDs for click-through from the catalog into the backend. */\n sampleTraceIds: string[];\n /** Channel the event was published on, if the caller provided `_autotel.channel`. */\n channel?: string;\n /** Service that produced the event, if not the snapshot's own service. */\n producer?: string;\n};\n\nexport interface ArchitectureSnapshotConfig {\n /** Service identifier that appears in the snapshot header. */\n service: string;\n /** Maximum number of trace IDs to retain per event (default 3). */\n maxSampleTraceIds?: number;\n}\n\nconst DEFAULT_MAX_SAMPLES = 3;\n\nexport class ArchitectureSnapshotSubscriber extends EventSubscriber {\n readonly name = 'ArchitectureSnapshotSubscriber';\n\n private readonly service: string;\n private readonly maxSampleTraceIds: number;\n private readonly observations = new Map<string, EventObservation>();\n\n constructor(config: ArchitectureSnapshotConfig) {\n super();\n this.service = config.service;\n this.maxSampleTraceIds = config.maxSampleTraceIds ?? DEFAULT_MAX_SAMPLES;\n }\n\n protected async sendToDestination(payload: EventPayload): Promise<void> {\n // Only `track()` events feed the architecture snapshot. Funnels, outcomes,\n // and value metrics belong to product analytics, not the architecture model.\n if (payload.type !== 'event') return;\n\n const existing = this.observations.get(payload.name);\n const now = payload.timestamp;\n const traceId = payload.autotel?.trace_id;\n const attrs = payload.attributes ?? {};\n const autotelMeta = readAutotelMeta(attrs);\n const fieldPaths = extractFieldPaths(stripAutotelMeta(attrs));\n\n if (!existing) {\n this.observations.set(payload.name, {\n name: payload.name,\n observedCount: 1,\n firstSeen: now,\n lastSeen: now,\n fieldPaths,\n sampleTraceIds: traceId ? [traceId] : [],\n channel: autotelMeta.channel,\n producer: autotelMeta.producer,\n });\n return;\n }\n\n existing.observedCount += 1;\n existing.lastSeen = now;\n existing.fieldPaths = mergeUnique(existing.fieldPaths, fieldPaths);\n\n if (\n traceId &&\n !existing.sampleTraceIds.includes(traceId) &&\n existing.sampleTraceIds.length < this.maxSampleTraceIds\n ) {\n existing.sampleTraceIds.push(traceId);\n }\n\n existing.channel ??= autotelMeta.channel;\n existing.producer ??= autotelMeta.producer;\n }\n\n /**\n * Build the snapshot in memory. Use this in tests or when you want to\n * inspect the result before writing it. Field paths and trace IDs are\n * sorted so equal inputs always produce byte-identical snapshots.\n */\n toSnapshot(now: () => Date = () => new Date()): ArchitectureSnapshot {\n const events: Record<string, EventObservation> = {};\n\n const names = [...this.observations.keys()].toSorted();\n for (const name of names) {\n const obs = this.observations.get(name);\n if (!obs) continue;\n events[name] = {\n ...obs,\n fieldPaths: obs.fieldPaths.toSorted(),\n sampleTraceIds: obs.sampleTraceIds.toSorted(),\n };\n }\n\n return {\n spec: ARCHITECTURE_SNAPSHOT_SPEC,\n generatedAt: now().toISOString(),\n service: this.service,\n events,\n };\n }\n\n /**\n * Write the snapshot to disk. Creates parent directories as needed.\n * Files are written with a trailing newline so they diff cleanly in git.\n */\n async writeToFile(\n filePath: string,\n options: { now?: () => Date } = {},\n ): Promise<void> {\n await fs.mkdir(path.dirname(filePath), { recursive: true });\n const json = JSON.stringify(this.toSnapshot(options.now), null, 2);\n await fs.writeFile(filePath, json + '\\n', 'utf8');\n }\n\n /** Reset all accumulated state. Useful between test cases. */\n reset(): void {\n this.observations.clear();\n }\n}\n\ntype AutotelMeta = {\n channel?: string;\n producer?: string;\n};\n\nfunction readAutotelMeta(attrs: Record<string, unknown>): AutotelMeta {\n const meta = attrs._autotel;\n if (!meta || typeof meta !== 'object') return {};\n const m = meta as Record<string, unknown>;\n return {\n channel: typeof m.channel === 'string' ? m.channel : undefined,\n producer: typeof m.producer === 'string' ? m.producer : undefined,\n };\n}\n\n/**\n * Top-level attribute keys that autotel injects automatically (correlation\n * context, baggage, service metadata). These describe the trace, not the\n * event payload, so they don't belong in the captured field paths.\n */\nconst AUTOTEL_INJECTED_KEYS = new Set([\n '_autotel',\n 'traceId',\n 'trace_id',\n 'spanId',\n 'span_id',\n 'parentSpanId',\n 'parent_span_id',\n 'correlationId',\n 'correlation_id',\n 'service',\n 'service.name',\n]);\n\nfunction stripAutotelMeta(attrs: Record<string, unknown>): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(attrs)) {\n if (AUTOTEL_INJECTED_KEYS.has(key)) continue;\n out[key] = value;\n }\n return out;\n}\n\n/**\n * Walk a JSON-like value and produce a sorted list of dotted field paths.\n * Arrays collapse with `[]`, so `items: [{ sku: 'x' }]` yields `items[].sku`.\n */\nexport function extractFieldPaths(value: unknown, prefix = ''): string[] {\n const paths = new Set<string>();\n walk(value, prefix, paths);\n return [...paths].toSorted();\n}\n\nfunction walk(value: unknown, prefix: string, out: Set<string>): void {\n if (value === null || value === undefined) return;\n if (Array.isArray(value)) {\n const arrayPrefix = prefix + '[]';\n for (const item of value) walk(item, arrayPrefix, out);\n return;\n }\n if (typeof value === 'object') {\n for (const [key, v] of Object.entries(value)) {\n const path = prefix === '' ? key : `${prefix}.${key}`;\n out.add(path);\n walk(v, path, out);\n }\n return;\n }\n // Primitives don't add new paths beyond what their key already added.\n}\n\nfunction mergeUnique(a: string[], b: string[]): string[] {\n if (b.length === 0) return a;\n const set = new Set(a);\n for (const v of b) set.add(v);\n return [...set];\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventAttributes, FunnelStatus, OutcomeStatus, AutotelEventContext,
|
|
1
|
+
import { EventSubscriber as EventSubscriber$1, EventAttributes, FunnelStatus, OutcomeStatus, AutotelEventContext, EventAttributesInput, EventTrackingOptions } from 'autotel/event-subscriber';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* EventSubscriber - Standard base class for building custom subscribers
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventAttributes, FunnelStatus, OutcomeStatus, AutotelEventContext,
|
|
1
|
+
import { EventSubscriber as EventSubscriber$1, EventAttributes, FunnelStatus, OutcomeStatus, AutotelEventContext, EventAttributesInput, EventTrackingOptions } from 'autotel/event-subscriber';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* EventSubscriber - Standard base class for building custom subscribers
|
package/dist/factories.d.cts
CHANGED
|
@@ -5,7 +5,7 @@ export { AmplitudeConfig } from './amplitude.cjs';
|
|
|
5
5
|
export { SegmentConfig } from './segment.cjs';
|
|
6
6
|
export { WebhookConfig } from './webhook.cjs';
|
|
7
7
|
export { SlackSubscriberConfig } from './slack.cjs';
|
|
8
|
-
import './event-subscriber-base-
|
|
8
|
+
import './event-subscriber-base-h285lBsH.cjs';
|
|
9
9
|
import 'posthog-node';
|
|
10
10
|
|
|
11
11
|
/**
|
package/dist/factories.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { AmplitudeConfig } from './amplitude.js';
|
|
|
5
5
|
export { SegmentConfig } from './segment.js';
|
|
6
6
|
export { WebhookConfig } from './webhook.js';
|
|
7
7
|
export { SlackSubscriberConfig } from './slack.js';
|
|
8
|
-
import './event-subscriber-base-
|
|
8
|
+
import './event-subscriber-base-h285lBsH.js';
|
|
9
9
|
import 'posthog-node';
|
|
10
10
|
|
|
11
11
|
/**
|