Google Cloud Logging exporter for OpenTelemetry

1.11.0a0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `CloudLoggingExporter` to send Python logs to Google Cloud Logging. It includes examples for both the traditional RPC-based export (using `BatchLogRecordProcessor`) and the recommended structured JSON export to stdout (using `SimpleLogRecordProcessor`), which is more efficient when running in GCP environments with a Cloud Logging agent.

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).")

view raw JSON →