回報監控指標

本頁面說明如何透過服務基礎架構,讓代管服務產生監控資料,進而將資料傳送至服務供應商服務用戶專案。Stackdriver Monitoring 可以讓您監控個別代管服務的效能、運作時間與整體健康狀態。

Service Control API 會接收指標,並將指標分批轉送至 Cloud Monitoring。Monitoring 會擷取這些指標,並透過資訊主頁、圖表和快訊產生深入分析。

雖然 Monitoring 本身就可以允許代管服務產生監控記錄以供該服務的開發人員使用,不過 Service Control API 也能讓您產生適合提供給使用者的監控資料,這種做法可協助使用者瞭解及診斷代管服務的使用情形。

請注意,本指南適用於監控代管服務定義的指標。無法透過 Service Control API 回報監控自訂指標。如要回報自訂指標,請按照 Monitoring 指南操作。

設定監控功能

將監控資料傳送至 Service Control API 之前,您必須先在服務設定中指定三種不同的資訊:

  • 受控資源受控資源是產生部分監控資料的雲端實體。如要在服務設定中定義受監控資源,請參閱 monitored_resource.proto 的技術規格。強烈建議您使用相同的受監控資源進行監控和記錄

  • 指標:您可以回報一或多個指標,每個指標都代表整體狀態的一部分,您可以追蹤狀態值隨著時間的變化。如要瞭解定義指標的技術規格,請參閱 metric.proto

  • 監控設定:可指定與個別受監控資源相關聯的指標,以及每個指標應傳送至的服務供應商或服務使用者專案。詳情請參閱 monitoring.proto

下列服務設定範例定義了受監控的資源和指標。

# The definition of the monitored resource to be used.
monitored_resources:
- type: library.appspot.com/Branch
  description: A library branch.
  display_name: Library Branch
  launch_stage: ALPHA
  labels:
  - key: resource_container
    description: The Google Cloud resource container (ie. project id) for
    the branch.
  - key: location
    description: The Google Cloud region the branch is located.
  - key: branch_id
    description: The ID of the branch.

# The definition of the metrics to be used.
metrics:
- name: library.appspot.com/book/num_overdue
  display_name: Books Overdue
  description: The current number of overdue books.
  launch_stage: ALPHA
  metric_kind: GAUGE
  value_type: INT64
  unit: 1
- name: library.googleapis.com/book/returned_count
  display_name: Books Returned
  description: The count of books that have been returned.
  launch_stage: ALPHA
  metric_kind: DELTA
  value_type: INT64
  unit: 1

# The monitoring configuration.
monitoring:
  producer_destinations:
  - monitored_resource: library.appspot.com/Branch
    metrics:
    - library.googleapis.com/book/returned_count
  consumer_destinations:
  - monitored_resource: library.appspot.com/Branch
    metrics:
    - library.appspot.com/book/num_overdue

撰寫這類設定後,您需要按照「管理服務設定」和「管理服務推行功能」的說明,將服務設定推送至 Service Management API,以便在 Cloud Monitoring 中設定受監控的資源和指標定義。

報表指標

服務設定推送完成後,您就可以呼叫 Service Control API 來回報指標。下列範例使用 gcurl 指令示範呼叫。如要瞭解相關設定方式,請參閱開始使用 Service Control API

gcurl -d "{
  'operations': [ {
    'operationId': '8356d3c5-f9b5-4274-b4f9-079a3731e6e5',
    'operationName': 'Return books',
    'consumerId': 'projects/library-consumer',
    'startTime': '`date +%FT%T%:z`',
    'endTime': '`date +%FT%T%:z --date="5 seconds"`',
    'labels': {
      'location': 'us-east1',
      'branch_id': 'my-test-library-branch'
    },
    'metricValueSets': [ {
      'metricName': 'library.googleapis.com/book/returned_count',
      'metricValues': [ { 'int64Value': '23' } ]
    } ]
  } ]
}" https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:report