Google Cloud Log Forwarding Setup

Overview

Forwarding logs from your Google Cloud environment enables near real-time monitoring of the resources and activities taking place in your organization or folder. You can set up log monitors to be notified of issues, use Cloud SIEM to detect threats, or leverage Watchdog to identify unknown issues or anomalous behavior.

Logs are forwarded by Google Cloud Dataflow using the Datadog Dataflow template. This approach offers batching and compression of your log events before forwarding them to Datadog, which is the most network-efficient way to forward your logs. You can specify which logs are forwarded with inclusion and exclusion filters.

Setup

Choose the Quick Start setup method if…

  • You are setting up log forwarding from Google Cloud for the first time.
  • You prefer a UI-based workflow and want to minimize the time it takes to create and configure the necessary resources.
  • You want to automate setup steps in scripts or CI/CD pipelines.
Prerequisite permissions

You must have the following permissions to complete the setup:

In Google Cloud:
In Datadog:

Your Datadog user account must have either the Datadog Admin role, or, if using custom roles, the following permissions:

  • api_keys_read
  • api_keys_write
Instructions
  1. In the Google Cloud integration tile, click the Configure Log Collection button.
  2. Select Quick Start. A setup script, configured with your Datadog credentials and site, is automatically generated.
  3. Copy the setup script. You can run the script locally or in Google Cloud Shell:
    • Locally: May be faster, but requires your Google Cloud credentials and the gcloud CLI installed on your machine.
    • Google Cloud Shell: Click Open Google Cloud Shell to run the script.
  4. After running the script, return to the Google Cloud integration tile.
  5. In the Select Projects section, select the folders and projects to forward logs from. If you select a folder, logs are forwarded from all of its child projects.
    Note: Only folders and projects that you have the necessary access and permissions for appear in this section. Likewise, folders and projects without a display name do not appear.
  6. In the Dataflow Job Configuration section, specify configuration options for the Dataflow job:
    • Select deployment settings (Google Cloud region and project to host the created resources—Pub/Sub topics and subscriptions, a log routing sink, a Secret Manager entry, a service account, a Cloud Storage bucket, and a Dataflow job)
    • Select scaling settings (number of workers and maximum workers)
    • Select performance settings (maximum number of parallel requests and batch size)
    • Select execution options
  7. In the Advanced Configuration section, optionally specify the machine type for your Dataflow worker VMs. If no machine type is selected, Dataflow automatically chooses an appropriate machine type based on your job requirements.
  8. Optionally, choose to specify inclusion and exclusion filters using Google Cloud’s logging query language.
  9. Review the steps to be executed in the Complete Setup section. If everything is satisfactory, click Complete Setup.

Choose the Terraform setup method if…

  • You manage infrastructure as code and want to keep the Datadog Google Cloud integration under version control.
  • You need to configure multiple folders or projects consistently with reusable provider blocks.
  • You want a repeatable, auditable deployment process that fits into your Terraform-managed environment.
Prerequisite permissions

You must have the following permissions to complete the setup:

In Google Cloud:
In Datadog:

Your Datadog user account must have either the Datadog Admin role, or, if using custom roles, the following permissions:

  • api_keys_read
  • api_keys_write
Instructions
  1. In the Google Cloud integration tile, click the Configure Log Collection button.
  2. Select Terraform.
  3. In the Select Projects section, select the folders and projects to forward logs from. If you select a folder, logs are forwarded from all of its child projects.
    Note: Only folders and projects that you have the necessary access and permissions for appear in this section. Likewise, folders and projects without a display name do not appear.
  4. In the Dataflow Job Configuration section, specify configuration options for the Dataflow job:
    • Select deployment settings (Google Cloud region and project to host the created resources—Pub/Sub topics and subscriptions, a log routing sink, a Secret Manager entry, a service account, a Cloud Storage bucket, and a Dataflow job)
    • Select scaling settings (maximum workers)
    • Select performance settings (maximum number of parallel requests and batch size)
    • Select execution options (Streaming Engine is enabled by default; read more about its benefits)
  5. In the Advanced Configuration section, optionally specify the machine type for your Dataflow worker VMs. If no machine type is selected, Dataflow automatically chooses an appropriate machine type based on your job requirements.
  6. Optionally, choose to specify inclusion and exclusion filters using Google Cloud’s logging query language.

See the instructions on the terraform-gcp-datadog-integration repo to set up and manage the necessary infrastructure through Terraform.

The instructions in this section guide you through the process of:

  1. Creating a Pub/Sub topic and pull subscription to receive logs from a configured log sink
  2. Creating a custom Dataflow worker service account to provide least privilege to your Dataflow pipeline workers
  3. Creating a log sink to publish logs to the Pub/Sub topic
  4. Creating a Dataflow job using the Datadog template to stream logs from the Pub/Sub subscription to Datadog

You have full control over which logs are sent to Datadog through the logging filters you create in the log sink, including GCE and GKE logs. See Google’s Logging query language page for information about writing filters. For a detailed examination of the created architecture, see Stream logs from Google Cloud to Datadog in the Cloud Architecture Center.

Note: You must enable the Dataflow API to use Google Cloud Dataflow. See Enabling APIs in the Google Cloud documentation for more information.

To collect logs from applications running in GCE or GKE, you can also use the Datadog Agent.

1. Create a Cloud Pub/Sub topic and subscription

  1. Go to the Cloud Pub/Sub console and create a new topic. Select the option Add a default subscription to simplify the setup.

    Note: You can also manually configure a Cloud Pub/Sub subscription with the Pull delivery type. If you manually create your Pub/Sub subscription, leave the Enable dead lettering box unchecked. For more details, see Unsupported Pub/Sub features.

The Create a topic page in the Google Cloud Console with the Add a default subscription checkbox selected
  1. Give that topic an explicit name such as export-logs-to-datadog and click Create.

  2. Create an additional topic and default subscription to handle any log messages rejected by the Datadog API. The name of this topic is used within the Datadog Dataflow template as part of the path configuration for the outputDeadletterTopic template parameter. When you have inspected and corrected any issues in the failed messages, send them back to the original export-logs-to-datadog topic by running a Pub/Sub to Pub/Sub template job.

  3. Datadog recommends creating a secret in Secret Manager with your valid Datadog API key value, for later use in the Datadog Dataflow template.

Cloud Pub/Subs are subject to Google Cloud quotas and limitations. If the number of logs you have exceeds those limitations, Datadog recommends you split your logs over several topics. See the Monitor the Pub/Sub Log Forwarding section for information on setting up monitor notifications if you approach those limits.

2. Create a custom Dataflow worker service account

The default behavior for Dataflow pipeline workers is to use your project’s Compute Engine default service account, which grants permissions to all resources in the project. If you are forwarding logs from a Production environment, you should instead create a custom worker service account with only the necessary roles and permissions, and assign this service account to your Dataflow pipeline workers.

  1. Go to the Service Accounts page in the Google Cloud console and select your project.
  2. Click CREATE SERVICE ACCOUNT and give the service account a descriptive name. Click CREATE AND CONTINUE.
  3. Add the roles in the required permissions table and click DONE.
Required permissions
Dataflow Admin
roles/dataflow.admin
Allow this service account to perform Dataflow administrative tasks.
Dataflow Worker
roles/dataflow.worker
Allow this service account to perform Dataflow job operations.
Pub/Sub Viewer
roles/pubsub.viewer
Allow this service account to view messages from the Pub/Sub subscription with your Google Cloud logs.
Pub/Sub Subscriber
roles/pubsub.subscriber
Allow this service account to consume messages from the Pub/Sub subscription with your Google Cloud logs.
Pub/Sub Publisher
roles/pubsub.publisher
Allow this service account to publish failed messages to a separate subscription, which allows for analysis or resending the logs.
Secret Manager Secret Accessor
roles/secretmanager.secretAccessor
Allow this service account to access the Datadog API key in Secret Manager.
Storage Object Admin
roles/storage.objectAdmin
Allow this service account to read and write to the Cloud Storage bucket specified for staging files.

Note: If you don’t create a custom service account for the Dataflow pipeline workers, ensure that the default Compute Engine service account has the required permissions above.

3. Export logs from Google Cloud Pub/Sub topic

  1. Go to the Logs Explorer page in the Google Cloud console.

  2. From the Log Router tab, select Create Sink.

  3. Provide a name for the sink.

  4. Choose Cloud Pub/Sub as the destination and select the Cloud Pub/Sub topic that was created for that purpose. Note: The Cloud Pub/Sub topic can be located in a different project.

    Export Google Cloud Pub/Sub Logs to Pub Sub
  5. Choose the logs you want to include in the sink with an optional inclusion or exclusion filter. You can filter the logs with a search query, or use the sample function. For example, to include only 10% of the logs with a severity level of ERROR, create an inclusion filter with severity="ERROR" AND sample(insertId, 0.1).

    The inclusion filter for a Google Cloud logging sink with a query of severity=ERROR and sample(insertId, 0.1)
  6. Click Create Sink.

Note: It is possible to create several exports from Google Cloud Logging to the same Cloud Pub/Sub topic with different sinks.

4. Create and run the Dataflow job

  1. Go to the Create job from template page in the Google Cloud console.

  2. Give the job a name and select a Dataflow regional endpoint.

  3. Select Pub/Sub to Datadog in the Dataflow template dropdown, and the Required parameters section appears. a. Select the input subscription in the Pub/Sub input subscription dropdown. b. Enter the following in the Datadog Logs API URL field:

    https://

    Note: Ensure that the Datadog site selector on the right of the page is set to your Datadog site before copying the URL above.

    c. Select the topic created to receive message failures in the Output deadletter Pub/Sub topic dropdown. d. Specify a path for temporary files in your storage bucket in the Temporary location field.

Required parameters in the Datadog Dataflow template
  1. Under Optional Parameters, check Include full Pub/Sub message in the payload.

  2. If you created a secret in Secret Manager with your Datadog API key value as mentioned in step 1, enter the resource name of the secret in the Google Cloud Secret Manager ID field.

Optional parameters in the Datadog Dataflow template with Google Cloud Secret Manager ID and Source of the API key passed fields both highlighted

See Template parameters in the Dataflow template for details on using the other available options:

  • apiKeySource=KMS with apiKeyKMSEncryptionKey set to your Cloud KMS key ID and apiKey set to the encrypted API key
  • Not recommended: apiKeySource=PLAINTEXT with apiKey set to the plaintext API key
  1. If you created a custom worker service account, select it in the Service account email dropdown.
Optional parameters in the Datadog Dataflow template with the service account email dropdown highlighted
  1. Click RUN JOB.

Note: If you have a shared VPC, see the Specify a network and subnetwork page in the Dataflow documentation for guidelines on specifying the Network and Subnetwork parameters.

Collecting Google Cloud logs with a Pub/Sub Push subscription is in the process of being deprecated.

The above documentation for the Push subscription is only maintained for troubleshooting or modifying legacy setups.

Datadog recommends instead using a Pull subscription with the Datadog Dataflow template, as described in the Quick Start and Terraform setup sections.

See the Stream logs from Google Cloud to Datadog guide in the Google Cloud architecture center for a more detailed explanation of the steps and architecture involved in log forwarding. For a deep dive into the benefits of the Pub/Sub to Datadog template, read Stream your Google Cloud logs to Datadog with Dataflow in the Datadog blog.

Validation

New logging events delivered to the Cloud Pub/Sub topic appear in the Datadog Log Explorer.

Note: You can use the Google Cloud Pricing Calculator to calculate potential costs.

Monitor the Cloud Pub/Sub log forwarding

The Google Cloud Pub/Sub integration provides helpful metrics to monitor the status of the log forwarding:

  • gcp.pubsub.subscription.num_undelivered_messages for the number of messages pending delivery
  • gcp.pubsub.subscription.oldest_unacked_message_age for the age of the oldest unacknowledged message in a subscription

Use the metrics above with a metric monitor to receive alerts for the messages in your input and deadletter subscriptions.

Monitor the Dataflow pipeline

Use Datadog’s Google Cloud Dataflow integration to monitor all aspects of your Dataflow pipelines. You can see all your key Dataflow metrics on the out-of-the-box dashboard, enriched with contextual data such as information about the GCE instances running your Dataflow workloads, and your Pub/Sub throughput.

You can also use a preconfigured Recommended Monitor to set up notifications for increases in backlog time in your pipeline. For more information, read Monitor your Dataflow pipelines with Datadog in the Datadog blog.

Further reading

Additional helpful documentation, links, and articles: