-
Notifications
You must be signed in to change notification settings - Fork 59
feat(publisher): degrade PubSubPublisher to noop when GCP credentials are absent #388
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
Open
julietshen
wants to merge
18
commits into
main
Choose a base branch
from
julietshen/noop-publisher-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
05c0cdc
feat(publisher): degrade PubSubPublisher to noop when GCP credentials…
julietshen 0d5f06c
Merge branch 'main' into julietshen/noop-publisher-fallback
julietshen f7d7fd6
fix(publisher): address review feedback on noop fallback
julietshen 641717a
docs(changelog): note PubSubPublisher noop fallback
julietshen 3b40c28
test(publisher): force GCP creds in validation-exporter conftest
julietshen 749156e
test(publisher): scope forced-creds patch to function to stop leak
julietshen f0cee7c
feat(publisher): add DISABLE_GCP_PUBSUB opt-out, drop per-send noop m…
julietshen b678c6f
feat(async-publisher): degrade AsyncPubSubPublisher to noop without G…
julietshen e8e2770
docs(changelog): cover both sync and async publisher noop fallback
julietshen 362a860
test(publisher): point exporter conftest at renamed cred helper
julietshen 48cefda
feat(async-publisher): add DISABLE_GCP_PUBSUB opt-out, drop noop metric
julietshen 6cbe134
style(test): collapse test signature to satisfy ruff format
julietshen 2d40415
feat(publisher): emit startup configuration.errors metric on missing …
julietshen 6b84b57
refactor(publisher): move enable/disable decision into a make_publish…
julietshen e825f47
test: drop vestigial cred patch from exporter conftest
julietshen b1adab9
refactor(publisher): opt-in OSPREY_PUBSUB_ENABLED, catch creds error …
julietshen f0b8f6e
refactor(publisher): default OSPREY_PUBSUB_ENABLED to true, restore p…
julietshen 707a180
docs(changelog): scope the entry to the publisher noop, not full GCP-…
julietshen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
osprey_worker/src/osprey/worker/lib/tests/test_publisher.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from typing import Iterator | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from google.auth.exceptions import DefaultCredentialsError | ||
| from osprey.worker.lib import publisher | ||
| from osprey.worker.lib.publisher import PubSubPublisher, _check_gcp_credentials | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_cred_cache() -> Iterator[None]: | ||
| publisher._gcp_credentials_available = None | ||
| yield | ||
| publisher._gcp_credentials_available = None | ||
|
|
||
|
|
||
| def test_check_gcp_credentials_true_when_default_resolves() -> None: | ||
| with patch.object(publisher.google.auth, 'default', return_value=(MagicMock(), 'proj')): | ||
| assert _check_gcp_credentials() is True | ||
|
|
||
|
|
||
| def test_check_gcp_credentials_false_when_default_raises() -> None: | ||
| with patch.object(publisher.google.auth, 'default', side_effect=DefaultCredentialsError()): | ||
| assert _check_gcp_credentials() is False | ||
|
|
||
|
|
||
| def test_check_gcp_credentials_is_cached() -> None: | ||
| with patch.object(publisher.google.auth, 'default', return_value=(MagicMock(), 'proj')) as default_mock: | ||
| _check_gcp_credentials() | ||
| _check_gcp_credentials() | ||
| _check_gcp_credentials() | ||
| assert default_mock.call_count == 1 | ||
|
|
||
|
|
||
| def test_pubsub_publisher_constructs_client_when_creds_present() -> None: | ||
| with ( | ||
| patch.object(publisher.google.auth, 'default', return_value=(MagicMock(), 'proj')), | ||
| patch.object(publisher, 'BatchPubsubPublisherClient') as client_cls, | ||
| ): | ||
| pub = PubSubPublisher('proj', 'topic') | ||
| assert pub._enabled is True | ||
| assert client_cls.called | ||
|
|
||
|
|
||
| def test_pubsub_publisher_noops_when_creds_absent(caplog: pytest.LogCaptureFixture) -> None: | ||
| with ( | ||
| patch.object(publisher.google.auth, 'default', side_effect=DefaultCredentialsError()), | ||
| patch.object(publisher, 'BatchPubsubPublisherClient') as client_cls, | ||
| ): | ||
| with caplog.at_level('WARNING', logger=publisher.logger.name): | ||
| pub = PubSubPublisher('proj', 'topic') | ||
| assert pub._enabled is False | ||
| assert not client_cls.called | ||
| assert 'noop mode' in caplog.text | ||
| assert 'project=proj' in caplog.text | ||
| assert 'topic=topic' in caplog.text | ||
|
|
||
|
|
||
| def test_publish_short_circuits_and_emits_noop_metric_when_disabled() -> None: | ||
| with ( | ||
| patch.object(publisher.google.auth, 'default', side_effect=DefaultCredentialsError()), | ||
| patch.object(publisher, 'metrics') as metrics_mock, | ||
| ): | ||
| pub = PubSubPublisher('proj', 'topic') | ||
| pub.publish(MagicMock()) | ||
| metrics_mock.increment.assert_called_once_with( | ||
| 'PubSubPublisher.publisher.noop', | ||
| tags=['project:proj', 'topic:projects/proj/topics/topic'], | ||
| ) | ||
|
|
||
|
|
||
| def test_stop_short_circuits_when_disabled() -> None: | ||
| with patch.object(publisher.google.auth, 'default', side_effect=DefaultCredentialsError()): | ||
| pub = PubSubPublisher('proj', 'topic') | ||
| pub.stop() | ||
| assert not hasattr(pub, '_publisher') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure on the usage of another metric here, and I think the dashboard could probably just surface this as a warning banner or something "Publishing to GCP PubSub is currently disabled due to invalid credentials"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooooh good idea! I'll work that in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm actually going to add that separately, since it's a UI change and i want to keep this and #411 focused on the publisher changes