Google Cloud Logging exporter for OpenTelemetry
The `opentelemetry-exporter-gcp-logging` library provides an OpenTelemetry Log exporter that sends log data to Google Cloud Logging. It's part of the OpenTelemetry Python Contrib project for Google Cloud operations. The current version is 1.11.0a0, and releases generally follow the OpenTelemetry Python SDK's release cadence, with specific updates for Google Cloud integration.
Warnings
- breaking As of v1.11.0a0, the `CloudLoggingExporter` defaults to outputting structured JSON to stdout via `structured_json_file=True` if `structured_json_file` is not explicitly set to `False` or if `exporter.export` is called directly without it. This changes the underlying mechanism of log export from direct RPC calls and assumes presence of a Cloud Logging agent to scrape stdout. If you relied on direct RPC behavior or are not in a compatible GCP environment, explicitly set `structured_json_file=False` or revert to an older version.
- breaking There are known compatibility constraints with newer OpenTelemetry SDK versions. `opentelemetry-exporter-gcp-logging` v1.11.0a0 has an upper bound set on `opentelemetry-sdk` due to potential breaking changes in the SDK's logging API. Exceeding the specified SDK version range can lead to runtime errors.
- gotcha Ensure the Google Cloud service account used by your application or host environment has the `roles/logging.logWriter` IAM role. Missing this permission is a common cause of logs failing to appear in Cloud Logging.
- gotcha While manual resource attribute creation is shown for `Resource.create()`, consider using `opentelemetry-resourcedetector-gcp` for automatic detection of environment-specific resource attributes when running in Google Cloud environments (e.g., GCE, GKE, Cloud Run). This enriches your telemetry data automatically with valuable metadata.
Install
-
pip install opentelemetry-exporter-gcp-logging
Imports
- CloudLoggingExporter
from opentelemetry.exporter.cloud_logging import CloudLoggingExporter
Quickstart
import logging
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, SimpleLogRecordProcessor
from opentelemetry.sdk.resources import Resource
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.cloud_logging import CloudLoggingExporter
# Configure OpenTelemetry LoggerProvider
resource = Resource.create(
{
"service.name": "my-python-app",
"service.instance.id": "instance-1",
}
)
logger_provider = LoggerProvider(resource=resource)
set_logger_provider(logger_provider)
# --- Example 1: Exporting via RPC (default for older versions, or if structured_json_file is not set) ---
exporter_rpc = CloudLoggingExporter(default_log_name='my_app_logs_rpc')
logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter_rpc))
# --- Example 2: Exporting as structured JSON to stdout (recommended for GCP environments >= v1.11.0) ---
# This is more efficient for environments with Cloud Logging agent that scrapes stdout
exporter_json = CloudLoggingExporter(default_log_name='my_app_logs_json', structured_json_file=True)
logger_provider.add_log_record_processor(SimpleLogRecordProcessor(exporter_json)) # Simple processor is often sufficient for stdout export
# Attach OTLP handler to the root logger
handler = LoggingHandler(level=logging.INFO, logger_provider=logger_provider)
logging.getLogger().addHandler(handler)
# Create a namespaced logger for your application
# It's recommended not to use the root logger with OTLP handler directly for app logs
app_logger = logging.getLogger("my_app_module")
app_logger.info("This is an info log with OTel context!")
app_logger.warning("A warning occurred: %s", "something went wrong")
# Logs will be exported asynchronously by BatchLogRecordProcessor
# For SimpleLogRecordProcessor, logs are sent immediately
# It's good practice to shut down the logger provider when the application exits
# This ensures all buffered logs are flushed.
# In a real application, you'd typically do this in a graceful shutdown hook.
logger_provider.shutdown()
print("Logs sent to Cloud Logging (or stdout for structured JSON exporter).")