Skip to content

Commit 2612bf5

Browse files
authored
Merge pull request #151 from ONSdigital/BLAIS5-4235
Promote from BLAIS5-4235 to main
2 parents 220847e + 36d590f commit 2612bf5

File tree

6 files changed

+16
-54
lines changed

6 files changed

+16
-54
lines changed

Makefile

-13
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,3 @@ test: lint check-types
2727

2828
requirements.txt:
2929
@poetry export -f requirements.txt --without-hashes --output requirements.txt
30-
31-
# TODO remove me
32-
.PHONY=deploy-log-error
33-
deploy-log-error: requirements.txt
34-
@gcloud functions deploy log-error \
35-
--region=europe-west2 \
36-
--entry-point=log_error \
37-
--trigger-http \
38-
--runtime=python39
39-
40-
# TODO remove me
41-
.PHONY=deploy
42-
deploy: deploy-slack-alerts deploy-log-error

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is a Cloud Function which is used to send alerts from Google Cloud Logging
44

55
## How it Works
66

7-
Some GCP infrastructure is required for this to working.
7+
Some GCP infrastructure is required for this to work.
88

99
1. **A PubSub Topic**:<br>
1010
Log messages will be put on this topic and trigger the Cloud Function.
@@ -50,30 +50,32 @@ This repository uses poetry. After cloning, install the dependencies by running:
5050
poetry install
5151
```
5252

53+
5354
### Makefile
5455

5556
A `Makefile` is included with some useful tasks to help with development.
5657
Running `make help` will list all available commands.
5758

58-
### GitHub Actions
59-
60-
The GitHub Actions run the linter, typechecker and tests.
59+
### Linting and Testing
6160

62-
To avoid getting failures, it's worth running `make test` before commit.
63-
Note, `make test` also runs the typechecker and linter.
61+
The [GitHub Actions](https://docs.github.com/en/actions), a CI/CD platform, runs the linter, typechecker and tests (using workflows), whenever a GitHub PR is raised.
6462

65-
### Linting Errors
63+
To minimise chances of failures when the GitHub Actions are ran, it's worth running `make test` before you push and commit to GitHub.
64+
Note, `make test` also runs the typechecker and linter.
6665

6766
Linting errors can usually be fixed quickly with `make format`.
6867

69-
### How to silence prod alerts
68+
69+
### How to silence specific event logs
70+
7071
1. Navigate to the log entry in GCP Console and copy the entry to the clipboard
7172
2. Create a test in the `test_main.py` file using the copied log entry
7273
3. Run tests using `make format test` - the test you just created should fail!
7374
4. Navigate to the `lib/filters` dir and create a new `.py` file
7475
5. Add new functionality to the newly created file (see `osconfig_agent_filter.py` for an example)
7576
6. Navigate to the `tests/lib/filters` dir and create a new `test_XX.py` file
7677
7. Create unit tests that test the actual filter functionality (again, check `test_osconfig_agent_filter.py` for an example). You will need to change the fixture!
78+
- **NB** Event logs can be difficult to replicate in a sandbox, so it is important that the unit tests are present and accurately written before it is deployed to an environment.
7779
8. In `send_alerts.py`, import the function you just created and add it to the filter array `[]` in the `log_entry_skipped` function
7880
```python
7981
def log_entry_skipped(log_entry: ProcessedLogEntry):
@@ -83,6 +85,6 @@ def log_entry_skipped(log_entry: ProcessedLogEntry):
8385
agent_connect_filter,
8486
... etc]
8587
```
86-
9. Run `make format test` - if all pass, push it up!
87-
88-
**NB.** Slack alerts will only be visible in "dev", "dev-training", "preprod" and "prod"
88+
9. Run `make format test` - if the checks pass, push and commit!
89+
10. Deploy the Cloud Function in a sandbox and ensure it works as expected.
90+
- **NB** Logs coming from sandboxes are filtered by default. If you want to reproduce error logs within a sandbox, make sure to remove `sandbox_filter` in `send_alerts/log_entry_skipped` before deploying the Cloud Function.

lib/cloud_run_revision/parse_event.py

-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,8 @@ def assert_field_in_event(field_name: str, event: dict) -> None:
1313
raise InvalidCloudRunRevisionEvent(f"Field '{field_name}' is missing.")
1414

1515

16-
def assert_is_v1_pubsub_message(event: dict) -> None:
17-
if event["@type"] != "type.googleapis.com/google.pubsub.v1.PubsubMessage":
18-
raise InvalidCloudRunRevisionEvent(
19-
"Field '@type' is 'type.googleapis.com/google.pubsub.v1.PubsubMessage'. "
20-
f"Got '{event['@type']}'"
21-
)
22-
23-
2416
def parse_event(event) -> Event:
2517
assert_field_in_event("data", event)
26-
assert_field_in_event("@type", event)
27-
assert_is_v1_pubsub_message(event)
2818

2919
try:
3020
return Event(data=json.loads(base64.b64decode(event["data"])))

poetry.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = ""
55
authors = ["Tom Oram <tom.oram@armakuni.com>"]
66

77
[tool.poetry.dependencies]
8-
python = "^3.8"
8+
python = "^3.9"
99
Flask = "^2.1.3"
1010
google-cloud-logging = "^3.2.1"
1111
requests = "^2.28.1"

tests/lib/cloud_functions/test_parse_event.py

-17
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ def event():
1919
}
2020

2121

22-
def test_parse_event_fails_when_type_does_not_exist(event):
23-
del event["@type"]
24-
with pytest.raises(InvalidCloudRunRevisionEvent) as e:
25-
parse_event(event)
26-
assert e.value.args[0] == "Field '@type' is missing."
27-
28-
29-
def test_parse_event_fails_when_type_is_not_v1_pubsub_message(event):
30-
event["@type"] = "unknown"
31-
with pytest.raises(InvalidCloudRunRevisionEvent) as e:
32-
parse_event(event)
33-
assert (
34-
e.value.args[0]
35-
== "Field '@type' is 'type.googleapis.com/google.pubsub.v1.PubsubMessage'. Got 'unknown'"
36-
)
37-
38-
3922
def test_parse_event_fails_when_data_does_not_exist(event):
4023
del event["data"]
4124
with pytest.raises(InvalidCloudRunRevisionEvent) as e:

0 commit comments

Comments
 (0)