Step 2: Route by Geographic Region
In addition to routing on content like severity, a critical routing pattern is based on geography. For regulations like GDPR, you must ensure that data from a specific region is handled in a compliant way.
This step teaches you how to add a geographic routing rule that takes precedence over your existing severity-based rules.
The Goal
You will modify your switch output so that any message with region: "EU" is sent to a special, dedicated destination for GDPR compliance, regardless of its severity. All non-EU data will continue to be routed by severity as before.
Implementation
-
Start with the Previous Pipeline: Copy the
severity-router.yamlfrom Step 1 to a new file namedgeo-router.yaml.cp severity-router.yaml geo-router.yaml -
Add the Geographic Routing Rule: Open
geo-router.yamland add a newcaseto the top of yourswitchblock. Because theswitchstops at the first match, placing this rule at the top gives it the highest priority.Modify the 'output' in geo-router.yamloutput:
switch:
cases:
# --- START: New addition ---
# Case 1: Handle all EU data first for GDPR compliance.
- check: this.region == "EU"
output:
file:
path: /tmp/eu-data.jsonl # A dedicated, compliant destination
codec: lines
# --- END: New addition ---
# Case 2: Handle CRITICAL events (for non-EU data)
- check: this.severity == "CRITICAL"
output:
file: # Your PagerDuty/alerting output from Step 1
path: /tmp/critical.jsonl
codec: lines
# Case 3: Handle WARN events (for non-EU data)
- check: this.severity == "WARN"
output:
file: # Your Slack/monitoring output from Step 1
path: /tmp/warnings.jsonl
codec: lines
# Case 4 (Default): Handle all other non-EU events
- output:
file: # Your Elasticsearch/logging output from Step 1
path: /tmp/other.jsonl
codec: lines -
Deploy and Test:
# Send a CRITICAL event from the EU region
curl -X POST http://localhost:8080/events \
-H "Content-Type: application/json" \
-d '{"region": "EU", "severity": "CRITICAL", "message": "This must go to the EU file"}'
# Send a CRITICAL event from the US region
curl -X POST http://localhost:8080/events \
-H "Content-Type: application/json" \
-d '{"region": "US", "severity": "CRITICAL", "message": "This must go to the critical file"}' -
Verify: Check your output files.
- The first event, despite being
CRITICAL, will be ineu-data.jsonlbecause the first rule in theswitchmatched it. - The second event will be in
critical.jsonlbecause it did not match the EU rule and fell through to the severity-based routing.
- The first event, despite being
You have now implemented a priority-based routing system that handles compliance rules before falling back to other business logic.