Skip to content

Commit ec82caa

Browse files
abueideclaude
andcommitted
feat(core): expose droppedEvents() on SegmentClient
Replace fragile log-interception pattern in e2e-cli with a proper counter on SegmentDestination. The CLI now calls client.droppedEvents() instead of monkey-patching logger.error and regex-matching drop messages. - Add droppedEventCount property to SegmentDestination - Add droppedEvents() accessor on SegmentClient (mirrors pendingEvents) - Remove interceptDropCount helper from e2e-cli Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c0515d7 commit ec82caa

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

e2e-cli/src/cli.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,6 @@ async function waitForQueueDrain(
168168
return false;
169169
}
170170

171-
/**
172-
* Intercepts logger.error() to count permanently dropped events.
173-
* Coupled to the log format in SegmentDestination.ts — matches:
174-
* "Dropped N events due to permanent errors"
175-
* "Dropped N events due to retry limit exceeded"
176-
*/
177-
function interceptDropCount(logger: Logger): () => number {
178-
let count = 0;
179-
const origError = logger.error;
180-
logger.error = (message?: unknown, ...rest: unknown[]) => {
181-
const match = String(message ?? '').match(/Dropped (\d+) events/);
182-
if (match) count += parseInt(match[1], 10);
183-
origError.call(logger, message, ...rest);
184-
};
185-
return () => {
186-
logger.error = origError;
187-
return count;
188-
};
189-
}
190-
191171
// ============================================================================
192172
// Main
193173
// ============================================================================
@@ -233,10 +213,9 @@ async function main() {
233213
}
234214
}
235215

236-
const getDropCount = interceptDropCount(logger);
237216
await client.flush();
238217
const drained = await waitForQueueDrain(client);
239-
const permanentDropCount = getDropCount();
218+
const permanentDropCount = client.droppedEvents();
240219

241220
const finalPending = drained ? 0 : await client.pendingEvents();
242221
const totalEvents = input.sequences.reduce(

packages/core/src/analytics.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,20 @@ export class SegmentClient {
10481048

10491049
return totalEventsCount;
10501050
}
1051+
1052+
/**
1053+
* Method to get count of events permanently dropped by SegmentDestination.
1054+
*/
1055+
droppedEvents(): number {
1056+
let count = 0;
1057+
for (const plugin of this.getPlugins()) {
1058+
if (plugin instanceof SegmentDestination) {
1059+
count += plugin.droppedEventCount;
1060+
}
1061+
}
1062+
return count;
1063+
}
1064+
10511065
private resumeTimeoutId?: ReturnType<typeof setTimeout>;
10521066
private waitingPlugins = new Set<WaitingPlugin>();
10531067

packages/core/src/plugins/SegmentDestination.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const SEGMENT_DESTINATION_KEY = 'Segment.io';
2222
export class SegmentDestination extends DestinationPlugin {
2323
type = PluginType.destination;
2424
key = SEGMENT_DESTINATION_KEY;
25+
droppedEventCount = 0;
2526
private apiHost?: string;
2627
private settingsResolve: () => void;
2728
private settingsPromise: Promise<void>;

0 commit comments

Comments
 (0)