<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>GCP Cloud Operations on Tarragon</title><link>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/</link><description>Recent content in GCP Cloud Operations on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Fri, 01 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/index.xml" rel="self" type="application/rss+xml"/><item><title>Cloud Monitoring Metrics Model 與 MQL</title><link>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-monitoring-mql/</link><pubDate>Mon, 22 Jun 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-monitoring-mql/</guid><description>&lt;blockquote>
&lt;p>本文是 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations&lt;/a> 的 vendor deep article，深化 overview「Cloud Monitoring uptime checks / SLO」跟「OTLP integration」段。初次接觸 GCP 觀測的讀者建議先讀 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁&lt;/a>。&lt;/p>&lt;/blockquote>
&lt;h2 id="問題情境">問題情境&lt;/h2>
&lt;p>GCP 服務預設把 metrics 寫到 Cloud Monitoring，工程師打開 Metrics Explorer 就能看到 CPU、記憶體、request count。問題通常出在三個地方：GCP 內建 metrics 的 resource model 跟應用層的 business metrics 用不同語言描述同一件事，PromQL 使用者要重新學 MQL 語法，alerting policy 的 condition type 跟 notification channel 配置比預期複雜。理解 Cloud Monitoring 的 metrics model 才能避免 custom metrics 爆量、alert noise、跟 Prometheus 生態的銜接摩擦。&lt;/p>
&lt;h2 id="核心概念">核心概念&lt;/h2>
&lt;h3 id="monitored-resource-與-metric-descriptor">Monitored resource 與 metric descriptor&lt;/h3>
&lt;p>Cloud Monitoring 的資料模型有兩個軸：&lt;strong>monitored resource&lt;/strong> 描述「誰產生了這個 metric」，&lt;strong>metric descriptor&lt;/strong> 描述「這個 metric 量什麼」。&lt;/p>
&lt;p>Monitored resource 是 GCP 自動帶入的標籤集合。GKE pod 的 monitored resource type 是 &lt;code>k8s_pod&lt;/code>，帶 &lt;code>project_id&lt;/code>、&lt;code>location&lt;/code>、&lt;code>cluster_name&lt;/code>、&lt;code>namespace_name&lt;/code>、&lt;code>pod_name&lt;/code>。Cloud Run revision 是 &lt;code>cloud_run_revision&lt;/code>，帶 &lt;code>service_name&lt;/code>、&lt;code>revision_name&lt;/code>、&lt;code>location&lt;/code>。這層標籤不需要工程師手動設定，GCP agent 或 SDK 自動填入。&lt;/p>
&lt;p>Metric descriptor 定義 metric 的名稱、型別（GAUGE / DELTA / CUMULATIVE）、value type（INT64 / DOUBLE / DISTRIBUTION）與自訂 label。GCP 內建 metrics 用 &lt;code>compute.googleapis.com/instance/cpu/utilization&lt;/code> 這樣的命名空間格式；custom metrics 用 &lt;code>custom.googleapis.com/&amp;lt;your-name&amp;gt;&lt;/code> 或 &lt;code>workload.googleapis.com/&amp;lt;your-name&amp;gt;&lt;/code>（後者透過 OTel Collector 或 Managed Prometheus 寫入時使用）。&lt;/p>
&lt;p>兩個軸相乘就是 time series 的數量。Cardinality 管理在 GCP 上等同於控制 monitored resource × metric label 的組合數。GCP 對 custom metrics 有每個 project 的 time series 配額（預設 500 per metric descriptor、可申請提高），超過時寫入會被拒。&lt;/p>
&lt;h3 id="mql-vs-promql">MQL vs PromQL&lt;/h3>
&lt;p>Cloud Monitoring 有兩種查詢語言。MQL（Monitoring Query Language）是 GCP 自家設計的 pipeline 語法：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">fetch k8s_container
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">| metric &amp;#39;kubernetes.io/container/cpu/core_usage_time&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">| align rate(1m)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">| every 1m
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">| group_by [resource.cluster_name, resource.namespace_name],
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl"> [value_cpu_usage: aggregate(value.core_usage_time)]&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>PromQL 在 Cloud Monitoring 上也可用（透過 Managed Service for Prometheus）。兩者的核心差異：&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>本文是 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations</a> 的 vendor deep article，深化 overview「Cloud Monitoring uptime checks / SLO」跟「OTLP integration」段。初次接觸 GCP 觀測的讀者建議先讀 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>。</p></blockquote>
<h2 id="問題情境">問題情境</h2>
<p>GCP 服務預設把 metrics 寫到 Cloud Monitoring，工程師打開 Metrics Explorer 就能看到 CPU、記憶體、request count。問題通常出在三個地方：GCP 內建 metrics 的 resource model 跟應用層的 business metrics 用不同語言描述同一件事，PromQL 使用者要重新學 MQL 語法，alerting policy 的 condition type 跟 notification channel 配置比預期複雜。理解 Cloud Monitoring 的 metrics model 才能避免 custom metrics 爆量、alert noise、跟 Prometheus 生態的銜接摩擦。</p>
<h2 id="核心概念">核心概念</h2>
<h3 id="monitored-resource-與-metric-descriptor">Monitored resource 與 metric descriptor</h3>
<p>Cloud Monitoring 的資料模型有兩個軸：<strong>monitored resource</strong> 描述「誰產生了這個 metric」，<strong>metric descriptor</strong> 描述「這個 metric 量什麼」。</p>
<p>Monitored resource 是 GCP 自動帶入的標籤集合。GKE pod 的 monitored resource type 是 <code>k8s_pod</code>，帶 <code>project_id</code>、<code>location</code>、<code>cluster_name</code>、<code>namespace_name</code>、<code>pod_name</code>。Cloud Run revision 是 <code>cloud_run_revision</code>，帶 <code>service_name</code>、<code>revision_name</code>、<code>location</code>。這層標籤不需要工程師手動設定，GCP agent 或 SDK 自動填入。</p>
<p>Metric descriptor 定義 metric 的名稱、型別（GAUGE / DELTA / CUMULATIVE）、value type（INT64 / DOUBLE / DISTRIBUTION）與自訂 label。GCP 內建 metrics 用 <code>compute.googleapis.com/instance/cpu/utilization</code> 這樣的命名空間格式；custom metrics 用 <code>custom.googleapis.com/&lt;your-name&gt;</code> 或 <code>workload.googleapis.com/&lt;your-name&gt;</code>（後者透過 OTel Collector 或 Managed Prometheus 寫入時使用）。</p>
<p>兩個軸相乘就是 time series 的數量。Cardinality 管理在 GCP 上等同於控制 monitored resource × metric label 的組合數。GCP 對 custom metrics 有每個 project 的 time series 配額（預設 500 per metric descriptor、可申請提高），超過時寫入會被拒。</p>
<h3 id="mql-vs-promql">MQL vs PromQL</h3>
<p>Cloud Monitoring 有兩種查詢語言。MQL（Monitoring Query Language）是 GCP 自家設計的 pipeline 語法：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">fetch k8s_container
</span></span><span class="line"><span class="ln">2</span><span class="cl">| metric &#39;kubernetes.io/container/cpu/core_usage_time&#39;
</span></span><span class="line"><span class="ln">3</span><span class="cl">| align rate(1m)
</span></span><span class="line"><span class="ln">4</span><span class="cl">| every 1m
</span></span><span class="line"><span class="ln">5</span><span class="cl">| group_by [resource.cluster_name, resource.namespace_name],
</span></span><span class="line"><span class="ln">6</span><span class="cl">    [value_cpu_usage: aggregate(value.core_usage_time)]</span></span></code></pre></div><p>PromQL 在 Cloud Monitoring 上也可用（透過 Managed Service for Prometheus）。兩者的核心差異：</p>
<table>
  <thead>
      <tr>
          <th>面向</th>
          <th>MQL</th>
          <th>PromQL（via Managed Prometheus）</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>資料來源</td>
          <td>所有 Cloud Monitoring metrics</td>
          <td>透過 Managed Prometheus 寫入的 metrics</td>
      </tr>
      <tr>
          <td>查詢介面</td>
          <td>Metrics Explorer / alerting condition</td>
          <td>Grafana / Prometheus UI / API</td>
      </tr>
      <tr>
          <td>Aggregation 語法</td>
          <td>pipe-style <code>group_by</code></td>
          <td>函式風格 <code>sum by (label)</code></td>
      </tr>
      <tr>
          <td>跨 GCP 與 custom</td>
          <td>原生支援 GCP 內建 metrics</td>
          <td>需要轉成 Prometheus 格式</td>
      </tr>
      <tr>
          <td>學習曲線</td>
          <td>GCP-specific、不可搬到其他平台</td>
          <td>跨平台標準、可搬到 Mimir / Thanos</td>
      </tr>
  </tbody>
</table>
<p>選擇判讀：純 GCP 環境且團隊沒有 Prometheus 經驗 → MQL 起步快。已有 Prometheus / Grafana 生態 → 用 Managed Prometheus + PromQL、把 GCP 內建 metrics 透過 Prometheus-compatible exporter 導入。混合環境 → 兩者並存、GCP 原生 metrics 用 MQL 做 alerting、application metrics 用 PromQL 查詢。</p>
<h2 id="配置-step-by-step">配置 step-by-step</h2>
<h3 id="custom-metrics-設計與寫入">Custom metrics 設計與寫入</h3>
<p>Custom metrics 的常見路徑有三條：</p>
<p><strong>路徑一：Cloud Monitoring API 直接寫入</strong>。應用程式用 Cloud Monitoring client library 建立 metric descriptor 並寫入 time series。適合 GCP-native 應用，不需要額外 agent。</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">metric type: custom.googleapis.com/checkout/latency_ms
</span></span><span class="line"><span class="ln">2</span><span class="cl">kind: GAUGE
</span></span><span class="line"><span class="ln">3</span><span class="cl">value type: DISTRIBUTION
</span></span><span class="line"><span class="ln">4</span><span class="cl">labels: [service, region, status_code]</span></span></code></pre></div><p><strong>路徑二：OTel Collector + GCP exporter</strong>。應用程式用 OTel SDK 產生 metrics，OTel Collector 透過 <code>googlecloud</code> exporter 寫到 Cloud Monitoring。Metrics 命名空間是 <code>workload.googleapis.com/</code>。適合已有 OTel instrumentation 的服務。</p>
<p><strong>路徑三：Managed Service for Prometheus</strong>。部署 GCP 的 Managed Prometheus collector（或自管 Prometheus + remote write），metrics 存在 GCP 託管的 Monarch backend。查詢用 PromQL。適合 Kubernetes 環境且團隊熟悉 Prometheus 生態。</p>
<p>三條路徑可以共存。選擇判讀：先看團隊的 metrics 生態是 GCP-native 還是 Prometheus-native，再看 multi-cloud 需求。Managed Prometheus 的優勢是 PromQL 可搬、劣勢是 GCP 內建 metrics 需要額外整合。</p>
<h3 id="alerting-policy-配置">Alerting policy 配置</h3>
<p>Cloud Monitoring alerting policy 由三部分組成：condition、notification channel、documentation。</p>
<p>Condition types：</p>
<ul>
<li><strong>Metric threshold</strong>：metric 超過閾值 N 分鐘。適合「error rate &gt; 1% 持續 5 分鐘」。</li>
<li><strong>Metric absence</strong>：metric 消失。適合偵測 scrape 斷裂或服務停擺。</li>
<li><strong>Forecasting</strong>：預測 metric 在 N 小時後超過閾值。適合 disk 滿、quota 耗盡。</li>
<li><strong>Process health</strong>：GCE instance 的 process 是否存活。</li>
<li><strong>Log-based</strong>：Cloud Logging 出現特定 pattern 時觸發。適合把 error log 轉成 alert。</li>
<li><strong>SLO burn rate</strong>：SLO 設定後、burn rate 超過閾值。對應 <a href="/blog/backend/knowledge-cards/burn-rate/" data-link-title="Burn Rate" data-link-desc="說明 error budget 消耗速度如何支援告警與事故分級">burn-rate</a> 概念。</li>
</ul>
<p>Notification channels：Email / PagerDuty / Slack / Pub/Sub / Webhook / SMS。Pub/Sub channel 適合接自定義 automation（收到 alert → trigger Cloud Function）。</p>
<p>Snooze 與 maintenance window：暫時抑制特定 alerting policy。部署期間或已知維護時使用。</p>
<h3 id="managed-prometheus-整合">Managed Prometheus 整合</h3>
<p>GCP Managed Service for Prometheus 的部署模式：</p>
<ul>
<li><strong>GKE 模式</strong>：啟用 GKE monitoring、Managed Prometheus collector 自動部署。不需要自管 Prometheus server。</li>
<li><strong>Remote write 模式</strong>：自管 Prometheus server + <code>remote_write</code> 到 GCP Monarch endpoint。保留本地查詢能力，同時長期儲存在 GCP。</li>
<li><strong>OTel Collector 模式</strong>：OTel Collector 用 <code>googlemanagedprometheus</code> exporter 寫到 Monarch。</li>
</ul>
<p>查詢端：用 GCP Console 的 PromQL UI、或部署 Grafana + GMP datasource。PromQL 功能子集支援良好（rate / histogram_quantile / aggregation），少數進階功能（subquery）有限制。</p>
<h2 id="故障演練與邊界">故障演練與邊界</h2>
<h3 id="custom-metric-配額用盡">Custom metric 配額用盡</h3>
<p><strong>觸發條件</strong>：custom metric descriptor 數量超過 project 配額（預設 500），或單一 metric descriptor 的 time series 數量超過配額。</p>
<p><strong>表現</strong>：API 回傳 429 或 quota exceeded error。新 time series 寫不進去，既有的不受影響。</p>
<p><strong>修復</strong>：清理不再使用的 metric descriptor（describe → delete）、合併語意重疊的 metrics、減少 label cardinality。GCP Console → IAM → Quotas 可以申請提高配額，但先確認是設計問題而非真的需要那麼多 series。</p>
<h3 id="alerting-policy-觸發延遲">Alerting policy 觸發延遲</h3>
<p><strong>觸發條件</strong>：alerting policy 使用的 metrics 的 alignment period 或 duration 設定過長。</p>
<p><strong>表現</strong>：異常已經發生 10 分鐘，alert 才觸發。原因是 Cloud Monitoring 的 evaluation cycle 跟 metrics ingestion delay 相加。GCP 內建 metrics 的 ingestion delay 約 1-3 分鐘；custom metrics 透過 API 寫入的 delay 約 10-30 秒。</p>
<p><strong>修復</strong>：把 condition 的 alignment period 設短（1 分鐘）、duration 設短（但太短會造成 flapping）。Log-based alerting condition 的 delay 通常比 metric-based 短（秒級 vs 分鐘級），緊急異常考慮用 log-based condition。</p>
<h3 id="managed-prometheus-查詢與自管-prometheus-結果不一致">Managed Prometheus 查詢與自管 Prometheus 結果不一致</h3>
<p><strong>觸發條件</strong>：同一個 PromQL query 在本地 Prometheus 跟 GMP 的結果不同。</p>
<p><strong>表現</strong>：dashboard 數字對不上、alert 觸發行為不一致。</p>
<p><strong>修復</strong>：先確認 remote write 是否有 sample drop（看 <code>prometheus_remote_storage_samples_failed_total</code>）。再確認 GMP 的 PromQL 子集限制（部分 subquery 語法不支援）。最後確認 metric naming：local Prometheus 的 metric name 跟 GMP 儲存後的 naming convention 可能有差異（加了 <code>__name__</code> prefix 或 resource label）。</p>
<h2 id="容量與成本">容量與成本</h2>
<p>Cloud Monitoring 的計費模型基於 <strong>ingested metrics volume</strong>（per million data points）。GCP 內建 metrics（agent metrics 除外）免費。Custom metrics 的前 150 MB per billing account 免費，超過後按 volume 計費。</p>
<p>成本治理的判讀：</p>
<ul>
<li>最大成本來源通常是高頻率的 custom metrics 或高 cardinality label</li>
<li>用 <code>monitoring.googleapis.com/billing/bytes_ingested</code> metric 追蹤 ingestion 量</li>
<li>減少 scrape interval（15s → 30s 或 60s）可以直接降低 ingestion 量</li>
<li>Managed Prometheus 的計費跟 custom metrics 分開計算（per samples ingested）</li>
</ul>
<h2 id="整合與下一步">整合與下一步</h2>
<ul>
<li><a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>：overview 與日常操作</li>
<li><a href="/blog/backend/04-observability/cardinality-cost-governance/" data-link-title="4.7 Cardinality 治理與成本邊界" data-link-desc="把 metric / log / trace 的 cardinality 與成本作為平台一級治理議題">4.7 cardinality 治理</a>：cardinality 治理的完整策略</li>
<li><a href="/blog/backend/04-observability/sli-slo-signal/" data-link-title="4.6 SLI 量測與 SLO 訊號設計" data-link-desc="把可靠性目標的訊號從 metric 端設計好、餵給 6.6 SLO 政策">4.6 SLI/SLO signal</a>：SLO burn rate alert 的訊號設計</li>
<li><a href="/blog/backend/04-observability/vendors/prometheus/" data-link-title="Prometheus" data-link-desc="Pull-based metrics 主流 OSS、PromQL 與 alerting">Prometheus</a>：Managed Prometheus 的上游概念</li>
<li><a href="/blog/backend/04-observability/vendors/opentelemetry/" data-link-title="OpenTelemetry" data-link-desc="可觀測性開放標準、SDK 與 Collector">OpenTelemetry</a>：OTel Collector + GCP exporter 整合</li>
<li><a href="../cloud-logging-export-compliance/">Cloud Logging 查詢、匯出與合規</a>：同 vendor 的 logs 面</li>
</ul>
]]></content:encoded></item><item><title>Cloud Logging 查詢、匯出與合規</title><link>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-logging-export-compliance/</link><pubDate>Mon, 22 Jun 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-logging-export-compliance/</guid><description>&lt;blockquote>
&lt;p>本文是 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations&lt;/a> 的 vendor deep article，深化 overview「Cloud Logging 結構化 logs」跟「BigQuery 匯出長期儲存」段。初次接觸 GCP 觀測的讀者建議先讀 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁&lt;/a>。&lt;/p>&lt;/blockquote>
&lt;h2 id="問題情境">問題情境&lt;/h2>
&lt;p>Cloud Logging 對 GCP 服務是預設開啟的 — GKE、Cloud Run、Cloud Functions 的 stdout/stderr 自動進 Cloud Logging，工程師不需要配置就能查。問題出在後續階段：log 量成長後的成本控制（GCP 的 ingestion 計費讓高 volume 服務成本快速累積）、合規需求要求特定 log 保留特定時間（healthcare / fintech 的 7 年留存）、organization-level 的 log 聚合與存取控制（多 project 集中 audit）、以及 PII 在 log 中的遮罩與加密。理解 Cloud Logging 的 router / sink 架構跟 retention bucket 才能從「預設全收」走向「可治理的 log pipeline」。&lt;/p>
&lt;h2 id="核心概念">核心概念&lt;/h2>
&lt;h3 id="log-router-與-sink">Log Router 與 Sink&lt;/h3>
&lt;p>Cloud Logging 的資料流是 &lt;strong>log entry → log router → sink → destination&lt;/strong>。每一筆 log 進入 Cloud Logging 後，log router 根據 inclusion filter 跟 exclusion filter 決定這筆 log 送到哪些 destination。&lt;/p>
&lt;p>&lt;strong>Sink&lt;/strong> 是 log router 的輸出端點。每個 GCP project 預設有兩個 sink：&lt;code>_Required&lt;/code>（admin activity audit log、system event，不可關閉）和 &lt;code>_Default&lt;/code>（其他所有 log、送到 &lt;code>_Default&lt;/code> log bucket、可修改 filter）。工程師可以建立自訂 sink，把符合條件的 log 送到 BigQuery、Cloud Storage、Pub/Sub 或 Splunk。&lt;/p>
&lt;p>&lt;strong>Exclusion filter&lt;/strong> 在 log router 層攔截 — 被排除的 log 不會寫入任何 sink destination，也不計入 ingestion 計費。這是成本控制的第一道防線。&lt;/p>
&lt;p>&lt;strong>Inclusion filter&lt;/strong> 在 sink 層生效 — 只有符合 filter 的 log 會送到該 sink 的 destination。&lt;/p>
&lt;p>路由順序很重要：exclusion filter 先執行（全域攔截），然後 &lt;code>_Required&lt;/code> sink 攔走必留 log，然後 &lt;code>_Default&lt;/code> sink 跟自訂 sink 各自的 inclusion filter 平行執行。一筆 log 可以同時送到多個 sink。&lt;/p>
&lt;h3 id="retention-與-log-bucket">Retention 與 Log Bucket&lt;/h3>
&lt;p>Cloud Logging 的儲存單位是 &lt;strong>log bucket&lt;/strong>。每個 project 預設有兩個 bucket：&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>本文是 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations</a> 的 vendor deep article，深化 overview「Cloud Logging 結構化 logs」跟「BigQuery 匯出長期儲存」段。初次接觸 GCP 觀測的讀者建議先讀 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>。</p></blockquote>
<h2 id="問題情境">問題情境</h2>
<p>Cloud Logging 對 GCP 服務是預設開啟的 — GKE、Cloud Run、Cloud Functions 的 stdout/stderr 自動進 Cloud Logging，工程師不需要配置就能查。問題出在後續階段：log 量成長後的成本控制（GCP 的 ingestion 計費讓高 volume 服務成本快速累積）、合規需求要求特定 log 保留特定時間（healthcare / fintech 的 7 年留存）、organization-level 的 log 聚合與存取控制（多 project 集中 audit）、以及 PII 在 log 中的遮罩與加密。理解 Cloud Logging 的 router / sink 架構跟 retention bucket 才能從「預設全收」走向「可治理的 log pipeline」。</p>
<h2 id="核心概念">核心概念</h2>
<h3 id="log-router-與-sink">Log Router 與 Sink</h3>
<p>Cloud Logging 的資料流是 <strong>log entry → log router → sink → destination</strong>。每一筆 log 進入 Cloud Logging 後，log router 根據 inclusion filter 跟 exclusion filter 決定這筆 log 送到哪些 destination。</p>
<p><strong>Sink</strong> 是 log router 的輸出端點。每個 GCP project 預設有兩個 sink：<code>_Required</code>（admin activity audit log、system event，不可關閉）和 <code>_Default</code>（其他所有 log、送到 <code>_Default</code> log bucket、可修改 filter）。工程師可以建立自訂 sink，把符合條件的 log 送到 BigQuery、Cloud Storage、Pub/Sub 或 Splunk。</p>
<p><strong>Exclusion filter</strong> 在 log router 層攔截 — 被排除的 log 不會寫入任何 sink destination，也不計入 ingestion 計費。這是成本控制的第一道防線。</p>
<p><strong>Inclusion filter</strong> 在 sink 層生效 — 只有符合 filter 的 log 會送到該 sink 的 destination。</p>
<p>路由順序很重要：exclusion filter 先執行（全域攔截），然後 <code>_Required</code> sink 攔走必留 log，然後 <code>_Default</code> sink 跟自訂 sink 各自的 inclusion filter 平行執行。一筆 log 可以同時送到多個 sink。</p>
<h3 id="retention-與-log-bucket">Retention 與 Log Bucket</h3>
<p>Cloud Logging 的儲存單位是 <strong>log bucket</strong>。每個 project 預設有兩個 bucket：</p>
<ul>
<li><code>_Required</code> bucket：admin activity audit log 跟 system event，保留 400 天，不可刪除或修改 retention</li>
<li><code>_Default</code> bucket：其他所有 log，預設保留 30 天，可調整為 1-3650 天</li>
</ul>
<p>自訂 log bucket 可以設定不同 retention 期。常見用法：把 application log 留 30 天、把 audit log 留 7 年（送到自訂 bucket 或 BigQuery）。</p>
<p>Cloud Logging 的 ingestion 計費跟 storage 計費是分開的。前 50 GiB/month per billing account 的 ingestion 免費；超過後按 ingestion volume 計費。<code>_Required</code> log 的 ingestion 免費。Storage 在 <code>_Default</code> bucket 的前 0.5 GiB 免費，自訂 bucket 按用量計費。</p>
<p>成本治理判讀：高 volume 服務（例如 GKE 的 container stdout）的成本主要來自 ingestion，而非 storage。Exclusion filter 攔掉不需要的 log 是最直接的降成本方式。</p>
<h3 id="查詢語言">查詢語言</h3>
<p>Cloud Logging 的查詢語言用在 Logs Explorer 跟 gcloud CLI：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">resource.type=&#34;k8s_container&#34;
</span></span><span class="line"><span class="ln">2</span><span class="cl">resource.labels.cluster_name=&#34;prod-us-central1&#34;
</span></span><span class="line"><span class="ln">3</span><span class="cl">severity&gt;=ERROR
</span></span><span class="line"><span class="ln">4</span><span class="cl">jsonPayload.order_id=&#34;ord-12345&#34;
</span></span><span class="line"><span class="ln">5</span><span class="cl">timestamp&gt;=&#34;2026-06-22T00:00:00Z&#34;</span></span></code></pre></div><p>語法特點：field path 用 <code>.</code> 分隔、支援 comparison operators（<code>=</code> / <code>!=</code> / <code>&gt;</code> / <code>&gt;=</code> / <code>&lt;</code> / <code>&lt;=</code>）、支援 boolean（<code>AND</code> / <code>OR</code> / <code>NOT</code>）、支援 regex（<code>=~</code> / <code>!~</code>）。</p>
<p>跟 KQL（Elastic）或 LogQL（Loki）相比，Cloud Logging 查詢語言更接近 structured filter 而非 full-text search。Full-text 搜尋要用 <code>textPayload:</code> 或 <code>jsonPayload:</code> prefix。進階分析（aggregation、time bucketing、join）需要匯出到 BigQuery 後用 SQL 做。</p>
<h2 id="配置-step-by-step">配置 step-by-step</h2>
<h3 id="organization-level-log-聚合">Organization-level log 聚合</h3>
<p>多 project 環境下，集中 log 的標準做法是在 organization 或 folder level 建立 aggregated sink：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">gcloud logging sinks create org-audit-sink \
</span></span><span class="line"><span class="ln">2</span><span class="cl">  bigquery.googleapis.com/projects/central-audit/datasets/org_audit_logs \
</span></span><span class="line"><span class="ln">3</span><span class="cl">  --organization=123456789 \
</span></span><span class="line"><span class="ln">4</span><span class="cl">  --include-children \
</span></span><span class="line"><span class="ln">5</span><span class="cl">  --log-filter=&#39;logName:&#34;cloudaudit.googleapis.com&#34;&#39;</span></span></code></pre></div><p><code>--include-children</code> 讓 organization 下所有 project、folder 的符合 log 都送到同一個 BigQuery dataset。Sink 的 service account 需要 destination 的寫入權限（BigQuery Data Editor）。</p>
<p>適用場景：SOC 團隊需要跨 project 的 audit log 查詢、compliance team 需要集中的 data access log 存檔、security team 需要異常 IAM 變更的全域偵測。</p>
<h3 id="data-access-audit-logs-啟用">Data Access Audit Logs 啟用</h3>
<p>GCP 的 audit log 分三類：</p>
<ul>
<li><strong>Admin Activity</strong>：對資源的管理操作（建立 / 刪除 / 修改 IAM）。預設開啟、不可關閉、不計費。</li>
<li><strong>Data Access</strong>：對資源的讀取操作（BigQuery query、GCS read、Cloud SQL connect）。預設關閉（除 BigQuery）、需手動啟用、計費。</li>
<li><strong>System Event</strong>：GCP 系統自動操作。預設開啟、不可關閉、不計費。</li>
</ul>
<p>Data Access audit log 的啟用是 per-service、per-project（或 org level）。啟用後 log 量會大幅增加 — 一個高 QPS 的 Cloud SQL 服務可能每秒產生數百筆 data access log。成本跟 volume 判讀要先做。</p>
<p>建議做法：先對 security-sensitive 服務啟用（IAM / KMS / Cloud SQL / GCS），其他服務按需啟用。用 exclusion filter 精細控制 — 例如只保留 <code>ADMIN_READ</code> 跟 <code>DATA_WRITE</code>、排除 <code>DATA_READ</code>（read 量通常遠大於 write）。</p>
<h3 id="vpc-flow-logs-與-dns-logs-的觀測用途">VPC Flow Logs 與 DNS Logs 的觀測用途</h3>
<p>VPC Flow Logs 記錄每一筆通過 VPC 的網路流量元資料（src/dst IP、port、protocol、bytes、packets）。啟用方式是 per-subnet 設定、支援 sampling rate（100% / 50% / 10%）。</p>
<p>DNS Logs 記錄 VPC 內的 DNS 查詢（query name、response code、source VM）。啟用方式是 per-VPC 或 per-policy 設定。</p>
<p>觀測用途：</p>
<ul>
<li><strong>異常流量偵測</strong>：VPC Flow Logs 送到 BigQuery 後用 SQL 找出異常流量模式（大量對外連線、非預期 port、跨 region 資料傳輸）</li>
<li><strong>網路效能分析</strong>：量測 inter-service latency、跨 AZ 流量比例</li>
<li><strong>安全稽核</strong>：DNS Logs 偵測 DNS tunneling 或 C2 callback</li>
</ul>
<p>成本注意：VPC Flow Logs 在高流量服務上的 ingestion 量非常大。100% sampling + 高 QPS 服務可能每天產生 TB 級 log。建議用 sampling rate 控制、或只對 security-sensitive subnet 啟用 100%。</p>
<h3 id="自建-vs-managed-pipeline-的取捨">自建 vs managed pipeline 的取捨</h3>
<p><a href="/blog/backend/04-observability/cases/cloudflare-internal-observability-architecture/" data-link-title="4.C12 Cloudflare：內部觀測平台的三層能力" data-link-desc="全球 300&#43; edge 節點的觀測架構，把 monitoring、analytics 與 forensics 拆成三個獨立能力層。">Cloudflare 觀測案例</a>展示了自建觀測 pipeline 的理由 — 全球 300+ edge locations、每秒數十億 request 的規模下，SaaS 觀測平台的帳單不合理，自建 pipeline 的 compute 成本反而更低。</p>
<p>但多數團隊的結論是反過來的。GCP 環境下，Cloud Logging 的 managed pipeline（log entry → router → sink → BigQuery / Cloud Storage）幾乎不需要維運人力。自建等價的 pipeline（Fluent Bit → Kafka → Elasticsearch / BigQuery）需要維運 Kafka cluster、Elasticsearch cluster、Fluent Bit DaemonSet 的升級與監控。</p>
<p>判斷分水嶺的兩個維度：</p>
<table>
  <thead>
      <tr>
          <th>維度</th>
          <th>偏向 managed（Cloud Logging）</th>
          <th>偏向自建</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Log volume</td>
          <td>&lt; 1 TB/day</td>
          <td>&gt; 10 TB/day（SaaS ingestion 成本超過自建 compute）</td>
      </tr>
      <tr>
          <td>查詢需求</td>
          <td>Logs Insights + 偶爾 BigQuery</td>
          <td>需要 Elasticsearch 的全文搜尋 + aggregation + visualization</td>
      </tr>
  </tbody>
</table>
<p>1-10 TB/day 的灰色地帶取決於查詢模式 — 如果 Logs Insights 能滿足 90% 的查詢、BigQuery 能處理剩下 10% 的分析，不需要自建。如果團隊需要 Kibana dashboard、Elasticsearch alerting、或跨 cloud 的統一 log backend，自建可能更合理。</p>
<h3 id="healthcare-分層-retention-在-gcp-的實現">Healthcare 分層 retention 在 GCP 的實現</h3>
<p><a href="/blog/backend/04-observability/cases/healthcare-access-traceability-and-retention/" data-link-title="Healthcare：存取可追溯性與保留邊界" data-link-desc="在資料主權限制下，建立可追溯存取證據與分層保留策略。">Healthcare 案例</a>的核心需求是分層 retention — 不同 log 類型有不同的法規留存要求（data access audit log 要 6 年+、application operational log 要 90 天、debug log 要 7 天）。</p>
<p>在 GCP 上用三層架構實現：</p>
<p><strong>Hot 層（Cloud Logging custom bucket）</strong>：application log 保留 90 天、audit log 保留 1 年。設定 custom log bucket + retention。優點是 Logs Explorer 直接可查、延遲低。</p>
<p><strong>Warm 層（BigQuery）</strong>：audit log sink 到 BigQuery dataset，BigQuery 的 partition expiration 設 2 年。需要分析跟 correlation 時用 SQL 查。成本低於 Cloud Logging storage。</p>
<p><strong>Cold 層（Cloud Storage + Object Lifecycle）</strong>：BigQuery 的 scheduled export 或直接 Cloud Logging sink 到 GCS bucket。Object lifecycle rule 把 90 天以上的 object 轉 Nearline / Coldline / Archive class。最終刪除設定在 7 年。</p>
<p>三層各自的 access control 要獨立設定 — cold 層的 GCS bucket 只有 compliance team 有讀取權限，application team 看不到。CMEK 在三層都啟用（Cloud Logging custom bucket 的 CMEK + BigQuery dataset 的 CMEK + GCS bucket 的 CMEK），金鑰由安全團隊集中管理。</p>
<h3 id="pii-治理與-cmek">PII 治理與 CMEK</h3>
<p>Cloud Logging 中的 PII 治理有三層：</p>
<p><strong>第一層：不寫入</strong>。Application 端在 log 之前就遮罩 PII（email → <code>***@***.com</code>、credit card → last 4 digits）。這是最有效的方式，因為一旦寫入 Cloud Logging，即使後續刪除 log entry，在 deletion 前可能已經被 sink 匯出到 BigQuery / GCS。</p>
<p><strong>第二層：log 層過濾</strong>。用 exclusion filter 把含 PII 的 log field 排除（例如排除特定 jsonPayload field）。限制是 Cloud Logging 的 exclusion filter 只能排除整筆 log entry，不能 redact 單一 field。需要 field-level redaction 的話，在 OTel Collector 或 Fluentd 層做 processor 處理、再送到 Cloud Logging。</p>
<p><strong>第三層：加密</strong>。Cloud Logging 預設用 Google-managed encryption。需要自管金鑰的場景（HIPAA / PCI-DSS / 金融監管）用 CMEK（Customer-Managed Encryption Keys）。CMEK 設定在 log bucket 層 — 自訂 log bucket 可以指定 Cloud KMS key。<code>_Default</code> bucket 也可以啟用 CMEK（需要把 <code>_Default</code> bucket 的 region 從 <code>global</code> 改成特定 region）。</p>
<p>存取控制：Cloud Logging 的 IAM role 分 <code>roles/logging.viewer</code>（讀 log）、<code>roles/logging.privateLogViewer</code>（讀含 data access 的 log）、<code>roles/logging.admin</code>（管理 sink / bucket / filter）。Audit log 的存取用 <code>roles/logging.privateLogViewer</code>、不是一般的 <code>roles/logging.viewer</code>。對應 <a href="/blog/backend/07-security-data-protection/audit-trail-and-accountability-boundary/" data-link-title="7.7 稽核追蹤與責任邊界" data-link-desc="以問題驅動方式整理高風險操作追蹤、可回查與責任切分">稽核追蹤與責任邊界</a> 的 GCP 實作。</p>
<h2 id="故障演練與邊界">故障演練與邊界</h2>
<h3 id="exclusion-filter-設太寬重要-log-被丟掉">Exclusion filter 設太寬，重要 log 被丟掉</h3>
<p><strong>觸發條件</strong>：為了降成本建立 exclusion filter，但 filter expression 太寬泛（例如排除整個 severity=INFO），連帶排除了 business-critical 的 info-level log。</p>
<p><strong>表現</strong>：事故時查不到關鍵 log、audit 證據鏈斷裂。因為 exclusion filter 在 ingestion 前執行，被排除的 log 無法回補。</p>
<p><strong>預防</strong>：exclusion filter 建立後先用 <code>gcloud logging read</code> 驗證哪些 log 會被排除。用 Logs Explorer 的 preview 功能確認 filter 不會命中關鍵 log。對 audit log 和 security log 不設 exclusion filter。</p>
<h3 id="bigquery-sink-匯出成本失控">BigQuery sink 匯出成本失控</h3>
<p><strong>觸發條件</strong>：org-level aggregated sink 把所有 log 送到 BigQuery，沒有 inclusion filter 限制。</p>
<p><strong>表現</strong>：BigQuery storage 跟 streaming insert 成本暴增。一個中型 GKE cluster 每天可能產生 100+ GB 的 container log，全部送 BigQuery 的月成本可能超過 Cloud Logging 本身。</p>
<p><strong>修復</strong>：在 sink 加 inclusion filter（只送 audit log 或 error-level log 到 BigQuery）。高 volume 的 application log 送 Cloud Storage（成本更低），需要查詢時用 BigQuery external table 做 federated query。</p>
<h3 id="log-entry-size-超過限制">Log entry size 超過限制</h3>
<p><strong>觸發條件</strong>：application log 寫入超過 256 KB 的單筆 log entry（Cloud Logging 的 per-entry 上限）。</p>
<p><strong>表現</strong>：超過限制的 log entry 被截斷或拒絕寫入。</p>
<p><strong>修復</strong>：application 端控制 log entry size — 大型 payload（request body / response body / stack trace）做 truncation 後再 log。需要完整內容的場景，把 payload 寫到 GCS、log 中只留 GCS URI。</p>
<h2 id="容量與成本">容量與成本</h2>
<table>
  <thead>
      <tr>
          <th>計費項目</th>
          <th>免費額度</th>
          <th>超出後計費</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ingestion（非 <code>_Required</code>）</td>
          <td>50 GiB/month per billing account</td>
          <td>per GiB ingested</td>
      </tr>
      <tr>
          <td>Storage（<code>_Default</code> bucket）</td>
          <td>0.5 GiB</td>
          <td>per GiB-month</td>
      </tr>
      <tr>
          <td>Storage（custom bucket）</td>
          <td>無免費額度</td>
          <td>per GiB-month</td>
      </tr>
      <tr>
          <td><code>_Required</code> log ingestion</td>
          <td>不計費</td>
          <td>不計費</td>
      </tr>
      <tr>
          <td>BigQuery sink streaming insert</td>
          <td>依 BigQuery 計費</td>
          <td>per GB inserted</td>
      </tr>
  </tbody>
</table>
<p>成本最佳化優先序：</p>
<ol>
<li><strong>Exclusion filter</strong>：攔掉不需要的 log、最直接</li>
<li><strong>降 log level</strong>：application 端把 verbose debug log 關掉</li>
<li><strong>Sampling</strong>：高 QPS 服務的 request log 做 sampling（在 application 端或 OTel Collector 層）</li>
<li><strong>BigQuery sink filter</strong>：只送需要長期分析的 log 到 BigQuery</li>
<li><strong>Cloud Storage sink</strong>：高 volume + 低查詢頻率的 log 送 GCS、按需用 BigQuery external table 查</li>
</ol>
<h2 id="整合與下一步">整合與下一步</h2>
<ul>
<li><a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>：overview 與日常操作</li>
<li><a href="../cloud-monitoring-mql/">Cloud Monitoring Metrics Model 與 MQL</a>：同 vendor 的 metrics 面</li>
<li><a href="/blog/backend/04-observability/audit-log-governance/" data-link-title="4.12 Audit Log 邊界與 PII 治理" data-link-desc="把稽核訊號從 operational log 拆出、按法規與不變性治理">4.12 Audit Log 邊界與 PII 治理</a>：跨 vendor 的 audit log 治理策略</li>
<li><a href="/blog/backend/04-observability/cases/fintech-audit-evidence-observability/" data-link-title="FinTech：審計證據鏈的可觀測性設計" data-link-desc="把交易與存取事件轉成可回查證據，降低合規審核與事故判讀落差。">4.C1 Fintech audit evidence</a>：審計證據鏈的案例回寫</li>
<li><a href="/blog/backend/04-observability/cases/healthcare-access-traceability-and-retention/" data-link-title="Healthcare：存取可追溯性與保留邊界" data-link-desc="在資料主權限制下，建立可追溯存取證據與分層保留策略。">4.C3 Healthcare retention</a>：長期保留的合規設計</li>
<li><a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">07 security 模組</a>：data access audit log 的安全面</li>
</ul>
]]></content:encoded></item></channel></rss>