Skip to main content

Modeled Metrics Monitoring Library

A Python library for monitoring modeled metrics with Google Cloud Monitoring.

Overview

This library provides a Python interface for working with Google Cloud Monitoring metric descriptors and writing metrics. It queries the Google Cloud Monitoring API to retrieve metric descriptors.

High level overview

Key Features

  • Direct API Integration: Queries Google Cloud Monitoring API for metric descriptors
  • Type Safety: Uses Google's protobuf MetricDescriptor objects
  • Flexible Metric Writing: Supports all metric value types (BOOL, INT64, DOUBLE, STRING, DISTRIBUTION)
  • Error Handling: Comprehensive exception handling for Google Cloud API errors

Usage

Development

# Install in development mode
pip install -e .

# Run the example
python -m modeled_metrics_monitoring.run

Building and Distribution

# Build the package
./build.sh

# Install the built package
pip install dist/*.whl

Using the Library

from modeled_metrics_monitoring import get_metric_descriptor_by_type, write_metric

# Get a metric descriptor by type
descriptor = get_metric_descriptor_by_type(
    "custom.googleapis.com/contextual-data-monitoring/modeled-metrics-ml-ops/vertex_pipeline/foot_traffic/feature_null_ratio"
)

# Write a metric
write_metric(
    descriptor,
    0.1,
    metric_labels={
        "feature_group_id": "temporal",
        "feature_group_revision": "r0_1",
        "feature_id": "is_weekend"
    }
)

# Or write a metric using the type string directly
write_metric(
    "custom.googleapis.com/contextual-data-monitoring/modeled-metrics-ml-ops/vertex_pipeline/foot_traffic/feature_null_ratio",
    0.1,
    metric_labels={
        "feature_group_id": "temporal",
        "feature_group_revision": "r0_1",
        "feature_id": "is_weekend"
    }
)

Configuration

The library can be configured using environment variables. All configuration values are defined in modeled-metrics-monitoring/src/modeled_metrics_monitoring/config.py.

Environment Variables

MONITORING_ENABLED

  • Description: Whether to enable monitoring functionality.
  • Type: Boolean (via environment variable)
  • Default: True
  • Accepted Values: 'true', '1', 'yes', 'on' (case-insensitive). Any other value disables monitoring.
  • Usage: Set to False to disable all monitoring operations without modifying code.
export MONITORING_ENABLED=False

MONITORING_CHECK_WRITE_ACCESS

  • Description: When enabled, monitoring is only considered active if the current principal can write metrics to Google Cloud Monitoring. When disabled, MONITORING_ENABLED alone controls whether monitoring runs; write access is not checked at import time.
  • Type: Boolean (via environment variable)
  • Default: False
  • Accepted Values: 'true', '1', 'yes', 'on' (case-insensitive). Any other value disables the write-access check.
  • Usage: Set to True in environments where you want monitoring to turn itself off if IAM does not allow metric writes (for example, local development or read-only credentials).
export MONITORING_CHECK_WRITE_ACCESS=True

MONITORING_INIT_FAIL_SHOULD_RAISE_EXCEPTION

  • Description: Whether to raise an exception when monitoring initialization fails (e.g., when the principal lacks required IAM permissions).
  • Type: Boolean (via environment variable)
  • Default: False
  • Accepted Values: 'true', '1', 'yes', 'on' (case-insensitive). Any other value disables exception raising.
  • Usage: Set to True to enable strict error handling. When False, initialization failures result in warnings and monitoring is disabled gracefully.
export MONITORING_INIT_FAIL_SHOULD_RAISE_EXCEPTION=True

MONITORING_PUSH_FAIL_SHOULD_RAISE_EXCEPTION

  • Description: Whether to re-raise exceptions when sending a metric to Google Cloud Monitoring fails (for example, API errors or network issues after write_metric builds the time series).
  • Type: Boolean (via environment variable)
  • Default: False
  • Accepted Values: 'true', '1', 'yes', 'on' (case-insensitive). Any other value keeps the default behavior (log and swallow).
  • Usage: Set to True so callers see push failures; when False, failures are logged at error level and the call returns without raising.
export MONITORING_PUSH_FAIL_SHOULD_RAISE_EXCEPTION=True

METRIC_WRITER_IAM_ROLE

  • Description: The IAM role name required for writing monitoring metrics.
  • Type: String
  • Default: "roles/monitoring.metricWriter"
  • Usage: Override if using a custom IAM role for metric writing permissions.
export METRIC_WRITER_IAM_ROLE=roles/monitoring.metricWriter

MONITORING_TARGET_GCP_PROJECT_ID

  • Description: The Google Cloud project ID where metric descriptors are stored and where the service account has monitoring permissions.
  • Type: String
  • Default: "uc-contextual-data-monitoring"
  • Usage: Set to the target GCP project ID where your metric descriptors are managed.
export MONITORING_TARGET_GCP_PROJECT_ID=your-project-id

Internal Configuration

METRIC_DESCRIPTOR_TYPE_PREFIX

  • Description: The prefix for all metric descriptor types managed by this project.
  • Type: String
  • Default: "custom.googleapis.com/contextual-data-monitoring/"
  • Warning: DO NOT CHANGE THIS VALUE. Changing this will cause all existing metric descriptors in Google Cloud Monitoring to become unsupported.
  • Note: This is an internal constant and should not be modified.

Architecture

  • Terraform: Uses YAML files from monitoring-metrics-definitions/metric-descriptors/*.yaml to create metric descriptors in Google Cloud Monitoring
  • Python Library: Queries Google Cloud Monitoring API directly to retrieve metric descriptors
  • Separation of Concerns: Terraform handles infrastructure (creating metric descriptors), Python library handles runtime operations (querying and writing metrics)

This approach ensures that the Python library is always working with the current state of metric descriptors in Google Cloud Monitoring, while Terraform manages the infrastructure definitions.

Release files for unacast-modeled-metrics-monitoring 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unacast-modeled-metrics-monitoring 0.1.7
File Size Uploaded
unacast_modeled_metrics_monitoring-0.1.7.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unacast-modeled-metrics-monitoring 0.1.7
File Interpreter ABI Platform
unacast_modeled_metrics_monitoring-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 21.5 kB

Release files / unacast_modeled_metrics_monitoring-0.1.7.tar.gz

Download URL unacast_modeled_metrics_monitoring-0.1.7.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
dc69821f0b7074dc2807183bb5bdb765824d8e514edcb5d1dbd3aa7d4f5501d8
BLAKE2b-256 checksum
How to use checksums
2bd091167948375b9d6aa13f48a874808bfdce3071917d5f54e533259a88c746
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Release files / unacast_modeled_metrics_monitoring-0.1.7-py3-none-any.whl

Download URL unacast_modeled_metrics_monitoring-0.1.7-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22e0597edbbfbc0d557248e0624a4bfe306f0f6108c3347de0f32dedbba27622
BLAKE2b-256 checksum
How to use checksums
93a7f0b9fe8356ac16ef0ac03608ad7497fddc31eddb0663a5d0ebc7b4d6b385
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page