This guide describes how to configure filters when you use the Monitoring API. You use filters to specify monitored resources, metric types, and time series.
Before you begin
If you aren't familiar with metrics, time series, and monitored resources, see Metrics, time series, and resources.
If you aren't familiar with labels, see Labels for an introduction.
Using filters
You can use filters in the Monitoring API to do the following:
- Select the specific time series data that is
returned from a
list
API request. The filter can select time series based on the data's monitored resource properties, and metric properties. For more information and examples, see Retrieving time series data.
List particular metric types. For more information and examples, see Listing metric descriptors.
List particular monitored resource types. For more information and examples, see Listing monitored resource descriptors.
Filter selectors
A filter consists of at least one selector, which is a filter keyword. The following examples illustrate the different selectors:
-
resource
: Matches monitored resources of a particular type or having particular label values.-
The following filter matches all monitored resources that are Compute Engine virtual machine (VM) instances:
resource.type = "gce_instance"
-
-
metric
: Matches a particular metric type or time series with with a particular label that matches a specific value.-
The following filter matches a specific metric type:
metric.type = "compute.googleapis.com/instance/cpu/usage_time"
-
The following filter matches time series with a label named
instance_name
, whose value starts withgke-hipster
orgke-nginx
:metric.labels.instance_name = monitoring.regex.full_match("gke-(hipster|nginx).*")
-
The following table shows which selectors are permitted in filters based on the Monitoring API call:
Filter purpose | resource selector |
metric selector |
---|---|---|
List time series | yes | yes * |
List metric descriptors | yes | |
List monitored resource descriptors | yes |
The following sections show examples of typical uses of monitoring filters. See Filter syntax for a complete discussion of the available filter objects and operators.
Retrieving time series data
Method:
projects.timeSeries.list
Filter objects:
project
, resource.type
, resource.labels.[KEY]
, metric.type
,
metric.labels.[KEY]
A time series is a list of time-stamped data points of a metric type from a specific monitored resource. For details, see The metric model. The metric type is specified by a metric descriptor, and the monitored resource is specified by a monitored-resource descriptor.
The filter specified to the timeSeries.list
method must include a
metric
selector, and that selector must specify exactly one metric type:
- To return all time series for a particular metric type:
metric.type = "compute.googleapis.com/instance/cpu/usage_time"
To return all time series from a specific Compute Engine instance, use the following filter:
metric.type = "compute.googleapis.com/instance/cpu/usage_time" AND metric.labels.instance_name = "my-instance-name"
Return all time series from Compute Engine instances whose names start with
frontend-
, use the following filter:metric.type = "compute.googleapis.com/instance/cpu/usage_time" AND metric.labels.instance_name = starts_with("frontend-")
Return all time series from Compute Engine instances whose names start with
gke-hipster
orgke-nginx
, use the following filter:metric.type = "compute.googleapis.com/instance/cpu/usage_time" AND metric.labels.instance_name = monitoring.regex.full_match("^gke-(hipster|nginx).*")
Listing metric descriptors
Method:
projects.metricDescriptors.list
Filter objects: project
, metric.type
Use a filter to limit which metric descriptors you retrieve.
For example, to return only the Compute Engine metric descriptors, use the following filter:
metric.type = starts_with("compute.googleapis.com")
See Metrics list for a complete list of the available metric types. For an overview of how metrics are named, see Metric naming conventions.
Listing monitored resource descriptors
Method:
projects.monitoredResourceDescriptors.list
Filter objects: resource.type
Use a filter to limit which monitored resource descriptors you retrieve.
For example, to retrieve only the Pub/Sub monitored resource descriptors, use the following filter:
resource.type = starts_with("pubsub")
See Monitored resource list for a complete list of the monitored resource types defined by Monitoring.
Examples
In the filtering examples, we use the following metric descriptor, monitored resource descriptor, and virtual machine instance, simplified for illustration:
# Metric descriptor: { "name": "projects/my-project-id/metricDescriptors/compute.googleapis.com%2Finstance%2Fdisk%2Fread_bytes_count" "type": "compute.googleapis.com/instance/disk/read_bytes_count", "labels": [ { "key": "device_name", "description": "The name of the disk device." } ] } # Monitored resource descriptor: { "name": "monitoredResourceDescriptors/gce_instance" "type": "gce_instance", "labels": [ { "key": "instance_id", "description": "The instance ID provide by Google Compute Engine." }, { "key": "zone", "description": "The Google Cloud Platform zone hosting the instance." } ] } # Resource descriptor for a virtual machine instance. { "type": "gce_instance", "instance_id": "1472038649266883453", "zone": "us-east-1b", "disks": [ "log_partition" ], "machine_type": "n1-standard-2", "tags": { "environment": "bleeding-edge", "role": "frobulator" }, "project_id": "my-project-id" }
Metric retrieval examples
To request the disk-read bandwidth usage for all instances and all devices, define a filter as follows. This filter returns, for each instance, a separate time series reporting the read bandwidth for each device:
metric.type = "compute.googleapis.com/instance/disk/read_bytes_count"
To refine the request to query for the read bandwidth for only the disk device known as "log_partition" on each instance, define the filter as follows. This filter returns, for each instance, at most one time series, depending on whether a device of that name exists on that instance:
metric.type = "compute.googleapis.com/instance/disk/read_bytes_count" AND metric.labels.device_name = "log_partition"
To restrict the request to a single instance, specify that instance:
resource.type = "gce_instance" AND resource.labels.instance_id = "1472038649266883453" AND metric.type = "compute.googleapis.com/instance/disk/read_bytes_count" AND metric.labels.device_name = "log_partition"
Reference: filter syntax
For an overview of filters with examples, see Using filters.
A monitoring filter is a string consisting of up to two types of selectors:
<monitoring_filter> ::= <resource_selector> AND <metric_selector>
The filter matches an item if all of the included selectors match the item.
As described in the following sections, some selectors can have multiple
comparisons joined by AND
or OR
. The order of the selectors in the
filter doesn't matter, but comparisons for different selectors must not be
intermingled.
Depending on the filter's purpose, certain selectors might be required, optional, or prohibited. For example, the filter used to list time series must contain a metric selector.
Comparisons
Filters and their selectors are built from comparisons. Each comparison has the following form:
-
[OBJECT]: selects a value to be tested; one of the following:
metric.type metric.labels.[KEY] resource.type resource.labels.[KEY]
[KEY] is a name, such as
zone
orinstance_id
.[KEYSTRING] can be name, but if it contains special characters, then it must be quoted with quotation marks (
"
). -
[OPERATOR]: a comparison operator; one of the following:
= # equality (case-sensitive) > < >= <= # numeric ordering != # not equal : # "has" substring match and test for key (case-sensitive)
-
[VALUE]: a literal value or a built-in function call; one of the following:
<string> # "a Unicode string". Don't use apostrophes (`'`) to quote strings. <bool> # true or false <number> # 0, -2, 123456, 3.14156 <function> # operators on the right side of '=' or '!=': # starts_with(<string>) # ends_with(<string>) # has_substring(<string> [, ignore_case=false]) # one_of(<string>,...,<string>) for up to 100 strings # monitoring.regex.full_match(<RE2-string>)
Except when used in the
timeSeries.list
method, thehas_substring
filter takes an optional second argument, which specifies whether the match ignores case or not. The default value isfalse
, so the default match is case-sensitive:- Case-sensitive:
display_name=has_substring("Demo")
- Case-sensitive:
display_name=has_substring("Demo", false)
- Case-insensitive:
display_name=has_substring("Demo", true)
When used in the
timeSeries.list
method, only thehas_substring(<string>)
form is supported.The
monitoring.regex.full_match
filter takes a regular-expression string in RE2 syntax. - Case-sensitive:
You can use the following operators to group or modify comparisons. OR
has
higher precedence than AND
. The operators must be written in upper case:
(...) # grouping comparisons AND # conjunction (optional but recommended) OR # disjunction
The AND
operator can be omitted between operators, but it is clearer and
less error-prone to include it.
The comparison x = one_of("a", "b", "c")
is equivalent to the following:
(x = "a" OR x = "b" OR x = "c")
Filter selectors
Use selectors to limit the filter selections to certain items.
In the following sections, braces are used to show repetition. For example,
the notation <x> {OR <y>}
means that you can write any of the
following:
<x> <x> OR <y> <x> OR <y> OR <y> <x> OR <y> OR <y> OR <y> ...
Resource selector
A resource selector limits the filter selection to resources—or items associated with resources—that have a specific resource type or label values:
<resource_selector> ::= <resource_type_expression> | <resource_label_expression> | <resource_type_expression> AND <resource_label_expression> <resource_type_expression> ::= resource.type '=' <string> | resource.type ':' <string> | resource.type '=' starts_with '(' <string>')' | resource.type '=' ends_with '(' <string> ')' <r_label_comparison> ::= resource.labels.[KEY] '=' (<string> | <bool>) | resource.labels.[KEY] ':' <string> | resource.labels.[KEY] '=' (starts_with | ends_with) '(' <string> ')' | resource.labels.[KEY] ('=' | '>' | '<' | '>=' | '<=') <number> <resource_label_expression> ::= <r_label_comparison> {AND <r_label_comparison>} | <r_label_comparison> {OR <r_label_comparison>}
If you use more than one <r_label_comparison>
in your selector, then
enclose them all in parentheses for better readability.
Metric selector
A metric selector specifies certain metrics or metric descriptors by limiting
the metric type and metric labels. When used with the
projects.timeSeries.list
method,
the metric selector must specify a single metric type:
<metric_selector> ::= <metric_name_expression> [AND <metric_label_expression>] <metric_name_expression> ::= metric.type '=' <string> | metric.type ':' <string> | metric.type '=' starts_with '(' <string> ')' | metric.type '=' ends_with '(' <string> ')' <metric_label_comparison> ::= metric.labels.[KEY] '=' <string> | <bool> | metric.labels.[KEY] ':' <string> | metric.labels.[KEY] '=' starts_with '(' <string> ')' | metric.labels.[KEY] '=' ends_with '(' <string> ')' | metric.labels.[KEY] ('=' | '>' | '<' | '>=' | '<=') <number> <metric_label_expression> ::= <metric_label_comparison> {[AND] <metric_label_comparison>} | <metric_label_comparison> {OR <metric_label_comparison>}
For example, the following filter could be used to retrieve a time series for a specific database instance:
metric.type = "cloudsql.googleapis.com/database/state" AND (metric.labels.resource_type = "instance" AND metric.labels.resource_id = "abc-123456")