Skip to content

Commit

Permalink
Merge pull request #17 from aws-samples/hotfix
Browse files Browse the repository at this point in the history
hotfix: events filtering
  • Loading branch information
mboulin authored Aug 22, 2023
2 parents 4909433 + ccf9c56 commit 0866508
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion cdk/lib/MetricsStack/cdk-metrics-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class MetricsStack extends NestedStack {

constructor(scope: Construct, id: string, props: MetricsStackProps) {
super(scope, id, props);
const region = Stack.of(this).region;

const parentStackName = Stack.of(this.nestedStackParent!).stackName;
const nestedStackName = 'Metrics';
Expand Down Expand Up @@ -194,7 +195,13 @@ export class MetricsStack extends NestedStack {
// Integration with EventBridge
new events.Rule(this, `${nestedStackName}-StreamEvents-Rule`, {
eventPattern: {
source: ['aws.ivs']
source: ['aws.ivs'],
region: [region],
detailType: [
'IVS Stream State Change',
'IVS Stream Health Change',
'IVS Limit Breach'
]
},
targets: [
new eventsTargets.ApiGateway(streamEventsApiGateway, {
Expand Down
20 changes: 15 additions & 5 deletions cdk/streamEventsApi/src/postStreamEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ const handler = async (
time: eventTime,
resources
} = request.body;

// Ignore "AWS API Call via CloudTrail" events
if (eventType === 'AWS API Call via CloudTrail') {
return;
}

console.info('Incoming Event', JSON.stringify(request.body));

let {
detail: { stream_id: streamId = '' }
} = request.body;
Expand Down Expand Up @@ -135,12 +143,16 @@ const handler = async (
additionalAttributes.hasErrorEvent = false;
additionalAttributes.isOpen = 'true';
}
} else if (eventName === SESSION_ENDED) {
}

if (eventName === SESSION_ENDED) {
additionalAttributes.endTime = eventTime;
attributesToRemove.push('isOpen');
}
if (eventType === LIMIT_BREACH_EVENT_TYPE)

if (eventType === LIMIT_BREACH_EVENT_TYPE) {
additionalAttributes.hasErrorEvent = true;
}

await updateStreamEvents({
additionalAttributes,
Expand All @@ -151,9 +163,7 @@ const handler = async (
userSub
});
} catch (error) {
console.error(error);
console.error(`Event body: ${JSON.stringify(request.body)}`);

console.error(error, JSON.stringify(request.body));
reply.statusCode = 500;

return reply.send({ __type: UNEXPECTED_EXCEPTION });
Expand Down

0 comments on commit 0866508

Please sign in to comment.