Skip to content

Add Google Cloud Run worker identity/deployment helper - #1776

Open
seanbollin wants to merge 7 commits into
mainfrom
cloud-run-worker-id
Open

Add Google Cloud Run worker identity/deployment helper#1776
seanbollin wants to merge 7 commits into
mainfrom
cloud-run-worker-id

Conversation

@seanbollin

Copy link
Copy Markdown

What & why

The Python SDK already ships an AWS Lambda worker module (temporalio/contrib/aws/lambda_worker) that sets the worker "ID" for Lambda. This adds the equivalent capability for Google Cloud Run, covering both Cloud Run worker pools and services.

Cloud Run runs a long-lived container, so — unlike Lambda's per-invocation handler — there is nothing to wrap. This PR adds a small metadata helper module at temporalio/contrib/gcp/cloud_run. It's marked experimental.

What it does

get_google_cloud_run_metadata(), called once at worker startup:

  • resolves the deployment name from CLOUD_RUN_WORKER_POOL (worker pools) → K_SERVICE (services), and the revision from CLOUD_RUN_REVISIONK_REVISION, and
  • makes a single HTTP GET to the instance metadata server (http://metadata.google.internal/computeMetadata/v1/instance/id, header Metadata-Flavor: Google) for the unique instance id (available on both worker pools and services).

It returns a GoogleCloudRunMetadata exposing ready-to-pass values:

  • .worker_identity<instance_id>@<revision>, for Client.connect(..., identity=...); and
  • .worker_deployment_config → a WorkerDeploymentConfig(version=..., use_worker_versioning=True), for Worker(..., deployment_config=...).

(.worker_deployment_version is also exposed.)

Notes

  • Draft — opening early for design feedback. Naming/idiom (GoogleCloudRun-style, experimental marking) intentionally mirrors the coordinated Google Cloud Run effort already visible in the .NET SDK (Temporalio.Extensions.Gcp.CloudRun.OpenTelemetry).
  • No new runtime dependencies — uses only the standard library (urllib.request) for the HTTP call. worker_deployment_config uses a deferred import so identity-only use doesn't pull in the worker package.
  • No tests / OpenTelemetry companion yet (kept intentionally minimal).
  • Verified locally: ruff check, ruff format --check, pydocstyle, and mypy all clean. (A full import temporalio... needs the compiled Rust bridge, which isn't built in my sandbox; the module's logic was verified directly instead.)

🤖 Generated with Claude Code

seanbollin and others added 3 commits August 21, 2026 12:22
Adds an experimental Google Cloud Run helper, mirroring the existing AWS
Lambda module's worker-ID behavior. Because Cloud Run runs a long-lived
container (unlike Lambda's per-invocation model), this is a metadata
helper rather than a worker wrapper: it reads the Cloud Run instance
metadata -- the instance id from the metadata server, plus the worker
pool/service name and revision from CLOUD_RUN_WORKER_POOL / CLOUD_RUN_REVISION
(worker pools) or K_SERVICE / K_REVISION (services) -- and derives a worker
identity and a WorkerDeploymentVersion to apply to a normal long-lived
worker. Covers both Cloud Run worker pools and services.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The worker-side apply helper enabled versioning and set the deployment
version but left the default versioning behavior unset, so a versioned
worker with a plain (un-annotated) workflow failed to register. Default it
to PINNED; a per-workflow versioning behavior still takes precedence.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Cover the temporalio.contrib.gcp.cloud_run helper:
- Environment precedence for the deployment name (CLOUD_RUN_WORKER_POOL over
  K_SERVICE) and revision (CLOUD_RUN_REVISION over K_REVISION).
- Worker identity formatting and its revision -> name -> instance-id fallbacks.
- WorkerDeploymentVersion derivation and its ValueError when name/revision empty.
- WorkerDeploymentConfig enabling worker versioning with PINNED default behavior.
- The metadata HTTP fetch via a local in-process server: asserts the
  Metadata-Flavor: Google header is sent, the body is trimmed, and a clear
  RuntimeError is raised on non-200 and unreachable responses.

Uses the helper's dependency-injection seams (getenv and metadata_url) so no
real environment or network access is required.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@seanbollin
seanbollin marked this pull request as ready for review August 26, 2026 19:37
@seanbollin
seanbollin requested a review from a team as a code owner August 26, 2026 19:37
seanbollin and others added 3 commits August 31, 2026 13:13
Re-architect the Cloud Run worker-ID helper into a plugin mirroring the
SDK's OpenTelemetry Cloud Run plugin. CloudRunPlugin subclasses
temporalio.plugin.SimplePlugin and is registered once on the client via
Client.connect(plugins=[...]); it propagates to workers automatically.

The plugin fetches Cloud Run instance metadata lazily at client connect
and caches it, then sets the client identity (only when the caller did
not provide one) and configures the worker with a PINNED
WorkerDeploymentConfig derived from the Cloud Run revision. Connecting
off Cloud Run fails fast with a clear error.

The GoogleCloudRunMetadata dataclass and its worker_identity /
worker_deployment_version / worker_deployment_config properties are kept
for advanced and non-plugin use.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Cloud Run can host multiple plugins (a worker-ID plugin and an
OpenTelemetry plugin both live in the same cloud_run area), so the
worker-ID plugin needs a specific name rather than the generic
CloudRunPlugin.

- Rename class CloudRunPlugin -> WorkerIDPlugin and move
  _plugin.py -> _worker_id_plugin.py (cloud_run package and
  GoogleCloudRunMetadata unchanged).
- Update the package __init__ export/__all__, quick-start, and README.
- Rename test_plugin.py -> test_worker_id_plugin.py and fix the
  basedpyright reportInvalidCast errors by constructing WorkerConfig()
  instead of cast(WorkerConfig, {}); silence reportUnusedParameter on
  the unused connect() callbacks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Relocate the Google Cloud Run worker-ID plugin from directly inside
temporalio/contrib/gcp/cloud_run/ into a new worker_id/ sub-package so it
owns its own __init__.py and README.md. This avoids a hard collision with
the separate OTel Cloud Run plugin, which also owns cloud_run/README.md and
cloud_run/__init__.py; after the move the two plugins share only the
minimal cloud_run/ and gcp/ namespace-marker __init__.py files.

The public names are unchanged; only the import path gains .worker_id:

    from temporalio.contrib.gcp.cloud_run.worker_id import WorkerIDPlugin

cloud_run/__init__.py is reduced to a minimal namespace-marker docstring
with no worker-ID exports. Mirrors the Go (contrib/gcp/cloudrun/workerid)
and .NET (CloudRun.WorkerId) layouts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant