Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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 Jun 29, 2026
0d5f06c
Merge branch 'main' into julietshen/noop-publisher-fallback
julietshen Jul 6, 2026
f7d7fd6
fix(publisher): address review feedback on noop fallback
julietshen Jul 6, 2026
641717a
docs(changelog): note PubSubPublisher noop fallback
julietshen Jul 7, 2026
3b40c28
test(publisher): force GCP creds in validation-exporter conftest
julietshen Jul 7, 2026
749156e
test(publisher): scope forced-creds patch to function to stop leak
julietshen Jul 9, 2026
f0cee7c
feat(publisher): add DISABLE_GCP_PUBSUB opt-out, drop per-send noop m…
julietshen Jul 10, 2026
b678c6f
feat(async-publisher): degrade AsyncPubSubPublisher to noop without G…
julietshen Jul 7, 2026
e8e2770
docs(changelog): cover both sync and async publisher noop fallback
julietshen Jul 7, 2026
362a860
test(publisher): point exporter conftest at renamed cred helper
julietshen Jul 9, 2026
48cefda
feat(async-publisher): add DISABLE_GCP_PUBSUB opt-out, drop noop metric
julietshen Jul 10, 2026
6cbe134
style(test): collapse test signature to satisfy ruff format
julietshen Jul 10, 2026
2d40415
feat(publisher): emit startup configuration.errors metric on missing …
julietshen Jul 10, 2026
6b84b57
refactor(publisher): move enable/disable decision into a make_publish…
julietshen Jul 10, 2026
e825f47
test: drop vestigial cred patch from exporter conftest
julietshen Jul 10, 2026
b1adab9
refactor(publisher): opt-in OSPREY_PUBSUB_ENABLED, catch creds error …
julietshen Jul 10, 2026
f0b8f6e
refactor(publisher): default OSPREY_PUBSUB_ENABLED to true, restore p…
julietshen Jul 13, 2026
707a180
docs(changelog): scope the entry to the publisher noop, not full GCP-…
julietshen Jul 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ For more information about each release including git tags and artifacts, see [R
- Event stream shows sensible defaults so first-load isn't empty ([#297](https://github.com/roostorg/osprey/pull/297) by [@haileyok](https://github.com/haileyok))
- Replace `react-scripts` with `rsbuild`/`rspack` for UI builds ([#235](https://github.com/roostorg/osprey/pull/235) by [@chimosky](https://github.com/chimosky))
- Migrate from npm to pnpm via Corepack ([#252](https://github.com/roostorg/osprey/pull/252) by [@haileyok](https://github.com/haileyok))
- `PubSubPublisher` degrades to a noop when GCP credentials are absent or when `DISABLE_GCP_PUBSUB` is set, so Osprey runs without GCP config instead of failing ([#388](https://github.com/roostorg/osprey/pull/388) by [@julietshen](https://github.com/julietshen))

### Fixed

Expand Down
25 changes: 23 additions & 2 deletions osprey_async_worker/src/osprey/async_worker/lib/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from google.api_core.retry import Retry
from google.cloud import pubsub_v1
from osprey.worker.lib.gcp_credentials import gcp_credentials_available
from osprey.worker.lib.instruments import metrics
from pydantic import BaseModel

Expand All @@ -29,6 +30,13 @@ class AsyncPubSubPublisher:

Messages are buffered in an asyncio.Queue and flushed either when
the batch reaches max_messages or after max_latency_seconds.

Degrades to noop mode when GCP credentials cannot be resolved at construction
time (e.g. local dev or adopter environments without GCP). In noop mode no
underlying client is built, publish paths return immediately, and an
`async_pubsub_publisher.publish.noop` metric is incremented per call so the
inert state is visible in dashboards. A one-time warning is logged at
construction.
"""

def __init__(
Expand All @@ -39,15 +47,23 @@ def __init__(
max_latency_seconds: float = 1.0,
):
self._topic_path = f'projects/{project_id}/topics/{topic_id}'
self._metric_tags = [f'project:{project_id}', f'topic:{topic_id}']
self._flush_task: asyncio.Task[None] | None = None
self._enabled = gcp_credentials_available()
if not self._enabled:
logger.warning(
'GCP credentials not detected, AsyncPubSubPublisher running in noop mode (project=%s, topic=%s)',
project_id,
topic_id,
)
return
self._client = pubsub_v1.PublisherClient(
batch_settings=pubsub_v1.types.BatchSettings(max_messages=1),
)
self._queue: asyncio.Queue[bytes] = asyncio.Queue()
self._max_messages = max_messages
self._max_latency = max_latency_seconds
self._flush_task: asyncio.Task[None] | None = None
self._started = False
self._metric_tags = [f'project:{project_id}', f'topic:{topic_id}']

def _ensure_started(self) -> None:
"""Start the background flush task on first publish."""
Expand Down Expand Up @@ -129,6 +145,9 @@ def publish(self, data: BaseModel) -> None:

def publish_bytes(self, data: bytes) -> None:
"""Queue raw bytes for async batched publishing."""
if not self._enabled:
metrics.increment('async_pubsub_publisher.publish.noop', tags=self._metric_tags)
return
self._ensure_started()
try:
self._queue.put_nowait(data)
Expand All @@ -138,6 +157,8 @@ def publish_bytes(self, data: bytes) -> None:

async def stop(self) -> None:
"""Flush remaining messages and stop."""
if not self._enabled:
return
if self._flush_task is not None:
self._flush_task.cancel()
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

from unittest.mock import MagicMock, patch

import pytest
from google.api_core.exceptions import NotFound
from osprey.async_worker.lib import publisher as publisher_module
from osprey.async_worker.lib.publisher import _PUBLISH_RETRY, AsyncPubSubPublisher


def _make_publisher():
"""Return an AsyncPubSubPublisher with a mocked PublisherClient."""
with patch('osprey.async_worker.lib.publisher.pubsub_v1.PublisherClient'):
"""Return an enabled AsyncPubSubPublisher with a mocked PublisherClient."""
with (
patch('osprey.async_worker.lib.publisher.gcp_credentials_available', return_value=True),
patch('osprey.async_worker.lib.publisher.pubsub_v1.PublisherClient'),
):
publisher = AsyncPubSubPublisher(project_id='proj', topic_id='topic')
publisher._client = MagicMock()
return publisher
Expand Down Expand Up @@ -48,3 +53,38 @@ def test_permanent_failure_metric_fires(mock_metrics):
assert len(failure_calls) == 1
assert failure_calls[0][0][0] == 'async_pubsub_publisher.publish.failure'
assert f'error:{exc.__class__.__name__}' in failure_calls[0][1]['tags']


def test_noops_when_creds_absent(caplog: pytest.LogCaptureFixture) -> None:
with (
patch('osprey.async_worker.lib.publisher.gcp_credentials_available', return_value=False),
patch('osprey.async_worker.lib.publisher.pubsub_v1.PublisherClient') as client_cls,
):
with caplog.at_level('WARNING', logger=publisher_module.logger.name):
publisher = AsyncPubSubPublisher(project_id='proj', topic_id='topic')
assert publisher._enabled is False
assert not client_cls.called
assert not hasattr(publisher, '_client')
assert 'noop mode' in caplog.text
assert 'project=proj' in caplog.text
assert 'topic=topic' in caplog.text


@patch('osprey.async_worker.lib.publisher.metrics')
def test_publish_bytes_short_circuits_and_emits_noop_metric_when_disabled(mock_metrics) -> None:
with patch('osprey.async_worker.lib.publisher.gcp_credentials_available', return_value=False):
publisher = AsyncPubSubPublisher(project_id='proj', topic_id='topic')
publisher.publish_bytes(b'data')

mock_metrics.increment.assert_called_once_with(
'async_pubsub_publisher.publish.noop',
tags=['project:proj', 'topic:topic'],
)


async def test_stop_short_circuits_when_disabled() -> None:
with patch('osprey.async_worker.lib.publisher.gcp_credentials_available', return_value=False):
publisher = AsyncPubSubPublisher(project_id='proj', topic_id='topic')
await publisher.stop()

assert not hasattr(publisher, '_client')
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ def monkeypatch_session(request):


@pytest.fixture(scope='function', autouse=True)
def pubsub_client_mock(monkeypatch_session) -> MagicMock:
def pubsub_client_mock(monkeypatch_session, monkeypatch) -> MagicMock:
# tagging as potential opensource item
from google.cloud import pubsub_v1
from osprey.worker.lib import publisher

pubsub_client_mock = MagicMock()
publisher_class_mock = MagicMock(return_value=pubsub_client_mock)

monkeypatch_session.setattr(pubsub_v1, 'PublisherClient', publisher_class_mock)
# PubSubPublisher degrades to noop without GCP credentials, which CI lacks;
# force detection so the mocked client is actually exercised. Function-scoped
# monkeypatch so it reverts after each test instead of leaking into later
# modules (e.g. test_publisher.py, whose disabled-publisher tests would then
# build a real client and hang on stop()).
monkeypatch.setattr(publisher, '_check_gcp_credentials', lambda: True)
return pubsub_client_mock
36 changes: 36 additions & 0 deletions osprey_worker/src/osprey/worker/lib/gcp_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Shared detection of whether GCP Pub/Sub publishing should be active.

Used by the Pub/Sub publishers to decide whether to run for real or degrade to
a noop (e.g. local dev or adopter environments without GCP). Publishing is off
when DISABLE_GCP_PUBSUB is set, or when GCP credentials cannot be resolved. The
credential result is cached at module level so google.auth.default() is probed
at most once per process, and guarded by a lock so concurrent constructors
probe only once.
"""

import os
import threading

import google.auth
from google.auth.exceptions import DefaultCredentialsError

_gcp_credentials_available: bool | None = None
_gcp_credentials_lock = threading.Lock()


def gcp_pubsub_disabled() -> bool:
return os.environ.get('DISABLE_GCP_PUBSUB', '').lower() == 'true'


def gcp_credentials_available() -> bool:
global _gcp_credentials_available
if _gcp_credentials_available is None:
with _gcp_credentials_lock:
# Re-check under the lock so concurrent callers probe google.auth.default() only once.
if _gcp_credentials_available is None:
try:
google.auth.default()
_gcp_credentials_available = True
except DefaultCredentialsError:
_gcp_credentials_available = False
return _gcp_credentials_available
37 changes: 35 additions & 2 deletions osprey_worker/src/osprey/worker/lib/publisher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import abc
import logging
from typing import TypeVar

from google.cloud import pubsub_v1
from osprey.worker.lib.gcp_credentials import gcp_credentials_available, gcp_pubsub_disabled
from osprey.worker.lib.instruments import metrics
from osprey.worker.lib.pubsub.publisher_client import BatchPubsubPublisherClient
from pydantic import BaseModel

_PydanticModelT = TypeVar('_PydanticModelT', bound=BaseModel)

logger = logging.getLogger(__name__)


class BasePublisher(abc.ABC):
"""
Expand Down Expand Up @@ -40,6 +44,15 @@ def stop(self) -> None:


class PubSubPublisher(BasePublisher):
"""Publishes Pydantic models to a Google Cloud Pub/Sub topic.

Degrades to noop mode when the DISABLE_GCP_PUBSUB env var is set, or when GCP
credentials cannot be resolved at construction time (e.g. local dev or adopter
environments without GCP). In noop mode no underlying client is built and
publish() and stop() return immediately. A one-time warning is logged at
construction so the inert state is visible.
"""

def __init__(
self,
project_id: str,
Expand All @@ -54,15 +67,31 @@ def __init__(
topic_id=topic_id,
)
self._project = project_id
self._raise_on_error = raise_on_error
self._tags = [f'project:{self._project}', f'topic:{self._topic_name}']
if gcp_pubsub_disabled():
self._enabled = False
logger.warning(
'DISABLE_GCP_PUBSUB is set, PubSubPublisher disabled (project=%s, topic=%s)',
project_id,
topic_id,
)
return
self._enabled = gcp_credentials_available()
if not self._enabled:
logger.warning(
'GCP credentials not detected, PubSubPublisher running in noop mode (project=%s, topic=%s)',
project_id,
topic_id,
)
return
batch_settings = pubsub_v1.types.BatchSettings(
max_bytes=max_bytes, # default 1MB
max_messages=max_messages, # default 100 messages
max_latency=max_latency,
)
# self._publisher = pubsub_v1.PublisherClient(batch_settings=batch_settings)
self._publisher = BatchPubsubPublisherClient(batch_settings=batch_settings)
self._raise_on_error = raise_on_error
self._tags = [f'project:{self._project}', f'topic:{self._topic_name}']

def prepare_data(self, data: _PydanticModelT) -> bytes:
"""
Expand All @@ -72,6 +101,8 @@ def prepare_data(self, data: _PydanticModelT) -> bytes:
return data.json(exclude_none=True).encode()

def publish(self, data: _PydanticModelT, attributes: dict[str, str] | None = None) -> None:
if not self._enabled:
return
if attributes is None:
attributes = {}

Expand All @@ -89,6 +120,8 @@ def publish(self, data: _PydanticModelT, attributes: dict[str, str] | None = Non
metrics.increment(f'{self.__class__.__name__}.publisher.success', tags=self._tags)

def stop(self) -> None:
if not self._enabled:
return
self._publisher.stop()


Expand Down
32 changes: 32 additions & 0 deletions osprey_worker/src/osprey/worker/lib/tests/test_gcp_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Iterator
from unittest.mock import MagicMock, patch

import pytest
from google.auth.exceptions import DefaultCredentialsError
from osprey.worker.lib import gcp_credentials
from osprey.worker.lib.gcp_credentials import gcp_credentials_available


@pytest.fixture(autouse=True)
def reset_cred_cache() -> Iterator[None]:
gcp_credentials._gcp_credentials_available = None
yield
gcp_credentials._gcp_credentials_available = None


def test_true_when_default_resolves() -> None:
with patch.object(gcp_credentials.google.auth, 'default', return_value=(MagicMock(), 'proj')):
assert gcp_credentials_available() is True


def test_false_when_default_raises() -> None:
with patch.object(gcp_credentials.google.auth, 'default', side_effect=DefaultCredentialsError()):
assert gcp_credentials_available() is False


def test_result_is_cached() -> None:
with patch.object(gcp_credentials.google.auth, 'default', return_value=(MagicMock(), 'proj')) as default_mock:
gcp_credentials_available()
gcp_credentials_available()
gcp_credentials_available()
assert default_mock.call_count == 1
63 changes: 63 additions & 0 deletions osprey_worker/src/osprey/worker/lib/tests/test_publisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from unittest.mock import MagicMock, patch

import pytest
from osprey.worker.lib import publisher
from osprey.worker.lib.publisher import PubSubPublisher


def test_pubsub_publisher_constructs_client_when_creds_present() -> None:
with (
patch.object(publisher, 'gcp_credentials_available', return_value=True),
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, 'gcp_credentials_available', return_value=False),
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_pubsub_publisher_disabled_via_env(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
monkeypatch.setenv('DISABLE_GCP_PUBSUB', 'true')
with (
patch.object(publisher, 'gcp_credentials_available') as cred_check,
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
# The opt-out short-circuits before probing credentials.
assert not cred_check.called
assert 'DISABLE_GCP_PUBSUB' in caplog.text


def test_publish_short_circuits_silently_when_disabled() -> None:
with (
patch.object(publisher, 'gcp_credentials_available', return_value=False),
patch.object(publisher, 'metrics') as metrics_mock,
):
pub = PubSubPublisher('proj', 'topic')
pub.publish(MagicMock())
metrics_mock.increment.assert_not_called()


def test_stop_short_circuits_when_disabled() -> None:
with patch.object(publisher, 'gcp_credentials_available', return_value=False):
pub = PubSubPublisher('proj', 'topic')
pub.stop()
assert not hasattr(pub, '_publisher')