> [!WARNING]
>
> **Preview**
>
>
> This product or feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section
> of the [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA products and features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

Analytics views in Observability Analytics let you define a SQL query with a custom
schema over your observability data so that you can simplify and reuse complex
transformations. Whereas you can only rerun a saved query, you can reference an
analytics view as a data source in the `FROM` clause of other SQL queries.

## Overview

An *analytics view* is a resource you can query. That is, after you
define an analytics view, you can write queries where you specify your
analytics view in the `FROM` clause.

The primary benefit of using analytics views is that you aren't constrained to
querying the data in the format an API or system defines. An analytics view
lets you define a schema, which lets you simplify your queries or reduce the
time you spend writing queries. The query in an analytics view can do things
like retain only specific fields, rename fields, move nested fields to a
top-level column, and perform grouping and aggregation.

Analytics views aren't
[materialized views](https://docs.cloud.google.com/bigquery/docs/materialized-views-intro.md.txt).
An analytics view isn't a precomputed view that periodically caches
query results. Therefore, querying an analytics view is equivalent to querying
the views that are listed in the analytics view's query.

Analytics views are more powerful than saved queries, which are queries that
you run on the **Observability Analytics** page and then save. You can rerun a saved query,
but you can't query the results of a saved query.

### Types of analytics views

There are two types of analytics views: user-defined and system-defined:

- *User-defined analytics views* are any analytics views that you create.
  You can query, edit, and delete user-defined analytics views.

- *System-defined analytics views* are analytics views created by
  Google Cloud services. You can query system-defined analytics views.
  However, you can't edit or delete them.

For information about listing the analytics views in your
Google Cloud project, see [List analytics views](https://docs.cloud.google.com/stackdriver/docs/observability/create-and-manage-analytics-views.md.txt#list).

### Location of analytics views

The location of an analytics view is determined by the location of the
resources it queries. For example, if an analytics view queries a
log view or observability view that is in the `global` location, then the location of
the analytics view must also be `global`. When you use the Google Cloud console
to create an analytics view, the location is set automatically.

## Example use cases

The following examples illustrate how you can use analytics views to
simplify and reuse queries.

### Query for network details

You want to analyze network performance by using VPC Flow Logs. You
need to analyze the overall networking performance and identify
specific networks, IP addresses, and hosts. This information is stored within
nested fields in the `json_payload` field of a log entry.

To simplify your analysis queries, you create the following analytics view
and name it `network_details`:

```sql
SELECT
  -- Create a column for each network field.
  JSON_VALUE(resource.labels.subnetwork_name) subnetwork_name,
  JSON_VALUE(json_payload.src_instance.vm_name) vm_name,
  JSON_VALUE(json_payload.connection.src_ip) as src_ip,
  JSON_VALUE(json_payload.connection.src_port) as src_port,
  JSON_VALUE(json_payload.connection.dest_ip) as dest_ip,
  JSON_VALUE(json_payload.connection.dest_port) as dest_port,
  CAST(JSON_VALUE(json_payload.bytes_sent) as INT64) as bytes_sent,
  CAST(JSON_VALUE(json_payload.packets_sent) as INT64) as packets_sent
FROM
  -- Query one log view.
  `PROJECT_ID.LOCATION.BUCKET_ID.LOG_VIEW_ID`
WHERE
  -- Only query the VPC Flow Logs log.
  log_id = "compute.googleapis.com/vpc_flows"
  AND SEARCH(json_payload.reporter, "SRC")
```

Before you use the query, set the values of the following variables:

- <var translate="no">PROJECT_ID</var>: The identifier of the project.
- <var translate="no">LOCATION</var>: The location of the log view.
- <var translate="no">BUCKET_ID</var>: The name or ID of the log bucket.
- <var translate="no">LOG_VIEW_ID</var>: The identifier of the log view, which is limited to 100 characters and can include only letters, digits, underscores, and hyphens.

When you want to analyze the network performance, you query your
analytics view. For example, if you are only interested in the name of the
instance and the amount of data sent, then you might write the following query:

```sql
SELECT vm_name, bytes_sent, packets_sent,
FROM `analytics_view.my_project.global.network_details`
ORDER BY bytes_sent DESC
LIMIT 100
```

### Base query for API latency analysis

Several teams need weekly performance data to complete their analysis. Instead
of each team generating the weekly data, you create an analytics view that
extracts the weekly data. Other teams can query the analytics view and then
perform their analysis.

The following query illustrates how to use SQL to compute the minimum, maximum,
and average request latency:

```sql
SELECT week, MIN(took_ms) as min, MAX(took_ms) AS max, AVG(took_ms) AS avg
FROM (
  SELECT TIMESTAMP_TRUNC(timestamp, WEEK) AS week,
  CAST( JSON_VALUE(json_payload, '$."http.resp.took_ms"') AS INT64) as took_ms
  FROM
    -- Query one log view.
    `PROJECT_ID.LOCATION.BUCKET_ID.LOG_VIEW_ID`
  WHERE json_payload IS NOT NULL
    AND SEARCH(labels,"frontend")
    AND JSON_VALUE(json_payload.message) = "request complete"
  ORDER BY took_ms DESC, timestamp ASC
)
GROUP BY week ORDER BY week
```

### Extract trace service name

Suppose that you want to write queries that analyze your trace data, and you
want to either display or group by a synthesized service name. The query for
your analytics view determines the service name for a span by using the
`COALESCE` function, which evaluates a list of expressions, in order, and
returns the value for the first expression that doesn't evaluate to `NULL`:

```sql
SELECT
  start_time,
  -- Set the value of service name based on the first non-null value in the list.
  COALESCE(
    JSON_VALUE(resource.attributes, '$."service.name"'),
    JSON_VALUE(attributes, '$."service.name"'),
    JSON_VALUE(attributes, '$."g.co/gae/app/module"')) AS service_name,
  name AS span_name,
  duration_nano,
  status.code AS status,
  trace_id,
  span_id
FROM
  `PROJECT_ID.us._Trace.Spans._AllSpans`
LIMIT 10
```

Before you use the query, replace <var translate="no">PROJECT_ID</var> with the ID of your
Google Cloud project.

### Join trace and log data by using the trace ID

When you analyze an application, you might want to correlate your log and
trace data. One option is to create an analytics view that performs the
join and reports select fields.

The following query joins log and trace data by using the span and trace IDs:

```sql
SELECT
  T.trace_id,
  T.span_id,
  T.name,
  T.start_time,
  T.duration_nano,
  L.log_name,
  L.severity,
  L.json_payload,
  L.text_payload,
  L.proto_payload,
  L.insert_id
FROM
  `PROJECT_ID.us._Trace.Spans._AllSpans` AS T
JOIN
  `PROJECT_ID.us.BUCKET_ID.LOG_VIEW_ID` AS L
ON
  -- Join log and trace data by both the span ID and trace ID.
  -- Don't join only on span ID because this field isn't globally unique.
  T.span_id = L.span_id
  -- A regular expression is required because the storage format of the trace ID
  -- differs between a log view and a trace view.
  AND T.trace_id = REGEXP_EXTRACT(L.trace, r'/([^/]+)$')
```

Before you use the query, set the values of the following variables:

- <var translate="no">PROJECT_ID</var>: The identifier of the project.
- <var translate="no">BUCKET_ID</var>: The name or ID of the log bucket.
- <var translate="no">LOG_VIEW_ID</var>: The identifier of the log view, which is limited to 100 characters and can include only letters, digits, underscores, and hyphens.

In the example, notice that both the log view and the observability view are
in the same location, `us`. An analytics view can query multiple views, but
those views must be in the same location.

## Limitations

The following limitations apply to analytics views:

- An analytics view can only query log views and observability views.
- The parent resource of an analytics view must be a Google Cloud project.
- You can only query an analytics view by using the **Observability Analytics** page, and only when you use the **Observability Analytics (default)** query engine.
- All views that an analytics view queries must be in the same location.
- You must use the Google Cloud console to create or manage your analytics views.
- You can create at most 100 analytics views per Google Cloud project.
- You can create at most 50 analytics views per region.
- Per Google Cloud project, at most 10 regions can contain an analytics view.

## What's next

- [Create, query, and manage analytics views](https://docs.cloud.google.com/stackdriver/docs/observability/create-and-manage-analytics-views.md.txt).
- [Query and analyze telemetry with Observability Analytics](https://docs.cloud.google.com/stackdriver/docs/observability/analytics.md.txt).
- [Chart SQL query results](https://docs.cloud.google.com/stackdriver/docs/observability/analytics-chart.md.txt).
- [Save and share queries](https://docs.cloud.google.com/stackdriver/docs/observability/analytics-save-share-query.md.txt).
- [Sample SQL queries](https://docs.cloud.google.com/stackdriver/docs/observability/analytics-samples.md.txt).