Skip to content

Commit 4854ff1

Browse files
committed
refactor tagging
1 parent 2250663 commit 4854ff1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/ExpressBridge.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,7 @@ export class ExpressBridge {
6868
},
6969
});
7070

71-
const matchedPatterns = this.comparableCollection.filter(
72-
(eventPattern: EventPattern<Partial<typeof incomingEvent>>) => {
73-
console.log('testing pattern: ', eventPattern);
74-
console.log('against... ', incomingEvent);
75-
return eventPattern.test(incomingEvent);
76-
}
77-
);
78-
79-
console.log('Matched patterns: ', matchedPatterns);
80-
console.log('Did we match patterns?: ', matchedPatterns.length > 0);
81-
console.log('patterns to match against: ', this.comparableCollection);
71+
const matchedPatterns = this.match(incomingEvent);
8272

8373
if (matchedPatterns.length > 0) {
8474
await this.telemetry?.beacon('EB-MATCH', {
@@ -147,6 +137,16 @@ export class ExpressBridge {
147137
public getTelemetryId() {
148138
return this.telemetry.eb_event_id;
149139
}
140+
141+
private match(
142+
incomingEvent: Record<string, any>
143+
): EventPattern<typeof incomingEvent>[] {
144+
return this.comparableCollection.filter(
145+
(eventPattern: EventPattern<Partial<typeof incomingEvent>>) => {
146+
return eventPattern.test(incomingEvent as any);
147+
}
148+
);
149+
}
150150
}
151151

152152
function pipeline(message: EventType, ...functions: handlerType[]): EventType {

src/Telemetry.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { v4 } from 'uuid';
23
import { AxiosRequestConfig } from 'axios';
34

45
export type TelemetryConfig = Partial<AxiosRequestConfig> & {
@@ -30,4 +31,20 @@ export class Telemetry {
3031
console.log('Error calling telemetry beacon', err);
3132
}
3233
}
34+
35+
public tagEvent(event: Record<string, any>): Record<string, any> {
36+
const { body, detail, Records } = event;
37+
let payload = body ?? detail ?? Records;
38+
39+
const tag = v4();
40+
if (Array.isArray(payload)) {
41+
for (const record of payload) {
42+
record.eb_event_id = record.eb_event_id || tag;
43+
}
44+
} else {
45+
payload = typeof payload === 'string' ? JSON.parse(payload) : payload;
46+
payload.eb_event_id = payload.eb_event_id || tag;
47+
}
48+
return event;
49+
}
3350
}

0 commit comments

Comments
 (0)