Skip to main content

Programmatic query builder for Google Cloud Logging filter language

Project description

GCP Observability

Python toolkit for Google Cloud observability — starting with a programmatic Cloud Logging query builder.

Cloud Logging Query Builder

Build Cloud Logging filter strings in Python instead of writing them by hand. The output is identical to what you'd type in the "Build query" panel of the Cloud Logging console.

Installation

pip install -e .

Example — find errors containing "ValueError: Bad"

from datetime import datetime
from gcp_observability.logging import QueryBuilder, Severity, F

query = (
    QueryBuilder()
    .severity_gte(Severity.ERROR)
    .time_range(
        start=datetime(2026, 7, 9, 10, 0, 0),   # 2026-07-09T10:00:00Z
        end=datetime(2026, 7, 9, 10, 30, 0),    # 2026-07-09T10:30:00Z
    )
    .where(
        F("textPayload").has("ValueError: Bad")
        | F("jsonPayload.message").has("ValueError: Bad")
    )
    .build()
)

print(query)

Output — paste this directly into the Cloud Logging console or pass to gcloud:

severity>=ERROR
timestamp>="2026-07-09T10:00:00Z"
timestamp<"2026-07-09T10:30:00Z"
(textPayload:"ValueError: Bad") OR (jsonPayload.message:"ValueError: Bad")

Use with the Cloud Logging client

import google.cloud.logging

client = google.cloud.logging.Client(project="my-gcp-project")
entries = client.list_entries(filter_=query)

for entry in entries:
    print(entry.timestamp, entry.payload)

Use with gcloud CLI

gcloud logging read 'severity>=ERROR
timestamp>="2026-07-09T10:00:00Z"
timestamp<"2026-07-09T10:30:00Z"
(textPayload:"ValueError: Bad") OR (jsonPayload.message:"ValueError: Bad")'

API reference

QueryBuilder methods

Method Description
.resource_type(type) Filter by resource type (e.g. cloud_run_revision)
.resource_label(key, value) Filter by a resource label
.project(project_id) Restrict to a GCP project
.log_name(name) Exact logName match
.severity(op, level) Severity with explicit operator (=, >=, etc.)
.severity_eq(level) severity=LEVEL
.severity_gte(level) severity>=LEVEL
.severity_range(low, high) severity>=LOW and severity<=HIGH
.time_range(start, end) Timestamp window (str or datetime, end is exclusive)
.since(hours, minutes, days) Relative window from now
.text_payload(value) Substring match on textPayload
.json_payload(field, op, value) Filter on a jsonPayload sub-field
.json_payload_has(field, value) Substring match on a jsonPayload sub-field
.proto_payload(field, op, value) Filter on a protoPayload sub-field
.http_method(method) HTTP request method
.http_status(op, status) HTTP response status code
.http_url(value) Substring match on request URL
.http_latency_gte(seconds) Requests slower than N seconds
.label(key, value) Log entry label (special chars in key auto-quoted)
.trace(trace_id) Filter by trace ID
.span_id(span_id) Filter by span ID
.where(expr) Add a raw F()-built expression
.raw(filter_str) Add a verbatim filter string
.build() Return the complete filter string

F() — low-level expression builder

from gcp_observability.logging import F

# Comparisons
F("severity") >= "ERROR"
F("resource.type") == "cloud_run_revision"
F("httpRequest.status") >= 500

# Substring / has-field
F("textPayload").has("panic")
F("jsonPayload.message").has("timeout")

# Chained dot access
F("resource").labels.zone == "us-central1-a"

# Labels with special characters
F("labels")["k8s-pod/app"] == "my-service"

# Boolean operators
expr_a & expr_b   # AND
expr_a | expr_b   # OR
~expr_a           # NOT

Constants

from gcp_observability.logging import Severity, ResourceType

Severity.DEBUG, Severity.INFO, Severity.NOTICE
Severity.WARNING, Severity.ERROR, Severity.CRITICAL

ResourceType.CLOUD_RUN_REVISION
ResourceType.GKE_CONTAINER
ResourceType.GCE_INSTANCE
ResourceType.LOAD_BALANCER
# ... see constants.py for full list

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_observability-0.1.0.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

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

gcp_observability-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file gcp_observability-0.1.0.tar.gz.

File metadata

  • Download URL: gcp_observability-0.1.0.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gcp_observability-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4885ffbc297bb5356e878c8396327e12f9bdeca0b21b93ccfbb73e62143869ab
MD5 341e02c547290881af5fed26df52aef5
BLAKE2b-256 f98f7187a485db7b2bd01afe73ca6be53a988f58caac582118de7afa108351c7

See more details on using hashes here.

File details

Details for the file gcp_observability-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gcp_observability-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gcp_observability-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18cd84b4b1161186347ced1820700f78aa0abdad0d9bf025529a52a6232ce21f
MD5 297e118a9fe450e68eb9030157bca4c8
BLAKE2b-256 207bf0bf9e5ca2fbca5df573d425344e3bf8de6c40b3da0fc1913d4bcdd600eb

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