Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Eventbridge v2: Add pattern matching #10664

Merged
merged 28 commits into from May 7, 2024

Conversation

maxhoheiser
Copy link
Member

@maxhoheiser maxhoheiser commented Apr 15, 2024

Motivation

This PR adds pattern matching to distribute incoming events based on event bus specified in the event, rules in the matching event bus and matches the patterns in the rules.
It uses the existing rule matching algorithm from v1, which is limited, e.g. patterns with anything but or exists = True.

Changes

Add parsing for event pattern to a dictionary, and add validating of pattern.
Add correct formatting of events and add additional keys.
Add API method to test event pattern using same matching algorithm as for distributing events to targets.
Remove skip markers from now passing tests.

Testing

Set the env variable PROVIDER_OVERRIDE_EVENTS=v2 and run tests in events folder. All unsupported tests have the appropriate skip markers.

@maxhoheiser maxhoheiser added aws:events Amazon EventBridge semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases labels Apr 15, 2024
@maxhoheiser maxhoheiser self-assigned this Apr 15, 2024
Copy link

github-actions bot commented Apr 16, 2024

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 37m 53s ⏱️ -55s
2 926 tests ±0  2 632 ✅  - 2  294 💤 +2  0 ❌ ±0 
2 928 runs  ±0  2 632 ✅  - 2  296 💤 +2  0 ❌ ±0 

Results for commit aae673a. ± Comparison against base commit 0f7d49f.

This pull request removes 5 and adds 5 tests. Note that renamed tests count towards both.
tests.aws.services.events.test_events.TestEvents ‑ test_event_pattern
tests.aws.services.events.test_events.TestEvents ‑ test_put_event_without_source
tests.aws.services.events.test_events.TestEvents ‑ test_put_events_with_nested_event_pattern
tests.aws.services.events.test_events.TestEvents ‑ test_put_events_with_values_in_array
tests.aws.services.events.test_events.TestEvents ‑ test_put_target_id_validation
tests.aws.services.events.test_event_patterns ‑ test_event_pattern_source
tests.aws.services.events.test_events.TestEventPattern ‑ test_put_events_pattern_nested
tests.aws.services.events.test_events.TestEventPattern ‑ test_put_events_pattern_with_values_in_array
tests.aws.services.events.test_events.TestEventTarget ‑ test_put_target_id_validation
tests.aws.services.events.test_events.TestEvents ‑ test_put_events_without_source
This pull request skips 1 test.
tests.aws.services.events.test_events.TestEvents ‑ test_put_event_without_detail

♻️ This comment has been updated with latest results.

except KeyError:
LOG.error(f"Error deleting target worker {target_arn}.")

def _put_entries(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to a more meaningful function name, also include docstring

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe dispatch_events, deliver_events, ...?

The API is called put_entries but that's from a user's perspective. Within the EventBridge service, using active terminology such as dispatch makes more sense. Just reading over the AWS docs and drafting a docstring might help decide upon a better name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed in: da0f12a

@maxhoheiser maxhoheiser changed the base branch from master to feature/eventbridge_v2-curd-alpha April 22, 2024 13:28
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-pattern-matching branch 7 times, most recently from d47fceb to 4153a06 Compare April 22, 2024 15:05
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-curd-alpha branch from a5ce64c to 24ed318 Compare April 22, 2024 21:10
Base automatically changed from feature/eventbridge_v2-curd-alpha to master April 23, 2024 08:37
@joe4dev joe4dev added this to the 3.5 milestone Apr 24, 2024
@maxhoheiser maxhoheiser modified the milestones: 3.5, Playground Apr 24, 2024
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-pattern-matching branch 9 times, most recently from bc536f6 to 08b80f9 Compare April 25, 2024 22:04
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-pattern-matching branch from 6c48233 to b011d8a Compare May 7, 2024 06:59
Copy link
Member

@joe4dev joe4dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Can we avoid duplicating the InternalInvalidEventPatternException before merging?
There are also a couple of enabled tests, which still have the unknown marker (see comments).

localstack/services/events/models_v2.py Outdated Show resolved Hide resolved
localstack/services/events/event_ruler.py Show resolved Hide resolved
"time": get_event_time(event),
"region": region,
"resources": event.get("Resources", []),
"detail": json.loads(event.get("Detail", "{}")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the AWS docs, detail should always be a JSON object. Hence, I guess we can safely assume that other strings or base64-encoded content is not expected here, right?

detail — A JSON object that contains information about the event. For more information about what can be included in this field, see Event message detail field.

}
)
return failed_entries
event_pattern = rule.event_pattern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: event_pattern_str or str typing could clarify the type here (especially given we have both versions in our codebase)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now for the v2 provider, since we use only the Java matching engine we only ever have event pattern as a string, we don't parse it into a JSON object. In addition, the API string type is called EventPattern, therefor I would keep it without the str suffix.
Also, I think the naming convention of match_rule is misleading since we don't match the rule but the rule_pattern and only pass in the pattern. We should think of updating this in the event_ruler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now for the v2 provider, since we use only the Java matching engine we only ever have event pattern as a string, we don't parse it into a JSON object. In addition, the API string type is called EventPattern, therefor I would keep it without the str suffix.

It's a good thing we don't need JSON parsing for the event pattern anymore in the new provider, but right one line below we have the counter-example of transforming an event into an event_str 😬 . Looking over the many implementations in LocalStack, this str vs json duality was very confusing, and being explicit can help to clarify the API usage here.


How do you think does rule differ from rule_pattern?

the naming convention of match_rule is misleading since we don't match the rule but the rule_pattern and only pass in the pattern.

Background

The terminology Ruler.matchesRule(event, rule) in the event_ruler.py follows the official AWS event-ruler and aims to be more generic than just for EventBridge. Notice that different services use slightly different terminology:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json vs strg makes sense - the old fallacy if you build it you know it and think its obvious to everyone else

tests/aws/services/events/test_events.py Show resolved Hide resolved
tests/aws/services/events/test_events_integrations.py Outdated Show resolved Hide resolved
tests/aws/services/events/test_events_integrations.py Outdated Show resolved Hide resolved
tests/aws/services/events/test_events_integrations.py Outdated Show resolved Hide resolved
tests/aws/services/events/test_events_integrations.py Outdated Show resolved Hide resolved
tests/aws/services/events/test_events.py Outdated Show resolved Hide resolved
@maxhoheiser maxhoheiser force-pushed the feature/eventbridge_v2-add-pattern-matching branch from d2b4586 to aae673a Compare May 7, 2024 11:41
@maxhoheiser maxhoheiser merged commit 21b57ab into master May 7, 2024
30 checks passed
@maxhoheiser maxhoheiser deleted the feature/eventbridge_v2-add-pattern-matching branch May 7, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:events Amazon EventBridge semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants