Implementing observability practices and troubleshooting issues · Topic 1 of 6

Collecting Logs with Ops Agent and OpenTelemetry, and Cutting Log Cost

Collect logs with Ops Agent, OpenTelemetry, Cloud Audit Logs, VPC Flow Logs and Cloud Service Mesh, then cut cost with exclusions, sampling and filters.

  • 5 min
In a nutshell
  • Ops Agent for VMs; automatic collection for GKE, Cloud Run and App Engine; OpenTelemetry for anything, anywhere.
  • Admin Activity audit logs are always on; Data Access is mostly off by default.
  • VPC Flow Logs are per subnet with sampling and aggregation settings.

What it is

Cloud Logging stores logs from Google Cloud services, from your applications and from systems outside Google Cloud. Some logs arrive automatically, some need an agent or instrumentation, and some must be turned on.

Collecting everything at full detail is expensive and noisy. The second half of this topic is reducing volume at the source and at the Log Router without losing what you need.

SourcesVMs, GKE, servicesAgentsOps Agent, OpenTelemetryLog Routerfilters, exclusionsLog buckets_Required, _Default, customAnalysis

How it works

Where logs come from

SourceHow logs are collected
Compute Engine VMsInstall the Ops Agent; it collects system and application log files and sends them to Cloud Logging
GKEContainer stdout and stderr and system logs are collected by GKE's logging; choose system only or system and workloads
Cloud Run, Cloud Run functions, App Enginestdout, stderr and request logs collected automatically
Apps anywhereOpenTelemetry SDKs and collectors, or the Cloud Logging client libraries
Google Cloud APIsCloud Audit Logs
NetworksVPC Flow Logs, firewall rules logging, load balancer logs
Service meshCloud Service Mesh proxies produce access logs and telemetry for traffic between services

Write logs as structured JSON to stdout on GKE and Cloud Run. Fields such as severity and message are recognized, and the rest land in jsonPayload, ready to filter.

Ops Agent

The Ops Agent collects both logs and metrics from Linux and Windows VMs. It is configured in /etc/google-cloud-ops-agent/config.yaml with receivers and pipelines:

logging:
  receivers:
    app_log:
      type: files
      include_paths: [/var/log/myapp/*.log]
  service:
    pipelines:
      app_pipeline:
        receivers: [app_log]

Install it on many VMs with an agent policy or in the VM image. The VM's service account needs the Logs Writer and Monitoring Metric Writer roles.

OpenTelemetry

OpenTelemetry (OTel) is the vendor-neutral standard for logs, metrics and traces. Instrument the app with an OTel SDK and send data to an OTel collector, which exports to Google Cloud. It suits hybrid and multi-cloud estates because the same instrumentation works everywhere.

Cloud Audit Logs

TypeEnabledRecords
Admin ActivityAlways, cannot be disabledConfiguration changes and metadata writes
Data AccessOff by default except BigQuery; turn on per serviceReads of configuration or data, and user data writes
System EventAlwaysActions taken by Google systems, such as live migration
Policy DeniedAlways generatedAccess denied by a security policy, such as VPC Service Controls

Data Access logs can be very large. Enable them only for the services and projects where you need them.

VPC Flow Logs

VPC Flow Logs record samples of network flows to and from VM interfaces, usually configured per subnet (newer configurations can also cover a whole network, VPN tunnels or Interconnect attachments). You choose the aggregation interval, the sample rate and whether to include metadata, and can add a filter. Higher sampling and shorter intervals mean more detail and more cost.

Optimizing logs

TechniqueWhereEffect
Lower log level in productionApplicationFewer DEBUG lines at the source
SamplingSource settings, such as load balancer logging sample rate or flow log sample rateKeep a share of high-volume entries
Exclusion filtersSinks, including _DefaultExcluded entries are not stored in that bucket and are not charged for storage there
Sampled exclusionsample() in an exclusion filterKeep a percentage of noisy logs
RetentionPer log bucketShorter retention for verbose logs
Route cold logsSink to Cloud StorageCheaper long-term retention
resource.type="http_load_balancer"
AND httpRequest.status<400
AND sample(insertId, 0.9)

Used as an exclusion filter, this drops about 90% of successful load balancer requests while keeping every error.

A worked example

A team's Cloud Logging bill doubled. Log Analytics shows most volume is successful load balancer requests and DEBUG logs from one GKE namespace.

The engineer sets the application log level to INFO, adds the sampled exclusion above to the _Default sink, and lowers the VPC Flow Logs sample rate on busy subnets. Admin Activity audit logs are untouched because they are always kept in _Required.

Find volumeLog AnalyticsFix sourcelog levelExclude and sample_Default sinkLower flow log samplingBill drops

How the exam asks about this

  • "Application log files on Compute Engine VMs do not appear in Cloud Logging." Install and configure the Ops Agent, and check the service account roles.
  • "Record who read objects in a sensitive bucket." Enable Data Access audit logs for Cloud Storage in that project.
  • "Reduce logging cost while keeping all error entries." Add an exclusion filter, sampled if needed, that matches only non-error entries.

What to remember for the exam

  • Ops Agent for VMs; automatic collection for GKE, Cloud Run and App Engine; OpenTelemetry for anything, anywhere.
  • Admin Activity audit logs are always on; Data Access is mostly off by default.
  • VPC Flow Logs are per subnet with sampling and aggregation settings.
  • Reduce at the source first, then use exclusions and sample() at the Log Router.
  • Structured JSON logs make filtering, metrics and trace correlation possible.
Watch out

An exclusion filter on the _Default sink only stops storage in _Default. Logs still pass through the Log Router, so other sinks that match them still receive them.

Common mistakes

Turning on Data Access logs everywhere. Volume and cost explode. Enable them for the services that need an access record.

Writing plain text logs. Fields cannot be filtered or turned into metrics easily. Log JSON with severity and context fields.

Excluding logs needed for audits. Check retention requirements before excluding. Route them to cheap storage instead.

Check yourself

  • Which audit log type cannot be disabled?

    Admin Activity audit logs, which record configuration changes and are always written.

  • What does the Ops Agent collect?

    Logs and metrics from Compute Engine VMs, configured with receivers, processors and pipelines in its config file.

  • How can you keep 10% of noisy successful requests in logs?

    Add an exclusion filter that matches those entries with sample(insertId, 0.9), so about 90% are excluded and 10% kept.

Topic 1 of 6 in Implementing observability practices and troubleshooting issues

Test yourself on Implementing observability practices and troubleshooting issues

The full Google Cloud Professional Cloud DevOps Engineer question bank has 751 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.