-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathevent-processor.ts
More file actions
163 lines (148 loc) · 5.51 KB
/
Copy pathevent-processor.ts
File metadata and controls
163 lines (148 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { SDK_VERSION } from './utils/version';
import { ensureTrailingSlash } from './utils/ensureTrailingSlash';
import { LikeFetch } from './flagsmith-core';
import { IFlagsmithValue } from './types';
export const FLAG_EXPOSURE_EVENT = '$flag_exposure';
export const DEFAULT_EVENTS_API_URL = 'https://events.api.flagsmith.com/';
const DEFAULT_MAX_BUFFER = 1000;
const DEFAULT_FLUSH_INTERVAL = 10000;
const DEFAULT_RETRY_BACKOFF_MS = 1000;
export interface IEvent {
event: string;
feature_name: string | null;
identifier: string | null;
value: string | null;
traits: Record<string, IFlagsmithValue> | null;
metadata: Record<string, unknown> | null;
timestamp: number;
}
export interface EventProcessorOptions {
environmentKey: string;
fetch: LikeFetch;
eventsApiUrl?: string;
maxBuffer?: number;
flushInterval?: number;
log?: (...args: any[]) => void;
retryBackoffMs?: number;
}
interface EventArgs {
identifier: string | null;
value: IFlagsmithValue;
traits: Record<string, IFlagsmithValue> | null;
metadata: Record<string, unknown> | null;
}
interface TrackEventArgs extends EventArgs {
event: string;
}
interface TrackExposureArgs extends EventArgs {
featureName: string;
}
export class EventProcessor {
private endpoint: string;
private environmentKey: string;
private fetch: LikeFetch;
private log: (...args: any[]) => void;
private maxBuffer: number;
private flushInterval: number;
private retryBackoffMs: number;
private buffer: IEvent[] = [];
private dedupeKeys: Set<string> = new Set();
private timer: ReturnType<typeof setInterval> | null = null;
constructor(opts: EventProcessorOptions) {
const url = ensureTrailingSlash(opts.eventsApiUrl || DEFAULT_EVENTS_API_URL);
this.endpoint = `${url}v1/events`;
this.environmentKey = opts.environmentKey;
this.fetch = opts.fetch;
this.log = opts.log || (() => {});
this.maxBuffer = opts.maxBuffer ?? DEFAULT_MAX_BUFFER;
this.flushInterval = opts.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
this.retryBackoffMs = opts.retryBackoffMs ?? DEFAULT_RETRY_BACKOFF_MS;
}
trackEvent(args: TrackEventArgs) {
this.bufferEvent(args.event, null, args.identifier, args.value, args.traits, args.metadata, false);
}
trackExposureEvent(args: TrackExposureArgs) {
this.bufferEvent(FLAG_EXPOSURE_EVENT, args.featureName, args.identifier, args.value, args.traits, args.metadata, true);
}
private bufferEvent(
event: string,
feature_name: string | null,
identifier: string | null,
value: IFlagsmithValue,
traits: Record<string, IFlagsmithValue> | null,
metadata: Record<string, unknown> | null,
dedupe: boolean,
) {
const stringValue = value != null ? String(value) : null;
if (dedupe) {
const key = JSON.stringify([event, feature_name, identifier, stringValue]);
if (this.dedupeKeys.has(key)) return;
this.dedupeKeys.add(key);
}
this.buffer.push({
event,
feature_name,
identifier,
value: stringValue,
traits,
metadata: { ...(metadata || {}), sdk_version: SDK_VERSION },
timestamp: Date.now(),
});
if (this.buffer.length >= this.maxBuffer) {
this.flush();
}
}
flush = async (): Promise<void> => {
if (!this.buffer.length) return;
const events = this.buffer;
this.buffer = [];
this.dedupeKeys.clear();
await this.postBatch(events, 0);
};
start() {
// stop() clears any existing timer and flushes; the flush is a no-op
// when the buffer is empty, which is the usual case on (re)start.
this.stop();
if (this.flushInterval > 0) {
this.timer = setInterval(this.flush, this.flushInterval);
this.timer?.unref?.();
}
}
stop() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
// Best-effort, fire-and-forget: this flush is not awaited, so on rapid
// re-init buffered events may be lost. Callers needing a guarantee
// (SSR/teardown) should await flushEvents() instead.
this.flush();
}
private postBatch = async (events: IEvent[], attempt: number): Promise<void> => {
try {
const res = await this.fetch(this.endpoint, {
method: 'POST',
body: JSON.stringify({ events }),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Environment-Key': this.environmentKey,
...(SDK_VERSION ? { 'Flagsmith-SDK-User-Agent': `flagsmith-js-sdk/${SDK_VERSION}` } : {}),
},
});
if (!res.status || res.status < 200 || res.status >= 300) {
throw new Error(`Events: unexpected status ${res.status}`);
}
this.log('Events: flush successful');
} catch (err) {
if (attempt < 1) {
this.log('Events: flush failed, retrying', err);
await new Promise<void>((resolve) => {
const t = setTimeout(resolve, this.retryBackoffMs);
t?.unref?.();
});
return this.postBatch(events, attempt + 1);
}
this.log('Events: flush failed, dropping batch', err);
}
};
}