Time Series Query#

Time series query for the Google Stackdriver Monitoring API (V3).

class google.cloud.monitoring.query.Aligner[source]#

Bases: object

Allowed values for the supported aligners.

class google.cloud.monitoring.query.Query(client, metric_type='compute.googleapis.com/instance/cpu/utilization', end_time=None, days=0, hours=0, minutes=0)[source]#

Bases: object

Query object for retrieving metric data.

The preferred way to construct a query object is using the query() method of the Client class.

Parameters:
  • client (google.cloud.monitoring.client.Client) – The client to use.
  • metric_type (str) – The metric type name. The default value is Query.DEFAULT_METRIC_TYPE, but please note that this default value is provided only for demonstration purposes and is subject to change. See the supported metrics.
  • end_time (datetime.datetime) –

    (Optional) The end time (inclusive) of the time interval for which results should be returned, as a datetime object. The default is the start of the current minute.

    The start time (exclusive) is determined by combining the values of days, hours, and minutes, and subtracting the resulting duration from the end time.

    It is also allowed to omit the end time and duration here, in which case select_interval() must be called before the query is executed.

  • days (int) – The number of days in the time interval.
  • hours (int) – The number of hours in the time interval.
  • minutes (int) – The number of minutes in the time interval.
Raises:

ValueError if end_time is specified but days, hours, and minutes are all zero. If you really want to specify a point in time, use select_interval().

align(per_series_aligner, seconds=0, minutes=0, hours=0)[source]#

Copy the query and add temporal alignment.

If per_series_aligner is not Aligner.ALIGN_NONE, each time series will contain data points only on the period boundaries.

Example:

query = query.align(Aligner.ALIGN_MEAN, minutes=5)

It is also possible to specify the aligner as a literal string:

query = query.align('ALIGN_MEAN', minutes=5)
Parameters:
  • per_series_aligner (str) – The approach to be used to align individual time series. For example: Aligner.ALIGN_MEAN. See Aligner and the descriptions of the supported aligners.
  • seconds (int) – The number of seconds in the alignment period.
  • minutes (int) – The number of minutes in the alignment period.
  • hours (int) – The number of hours in the alignment period.
Return type:

Query

Returns:

The new query object.

as_dataframe(label=None, labels=None)[source]#

Return all the selected time series as a pandas dataframe.

Note

Use of this method requires that you have pandas installed.

Examples:

# Generate a dataframe with a multi-level column header including
# the resource type and all available resource and metric labels.
# This can be useful for seeing what labels are available.
dataframe = query.as_dataframe()

# Generate a dataframe using a particular label for the column
# names.
dataframe = query.as_dataframe(label='instance_name')

# Generate a dataframe with a multi-level column header.
dataframe = query.as_dataframe(labels=['zone', 'instance_name'])

# Generate a dataframe with a multi-level column header, assuming
# the metric is issued by more than one type of resource.
dataframe = query.as_dataframe(
    labels=['resource_type', 'instance_id'])
Parameters:
  • label (str) – (Optional) The label name to use for the dataframe header. This can be the name of a resource label or metric label (e.g., "instance_name"), or the string "resource_type".
  • labels (list of strings, or None) – A list or tuple of label names to use for the dataframe header. If more than one label name is provided, the resulting dataframe will have a multi-level column header. Providing values for both label and labels is an error.
Return type:

pandas.DataFrame

Returns:

A dataframe where each column represents one time series.

copy()[source]#

Copy the query object.

Return type:Query
Returns:The new query object.
filter#

The filter string.

This is constructed from the metric type, the resource type, and selectors for the group ID, monitored projects, resource labels, and metric labels.

iter(headers_only=False, page_size=None)[source]#

Yield all time series objects selected by the query.

The generator returned iterates over TimeSeries objects containing points ordered from oldest to newest.

Note that the Query object itself is an iterable, such that the following are equivalent:

for timeseries in query:
    ...

for timeseries in query.iter():
    ...
Parameters:
  • headers_only (bool) – Whether to omit the point data from the time series objects.
  • page_size (int) – (Optional) Positive number specifying the maximum number of points to return per page. This can be used to control how far the iterator reads ahead.
Raises:

ValueError if the query time interval has not been specified.

metric_type#

The metric type name.

reduce(cross_series_reducer, *group_by_fields)[source]#

Copy the query and add cross-series reduction.

Cross-series reduction combines time series by aggregating their data points.

For example, you could request an aggregated time series for each combination of project and zone as follows:

query = query.reduce(Reducer.REDUCE_MEAN,
                     'resource.project_id', 'resource.zone')
Parameters:
  • cross_series_reducer (str) – The approach to be used to combine time series. For example: Reducer.REDUCE_MEAN. See Reducer and the descriptions of the supported reducers.
  • group_by_fields (strs) – Fields to be preserved by the reduction. For example, specifying just "resource.zone" will result in one time series per zone. The default is to aggregate all of the time series into just one.
Return type:

Query

Returns:

The new query object.

select_group(group_id)[source]#

Copy the query and add filtering by group.

Example:

query = query.select_group('1234567')
Parameters:group_id (str) – The ID of a group to filter by.
Return type:Query
Returns:The new query object.
select_interval(end_time, start_time=None)[source]#

Copy the query and set the query time interval.

Example:

import datetime

now = datetime.datetime.utcnow()
query = query.select_interval(
    end_time=now,
    start_time=now - datetime.timedelta(minutes=5))

As a convenience, you can alternatively specify the end time and an interval duration when you create the query initially.

Parameters:
  • end_time (datetime.datetime) – The end time (inclusive) of the time interval for which results should be returned, as a datetime object.
  • start_time (datetime.datetime) – (Optional) The start time (exclusive) of the time interval for which results should be returned, as a datetime object. If not specified, the interval is a point in time.
Return type:

Query

Returns:

The new query object.

select_metrics(*args, **kwargs)[source]#

Copy the query and add filtering by metric labels.

Examples:

query = query.select_metrics(instance_name='myinstance')
query = query.select_metrics(instance_name_prefix='mycluster-')

A keyword argument <label>=<value> ordinarily generates a filter expression of the form:

metric.label.<label> = "<value>"

However, by adding "_prefix" or "_suffix" to the keyword, you can specify a partial match.

<label>_prefix=<value> generates:

metric.label.<label> = starts_with("<value>")

<label>_suffix=<value> generates:

metric.label.<label> = ends_with("<value>")

If the label’s value type is INT64, a similar notation can be used to express inequalities:

<label>_less=<value> generates:

metric.label.<label> < <value>

<label>_lessequal=<value> generates:

metric.label.<label> <= <value>

<label>_greater=<value> generates:

metric.label.<label> > <value>

<label>_greaterequal=<value> generates:

metric.label.<label> >= <value>
Parameters:
  • args (tuple) – Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction.
  • kwargs (dict) – Label filters to include in the conjunction as described above.
Return type:

Query

Returns:

The new query object.

select_projects(*args)[source]#

Copy the query and add filtering by monitored projects.

This is only useful if the target project represents a Stackdriver account containing the specified monitored projects.

Examples:

query = query.select_projects('project-1')
query = query.select_projects('project-1', 'project-2')
Parameters:args (tuple) – Project IDs limiting the resources to be included in the query.
Return type:Query
Returns:The new query object.
select_resources(*args, **kwargs)[source]#

Copy the query and add filtering by resource labels.

Examples:

query = query.select_resources(zone='us-central1-a')
query = query.select_resources(zone_prefix='europe-')
query = query.select_resources(resource_type='gce_instance')

A keyword argument <label>=<value> ordinarily generates a filter expression of the form:

resource.label.<label> = "<value>"

However, by adding "_prefix" or "_suffix" to the keyword, you can specify a partial match.

<label>_prefix=<value> generates:

resource.label.<label> = starts_with("<value>")

<label>_suffix=<value> generates:

resource.label.<label> = ends_with("<value>")

As a special case, "resource_type" is treated as a special pseudo-label corresponding to the filter object resource.type. For example, resource_type=<value> generates:

resource.type = "<value>"

See the defined resource types.

Note

The label "instance_name" is a metric label, not a resource label. You would filter on it using select_metrics(instance_name=...).

Parameters:
  • args (tuple) – Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction.
  • kwargs (dict) – Label filters to include in the conjunction as described above.
Return type:

Query

Returns:

The new query object.

class google.cloud.monitoring.query.Reducer[source]#

Bases: object

Allowed values for the supported reducers.