如要從檢測應用程式收集 OTLP 記錄、指標和追蹤記錄,並將資料匯出至 Google Cloud,您可以在 Cloud Run 上,以邊車容器的形式執行 Google 建構的 OpenTelemetry Collector。
事前準備
如要執行 Google 建構的 OpenTelemetry 收集器,必須具備下列資源:
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- Cloud Run 服務。如果您沒有 Cloud Run 服務,請按照「規劃及準備服務」一文中的操作說明進行。
- 安裝
gcloud。如要瞭解如何安裝gcloud,請參閱「安裝gcloudCLI」。
設定收集器的權限
根據預設,Cloud Run 工作和服務會使用 Compute Engine 預設服務帳戶 (PROJECT_NUMBER-compute@)。這個服務帳戶通常具備必要的 Identity and Access Management (IAM) 角色,可寫入本文所述的指標和記錄:
請管理員授予您專案的下列 IAM 角色:
- Cloud Run 管理員 (
roles/run.admin) Secret Manager Secret Accessor(roles/secretmanager.secretAccessor)
您也可以為 Cloud Run 設定使用者自行管理的服務帳戶。使用者管理的服務帳戶也必須具備這些角色。如要進一步瞭解 Cloud Run 的服務帳戶,請參閱「服務身分簡介」。
部署收集器
如要將 Google 建構的 OpenTelemetry Collector 安裝為 Cloud Run 的 sidecar,請先建立 Secret 來儲存收集器的設定。
gcloud secrets create SECRET_NAME --data-file=config.yaml --project=PROJECT_ID
接著,將 Google 建構的 OpenTelemetry Collector 新增為 service.yaml 的 Sidecar:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
annotations:
run.googleapis.com/launch-stage: ALPHA
name: google-otel-cloud-run-sample
spec:
template:
metadata:
annotations:
# [REQUIRED] set the collector as a parent to the app
run.googleapis.com/container-dependencies: "{app:[collector]}"
run.googleapis.com/secrets: 'SECRET_NAME:projects/PROJECT_ID/secrets/SECRET_NAME'
spec:
containers:
- image: my-app
name: app
ports:
- containerPort: 8080
env:
- name: "OTEL_EXPORTER_OTLP_ENDPOINT"
value: "http://localhost:4317"
- image: "us-docker.pkg.dev/cloud-ops-agents-artifacts/google-cloud-opentelemetry-collector/otelcol-google:0.165.2-r.1"
args:
- --config=/etc/otelcol-google/config.yaml
name: collector
startupProbe:
httpGet:
path: /
port: 13133
timeoutSeconds: 30
periodSeconds: 30
livenessProbe:
httpGet:
path: /
port: 13133
timeoutSeconds: 30
periodSeconds: 30
volumeMounts:
- mountPath: /etc/otelcol-google/
name: config
volumes:
- name: config
secret:
items:
- key: latest
path: config.yaml
secretName: 'SECRET_NAME'
設定收集器
我們提供 OpenTelemetry Collector 設定,可與 Google 建構的 Collector 搭配使用。這項設定旨在提供大量 OTLP 指標、記錄和追蹤記錄。這項設定也能避免常見的攝入問題。您可以新增設定,但強烈建議不要移除元素。
本節說明提供的設定、匯出工具、處理器、接收器等重要元件,以及其他可用元件。
提供的收集器設定
您可以在opentelemetry-operations-collector 存放區的 google-built-opentelemetry-collector 目錄中找到收集器設定:
出口商
收集器設定會使用下列項目:
- OTLP gRPC 匯出工具,用於將記錄、指標和追蹤記錄傳送至 Telemetry (OTLP) API。
- 用於 API 驗證的
googleclientauthextension。
處理器
收集器設定包含下列處理器:
batch:設定為批次處理遙測要求,每項要求最多 Google Cloud 個項目,或每 5 秒 Google Cloud 最短間隔 (以先到者為準)。memory_limiter:限制收集器的記憶體用量,在超過限制時捨棄資料點,避免記憶體不足而當機。resourcedetection:自動偵測 Google Cloud 資源標籤,例如project_id。
接收器
收集器設定僅包含 otlp 接收器。如要瞭解如何檢測應用程式,將 OTLP 追蹤記錄和指標傳送至收集器的 OTLP 端點,請參閱「選擇檢測方法」。
可用元件
Google 建構的 OpenTelemetry Collector 包含大多數使用者所需的元件,可在 Google Cloud Observability 中提供豐富體驗。如需可用元件的完整清單,請參閱 opentelemetry-operations-collector 存放區中的元件。
如要要求變更或新增可用元件,請在 opentelemetry-operations-collector 存放區中提出功能要求。
生成遙測資料
如要產生遙測資料,請建立含有側車收集器的 Cloud Run 應用程式。使用 OpenTelemetry sidecar 寫入 OTLP 指標一文提供教學課程,說明如何使用 Google 建構的 OpenTelemetry Collector 做為 sidecar。您可以透過本教學課程,使用 Google 建構的收集器產生遙測資料。
查看遙測資料
Google 建構的 OpenTelemetry Collector 會將檢測應用程式的指標、記錄和追蹤記錄傳送至 Google Cloud Observability。收集器也會傳送自我觀測能力指標。以下各節說明如何查看這項遙測資料。
查看指標
Google 建構的 OpenTelemetry Collector 會收集 Prometheus 指標,您可以使用 Metrics Explorer 查看這些指標。收集的指標取決於應用程式的檢測,但 Google 建構的收集器也會寫入一些自我指標。
如要查看 Google 建構的 OpenTelemetry Collector 收集的指標,請按照下列步驟操作:-
前往 Google Cloud 控制台的leaderboard「Metrics Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
- 在 Google Cloud 控制台的工具列中,選取 Google Cloud 專案。如果是 App Hub 設定,請選取 App Hub 主專案或已啟用應用程式管理功能的資料夾管理專案。
- 在「Metric」(指標) 元素中,展開「Select a metric」(選取指標) 選單,在篩選列中輸入
Prometheus Target,然後使用子選單選取特定資源類型和指標:- 在「Active resources」(有效資源) 選單中,選取「Prometheus Target」(Prometheus 目標)。
- 如要選取指標,請使用「使用中的指標類別」和「使用中的指標」選單。
Google 建構的 OpenTelemetry Collector 收集的指標,前置字串為
prometheus.googleapis.com。 - 按一下「套用」。
如要新增篩選器,從查詢結果中移除時間序列,請使用「Filter」(篩選器) 元素。
- 設定資料的查看方式。
如果指標的測量結果是累計值,Metrics Explorer 會自動依據校正週期正規化測量資料,圖表則會顯示比率。詳情請參閱「種類、類型和轉換」。
測量整數或雙精度值時 (例如使用
counter指標),Metrics Explorer 會自動加總所有時間序列。如要變更這項行為,請將「Aggregation」(匯總) 項目中的第一個選單設為「None」(無)。如要進一步瞭解如何設定圖表,請參閱「在使用 Metrics Explorer 時選取指標」。
查看追蹤記錄
如要查看追蹤記錄資料,請按照下列步驟操作:
-
前往 Google Cloud 控制台的
「Trace Explorer」頁面:前往「Trace explorer」(Trace 探索工具)
您也可以透過搜尋列找到這個頁面。
- 在 Google Cloud 控制台的工具列中,選取 Google Cloud 專案。如要設定 App Hub,請選取 App Hub 主專案或管理專案。
- 在頁面的表格部分選取一列。
在「Trace details」(追蹤記錄詳細資料) 面板的甘特圖中,選取所需時距。
畫面上會開啟一個面板,顯示追蹤要求的相關資訊。這些詳細資料包括方法、狀態碼、位元組數,以及呼叫端的使用者代理程式。
如要查看與這個追蹤記錄相關聯的記錄檔,請選取「Logs & Events」(記錄檔和事件) 分頁標籤。
這個分頁會顯示個別記錄。如要查看記錄項目的詳細資料,請展開記錄項目。您也可以點選「View Logs」(查看記錄),並使用 Logs Explorer 查看記錄。
如要進一步瞭解如何使用 Cloud Trace Explorer,請參閱「尋找及探索追蹤記錄」。
查看記錄檔
您可以在 Logs Explorer 中檢查記錄,也可以查看相關聯的追蹤記錄 (如有)。
-
前往 Google Cloud 控制台的 「Logs Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。
找出檢測應用程式的記錄項目,然後展開該項目查看詳細資料。
在含有追蹤訊息的記錄項目上,按一下
「Traces」(追蹤記錄),然後選取「View trace details」(查看追蹤記錄詳細資料)。「Trace details」(追蹤記錄詳細資料) 面板隨即開啟,並顯示所選追蹤記錄。
如要進一步瞭解如何使用 Logs Explorer,請參閱「使用 Logs Explorer 查看記錄檔」。
觀察收集器並偵錯
Google 建構的 OpenTelemetry Collector 會自動提供自我觀測能力指標,協助您監控其效能,並確保 OTLP 擷取管道持續正常運作。
如要監控收集器,請安裝收集器的範例資訊主頁。這個資訊主頁可讓您一目瞭然地查看 Collector 的多項指標,包括運作時間、記憶體用量,以及對 Google Cloud Observability 的 API 呼叫。
如要安裝資訊主頁,請按照下列步驟操作:
-
在 Google Cloud 控制台中,前往「Dashboards」(資訊主頁) 頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
- 按一下「資訊主頁範本」。
- 搜尋「OpenTelemetry Collector」資訊主頁。
- 選用:如要預覽資訊主頁,請選取該資訊主頁。
按一下「playlist_add Add dashboard to your list」(將資訊主頁新增至清單),然後完成對話方塊。
您可以在對話方塊中選取資訊主頁名稱,並為資訊主頁新增標籤。
如要進一步瞭解如何安裝資訊主頁,請參閱「安裝資訊主頁範本」。