回報記錄

本頁面說明如何透過服務基礎架構,讓代管服務傳送記錄檔至服務供應商服務用戶專案。

Service Control API 接受結構化或非結構化記錄資料,並將這些資料分批轉送至 Cloud Logging。您和受管理服務的使用者都可以使用Google Cloud 控制台和 Google API 控制台查看記錄資料,或使用 Cloud Logging API 以程式輔助方式存取資料。

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

設定記錄檔

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

  • 記錄名稱:您可以擁有一個或多個不同記錄檔,並藉由不重複的名稱識別各個記錄檔。採用多個記錄檔的做法如果可讓資料更容易視覺化或使用,就能協助您區隔不同類型的資料。

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

  • 記錄設定:可指定與任何記錄串流相關聯的受監控資源,以及每個記錄應傳送至的服務供應商或服務用戶專案。如要設定記錄關聯,請參閱logging.proto 的技術規格。

下列服務設定範例會將 activity_log 記錄檔設為傳送至服務用戶。

# 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 log name to be used.
logs:
- name: activity_log

# The logging configuration.
logging:
  consumer_destinations:
  - monitored_resource: library.appspot.com/Branch
    logs:
    - activity_log

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

回報記錄

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

gcurl -d "{
  'operations': [ {
    'operationId': 'e8bf36ef-f9b5-4274-b4f9-079a3731e6e5',
    'operationName': 'New book arrived',
    'consumerId': 'projects/library-consumer',
    'startTime': '`date +%FT%T%:z`',
    'endTime': '`date +%FT%T%:z`',
    'labels': {
      'location': 'us-east1',
      'branch_id': 'my-test-library-branch'
    },
    'logEntries': [ {
      'severity': 'INFO',
      'textPayload': 'new book arrived',
      'name': 'activity_log'
    } ]
  } ]
}" https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:report