Skip to main content

A library for monitoring modeled metrics with Google Cloud Monitoring

Project description

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_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

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.

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

unacast_modeled_metrics_monitoring-0.1.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file unacast_modeled_metrics_monitoring-0.1.2.tar.gz.

File metadata

File hashes

Hashes for unacast_modeled_metrics_monitoring-0.1.2.tar.gz
Algorithm Hash digest
SHA256 61db9772f9c651e7e4d9f3aabaccaa41c74657dd2bb1fa3fbf0a821f581dddac
MD5 8ba32d7aed6a9a8aae721745b43858e3
BLAKE2b-256 e3e71aaef8094424082f21454cb518b15608c76c2fe1aca9a51eab02019dd68b

See more details on using hashes here.

File details

Details for the file unacast_modeled_metrics_monitoring-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for unacast_modeled_metrics_monitoring-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9e76b161eea12717c7f64d5fcde0fbeb2f7d04033368c5514714080d3e2b680
MD5 0a04d2abf28c8082fe8c5841b83ca4fd
BLAKE2b-256 cc7b1907cc30ba622820a34817480ae8f4ad424f19f3d7bbdd41b9653d6a4031

See more details on using hashes here.

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