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.
pip install opentelemetry-exporter-gcp-loggingVerified import paths — ran on the pinned version, not inferred.
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.
If direct RPC export is needed, instantiate `CloudLoggingExporter` with `structured_json_file=False`. For GCP environments, structured JSON to stdout is generally preferred, so ensure your deployment has a suitable Cloud Logging agent configured.
Always check the project's `pyproject.toml` or `setup.py` (e.g., `pyproject.toml` often specifies `opentelemetry-sdk < 1.40.0, >= 1.20.0`) for the exact compatible `opentelemetry-sdk` version range and ensure your environment adheres to it. Upgrade `opentelemetry-exporter-gcp-logging` if a newer SDK version is required.
Grant the `Logging Log Writer` role (`roles/logging.logWriter`) to the service account associated with your application's environment (e.g., Compute Engine default service account, GKE Workload Identity service account, or a service account key file).
Install `opentelemetry-resourcedetector-gcp` and integrate it into your `LoggerProvider` setup to automatically populate resource attributes relevant to your GCP environment.
Install the correct package using `pip install opentelemetry-exporter-gcp-logging` and ensure the import statement is `from opentelemetry.exporter.cloud_logging import CloudLoggingExporter`.
Provide Google Cloud credentials by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of a service account key JSON file, or by ensuring the code runs within a GCP environment with a properly configured service account and necessary scopes.
Grant the appropriate IAM role, such as 'Logs Writer' (`roles/logging.logWriter`), to the service account or user trying to export logs within your Google Cloud project.
Enable debug logging for the OpenTelemetry SDK to get more specific error messages. Systematically check network connectivity, confirm the correct Google Cloud project ID, verify authentication credentials, ensure proper IAM permissions for the service account, and check for any network policies or firewalls blocking traffic to Cloud Logging endpoints.