Reporting Billing Metrics

This page describes how to use Google Service Control to enable services to send billing usage data so that Cloud Billing can charge against service consumers. For example, you can define a metric called library.appspot.com/books/borrowed to measure the number of books borrowed by a customer and define a SKU and price for it.

Service Control accepts metrics, which it batches, aggregates and forwards to Cloud Billing. Cloud Billing ingests these metrics and generates bills based on the predefined price model. If there is no pre-defined price model, Cloud Billing simply disregards the billing metrics. To define the price model, please contact your GCP partner account manager.

Configuring billing

Before you send billing data to Service Control, you need to specify three different bits of information in your service configuration:

  • Monitored resources: You can collect billing usage for each resource. The monitored resources need at least one label key cloud.googleapis.com/location to indicate the location of the billing usage.

  • Metrics: You can report one or more metrics. Each metric measures the usage of a given type of resource. Only DELTA metrics with INT64 values are supported for billing.

  • Billing configuration: Lets you specify the metrics to be sent to Cloud Billing.

In the following example of service configuration, we defined one metric measuring the number of books borrowed by a customer. A client can invoke the services.report method to report values on the metric. See billing.proto for details on billing configuration.

# library
monitored_resources:
 - type: library.appspot.com/branch
   labels:
   - key: cloud.googleapis.com/location
     description: The cloud location.
   - key: library.appspot.com/city
     description: The city where the library branch is located in.

# The metrics to be used.
metrics:
- name: library.appspot.com/books/borrowed
  metric_kind: DELTA
  value_type: INT64
  unit: '1'

# The billing configuration.
billing:
  consumer_destinations:
  - monitored_resource: library.appspot.com/branch
    metrics:
    - library.appspot.com/books/borrowed

Reporting billing metrics

Once you have configured monitoring in the service configuration and pushed the configuration to Google Service Management, you can call the Service Control API to report metrics. The following example uses the gcurl command to demonstrate the call. See Getting Started for how to set this up.

$ gcurl -d "{
  'operations': [ {
    'operationId': '8356d3c5-f9b5-4274-b4f9-079a3731e6e5',
    'consumerId': 'project:library-consumer',
    'startTime': '`date +%FT%T%:z`',
    'endTime': '`date +%FT%T%:z`',
    'labels': {
      'cloud.googleapis.com/location': 'us-west1',
      'library.appspot.com/city': 'Kirkland',
    },
    'metricValueSets': [ {
      'metricName': 'library.appspot.com/books/borrowed',
      'metricValues': [ { 'int64Value': '10' } ]
    } ]
  } ]
}" https://servicecontrol.googleapis.com/v1/services/library.appspot.com:report