Skip to main content

Read Google Secret Manager values and send Email / Google Chat alerts from GCP projects and notebooks, with automatic project detection.

Project description

GCP Notifier

Upload Python Package

Read Google Secret Manager values and send Email / Google Chat alerts from GCP projects and notebooks (Jupyter, Colab, Vertex AI Workbench). The project is auto-detected from the ambient credentials, so get_secret("MY_SECRET") is usually all you need. It wraps the official google-cloud-secret-manager SDK; its value is the project auto-detection and the one-line call.

Synchronous Usage

Import and use in your Python code:

from gcp_notifier import notify, notify_on_failure

# Send a notification (choose channels: "email", "gchat", or both)
notify(
  subject="Alert Subject",
  body="Alert body text",
  channels=["email", "gchat"]  # or ["email"] or ["gchat"]
)

# Use as tenacity retry_error_callback
from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(3), retry_error_callback=notify_on_failure)
def always_fails():
  # This function will always fail, triggering notify_on_failure after retries
  raise ValueError("This is a test error for notification.")

always_fails()

Async Usage

You can send notifications asynchronously using the async API.

Async notify example

import asyncio  # only if running a script, otherwise, do not import
from gcp_notifier import async_notify

async def main():
  await async_notify(
    subject="Async Alert",
    body="This is an async alert!",
    channels=["email", "gchat"]
  )

asyncio.run(main()) # if running a script
await main()        # if using a notebook

Async tenacity error callback example

import asyncio  # only if running a script, otherwise, do not import
from gcp_notifier import async_notify_on_failure
from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(3), retry_error_callback=async_notify_on_failure)
async def always_fails_async():
  raise ValueError("This is a test error for async notification.")

asyncio.run(always_fails_async())   # if running a script
await always_fails_async()          # if using a notebook

Retrieving Secrets

The same Secret Manager access the library uses internally is exposed as a public helper. project_id defaults to the project detected from the ambient credentials (google.auth.default()), so in most cases you only pass the secret ID.

from gcp_notifier import get_secret

# Uses the auto-detected project
api_key = get_secret("MY_API_KEY")

# Or target a specific project / version explicitly
db_password = get_secret(
    "DB_PASSWORD",
    project_id="my-other-project",
    version_id="3",
)

Raises ValueError if secret_id is empty, or if no project_id is provided and none can be detected from the environment.

Secrets in Notebooks

get_secret is designed to work from notebooks. Importing the package does not fetch any notification secrets, so it stays quiet even when only secret access is needed. You only need credentials the project can be detected from.

In Google Colab, authenticate the interactive user first:

from google.colab import auth
auth.authenticate_user()

from gcp_notifier import get_secret

api_key = get_secret("MY_API_KEY")

On Vertex AI Workbench, or local Jupyter using Application Default Credentials, the credentials are already present (run gcloud auth application-default login once locally), so no auth call is needed:

from gcp_notifier import get_secret

api_key = get_secret("MY_API_KEY")

The account behind those credentials needs the Secret Manager Secret Accessor role on the project (see Quick Start below).

Installation

Install from PyPI (recommended):

pip install gcp-notifier

For async features (async_notify, async_notify_on_failure), install with the optional async dependencies:

pip install 'gcp-notifier[async]'

This will install the package plus the required async libraries (aiosmtplib, httpx).

Quick Start

  1. Install the package (see Installation above).

  2. The account (personal or service) running this code must have the 'Secret Manager Secret Accessor' role in your GCP project.

  3. The required secrets must be in the same GCP project where your Python script or notebook is running.

  4. Add your required secrets to Google Secret Manager in your GCP project:

    • GCHAT_WEBHOOK_URL (for Google Chat)
    • EMAIL_SENDER (sender email address for Email)
    • EMAIL_PASSWORD (password or app password for sender)
    • EMAIL_RECIPIENTS (comma-separated list of recipient email addresses)
  5. Import and use notify in your code as shown below.

Building and Publishing

This project uses a modern Python packaging workflow with pyproject.toml.

To build the package:

python -m pip install --upgrade build
python -m build

To check and upload to PyPI:

python -m pip install --upgrade twine
twine check dist/*
twine upload dist/*

See the Python Packaging User Guide for more details.

Changelog

See CHANGELOG.md for the release history.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcp_notifier-0.1.8.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gcp_notifier-0.1.8-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file gcp_notifier-0.1.8.tar.gz.

File metadata

  • Download URL: gcp_notifier-0.1.8.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gcp_notifier-0.1.8.tar.gz
Algorithm Hash digest
SHA256 426696d192965719e1f1e605a8d5b345d629aef368a7711ad1eebe69de500af1
MD5 6eb4d9b51342f33370f98aa0e3aba0ff
BLAKE2b-256 299cea5b50479b5b15ea0efd39089c73e0098e8081831146e129455249f89255

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcp_notifier-0.1.8.tar.gz:

Publisher: python-publish.yml on marcellusmontilla/gcp_notifier

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gcp_notifier-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: gcp_notifier-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gcp_notifier-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 032ed20261d52b9457a531ffa43e708184a2cef58bd9f2aa444c8dd38dca06c6
MD5 2824a3a7fee4f01386dde75f0b07ec7e
BLAKE2b-256 8d1b96693e6b90ca6a84e105dee34238f9cec2618ac35510516ef5c99edde547

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcp_notifier-0.1.8-py3-none-any.whl:

Publisher: python-publish.yml on marcellusmontilla/gcp_notifier

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page