Google Cloud Monitoring Exporter for OpenTelemetry

1.11.0a0 · active · verified Thu Apr 09

The `opentelemetry-exporter-gcp-monitoring` library provides an OpenTelemetry Python exporter for sending metrics to Google Cloud Monitoring. It is part of the `opentelemetry-operations-python` project, ensuring compatibility with other Google Cloud OpenTelemetry components. The current version is 1.11.0a0 and the project maintains a regular release cadence with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `CloudMonitoringMetricsExporter` to send custom metrics to Google Cloud Monitoring. It sets up a `MeterProvider` with a `PeriodicExportingMetricReader` to automatically export metrics at a defined interval. Ensure your Google Cloud authentication (e.g., `GOOGLE_APPLICATION_CREDENTIALS` environment variable or default application credentials) is properly configured for the exporter to work. Resource attributes are crucial as they map to GCP monitored resource labels.

import os
import time
from opentelemetry import metrics
from opentelemetry.exporter.gcp_monitoring import CloudMonitoringMetricsExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader

# Ensure your Google Cloud credentials are set up (e.g., via GOOGLE_APPLICATION_CREDENTIALS)
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/your/key.json'

# Configure Resource attributes (these map to GCP MonitoredResource labels)
resource = Resource.create(
    {
        "service.name": os.environ.get('SERVICE_NAME', 'my-gcp-metrics-service'),
        "service.namespace": os.environ.get('SERVICE_NAMESPACE', 'default'),
        "service.instance.id": os.environ.get('SERVICE_INSTANCE_ID', 'instance-1'),
    }
)

# Configure the exporter and metric reader
exporter = CloudMonitoringMetricsExporter()
reader = PeriodicExportingMetricReader(
    exporter,
    export_interval_millis=5000, # Export every 5 seconds
    export_timeout_millis=30000 # Timeout after 30 seconds
)

meter_provider = MeterProvider(metric_readers=[reader], resource=resource)
metrics.set_meter_provider(meter_provider)

# Create a meter from the global meter provider
meter = metrics.get_meter(__name__)

# Create a counter instrument
counter = meter.create_counter(
    "my_app_requests_total",
    description="Total number of application requests",
    unit="1",
)

# Record some measurements
print("Recording metrics...")
for i in range(5):
    counter.add(1, {"http_method": "GET", "http_status": "200"})
    counter.add(1, {"http_method": "POST", "http_status": "201"})
    print(f"  Added metrics batch {i+1}")
    time.sleep(2) # Sleep to allow multiple export intervals

print("Metrics sent to Google Cloud Monitoring. Check your dashboard.")

# Flush and shutdown the provider to ensure all metrics are exported
meter_provider.shutdown()
print("Metric provider shut down.")

view raw JSON →