Reporting Logs

This page describes how to use the Service Infrastructure to enable managed services to send logs to both service producer and service consumer projects.

The Service Control API accepts structured or unstructured log data, which it batches and forwards to Cloud Logging. Both you and users of your managed services can use Google Cloud console and Google API Console to view the log data, or use the Cloud Logging API to access the data programmatically.

Whereas Logging alone allows a managed service to generate logs for use by you as the developer of the managed service, the Service Control API also lets you generate logs that are intended for your users. This makes it possible for your users to understand and diagnose their use of your managed service.

Configuring logs

Before you send logs to the Service Control API, you need to specify three different bits of information in your service configuration:

  • Log names: You can have one or more distinct logs. Each log is identified by a unique name. Individual logs make it possible for you to isolate distinct types of data if it makes the data easier to visualize or use.

  • Monitored resources: A monitored resource represents a cloud entity that produces some logging data. To define a monitored resource in the service configuration, see monitored_resource.proto for the technical specification. We strongly recommend you to use the same monitored resource for both monitoring and logging.

  • Log configuration: Lets you specify the monitored resource associated with any log stream and the service producer or service consumer project where each log should be sent. To configure the log association, see logging.proto for the technical specification.

In the following example of service configuration, the log activity_log is configured to be sent to the service consumer.

# The definition of the monitored resource to be used.
- type:
  description: A library branch
  display_name: Library Branch
  launch_stage: ALPHA
  - 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.
- name: activity_log

# The logging configuration.
  - monitored_resource:
    - activity_log

After you have authored such configuration, you will need to follow Managing Service Configurations and Managing Service Rollouts to push the service configuration to the Service Management API, which will configure the monitored resource in Cloud Logging.

Reporting logs

After the service configuration push finishes, you can call the Service Control API to report log entries. The following example uses the gcurl command to demonstrate the call. To learn how to set this up, see Getting Started with the 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'
    } ]
  } ]