报告日志

本页面介绍如何使用 Service Infrastructure 启用托管式服务,以向服务提供方项目和服务使用方项目发送日志。

Service Control API 接受结构化或非结构化日志数据,然后将其批量处理并转发到 Cloud Logging。您和托管服务的用户都可以使用 Google Cloud 控制台和 Google API 控制台查看日志数据,或使用 Cloud Logging API 以编程方式访问数据。

Logging 单独可允许托管式服务生成日志,以供作为托管式服务开发者的您使用,而 Service Control API 亦允许您生成供用户使用的日志。这使您的用户可以了解和诊断托管式服务的使用情况。

配置日志

将日志发送到 Service Control API 之前,您需要在服务配置中指定三种不同的信息:

  • 日志名称:您可以拥有一个或多个不同的日志。每个日志都采用唯一名称进行标识。如果日志可以使数据更易于呈现或使用,则您可以使用各个日志来隔离不同类型的数据。

  • 受监控的资源受监控的资源表示生成某些日志记录数据的云实体。要在服务配置中定义受监控的资源,请参阅 monitored_resource.proto 了解相关技术规范。我们强烈建议您对 monitoring 和 Logging 使用同一受监控的资源。

  • 日志配置:允许您指定与任何日志流相关的受监控的资源,以及各项日志应发送到的服务提供方或服务使用方项目。 要配置日志关联,请参阅 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