To write Metrics API v2 queries in Data Explorer, turn on Advanced mode and follow the steps below.
In Advanced mode, you can:
metric A + metric B.You can start building a simple query through the web UI and then turn on Advanced mode to see the underlying query.
For example, if you start with this query with Advanced mode off:

you will see this when you turn Advanced mode on:

The underlying query code is displayed in the form in which it's passed to the Metrics API v2:
builtin:host.cpu.usage:splitBy("dt.entity.host"):avg:auto:sort(value(avg,descending)):limit(20)
For simple queries where you need one of the following transformations, you can turn advanced mode off again:
For a detailed description of the individual transformations, see metric selector.
Turning advanced mode off is possible only if the transformations you edited in advanced mode were added when advanced mode was off.
For example, if you initially applied a sort transformation with advanced mode off, you can change the sort order from ascending to descending with advanced mode on and then switch advanced mode back off. After you add a new transformation, such as a timeshift or metric expression, you can't switch advanced mode back off and continue working.
The main functions of the query editor are the same whether Advanced mode is turned on or off:
To add a metric, select Add metric to add a row (another metric) to the query.
To duplicate a metric, select More (…) > Duplicate in a row to duplicate that row (metric).
To delete a metric, select More (…) > Delete in a row to delete that row (metric).
To reorder metrics, select and drag the metric to a new position in the list of metrics, then rerun the query to see your changes.

The order in which metrics are listed in your query affects the following:
The main difference is in how you edit the query and the number of possibilities available:
The easiest way to select metrics for a query is to start with Advanced mode turned off.
Place the pointer in a row and enter a metric name. Matching metrics are listed.
For example, enter cpu usage, then select builtin:host.cpu.usage from the list.

You can select commonly applied aggregations, dimensions, and transformations:
Average, Count, Maximum, Minimum, Sum, Median, Percentile 10th, Percentile 75th, or Percentile 90th.The selected aggregation is applied after the Split by. For example, if you select Percentile 10th and split by Host for a gauge metric such as builtin:host.cpu.idle, the percentile is calculated on the values after splitting by host.
Turn Advanced mode on.
Edit the results as needed.
To add a metric in Advanced mode
Place the pointer in a row and enter a metric key. Matching metrics are listed.
For example, enter host.cpu, then select builtin:host.cpu.usage from the list.

You need to add all transformations manually.
In the edit box:
:) to list what you can add at that pointer position, then select an item from the list.Shift+Enter to force a new line. This can improve readability and doesn't affect query evaluation.( to wrap the selection in parentheses.An operand is a metric or a number.
(). You can also use parentheses to enforce precedence.For full details on writing metric queries, see:
Metric expressions enable you to apply simple arithmetic operations on operands (metrics or numbers).
For example, this expression calculates the ratio (as a percentage) of two metrics:
((metric1)/(metric2))*(100)
The expression contains these basic components:
()+, -, *, /-()Arithmetic operations use the data points of tuples (unique combinations of metric—dimension—dimension value) of metrics. Identical tuples of each metric are paired and then their data points are aligned. For details, see metric expression tuples.
Learn how to:
This example shows how to transform a gauge metric into a delta count metric.
With Advanced mode turned on, assemble this gauge metric:
builtin:cloud.kubernetes.pod.containerRestarts:splitBy()
Make the following selections in the editor:
Select metric: Enter part of the metric key until the metric appears, then select it.

Select splitBy: Enter a colon (:) followed by a partial search string for the item you want to add, such as splitBy, then select it from the list.

Intermediate state:

builtin:cloud.kubernetes.pod.containerRestarts:splitBy()
Change the gauge metric to a delta count metric.
Select avg: Enter a colon (:) followed by a partial search string for avg, then select it from the list.

Select delta: Enter a colon (:) followed by a partial search string for delta, then select it from the list.

Final state:

builtin:cloud.kubernetes.pod.containerRestarts:splitBy():avg:delta
Run the query.
Learn how to:
This example displays the error rate for a conversion page. Start with these metrics:
builtin:apps.web.action.count.load.browser)builtin:apps.web.action.countOfErrors)Simple counts don't tell the whole story. To report the conversion page error rate, calculate a third metric from the action and error counts. The query for the third metric will divide the error count by the action count and filter for the page name.
You can create this query mostly through menu selections.
With Advanced mode turned off, select the metrics, aggregations, and filters.

With Advanced mode turned on, view the query code.

Copy and paste the contents of edit box B into edit box A, combining the two queries with added parentheses and a division sign, and then delete B.
If A is the first operand:
builtin:apps.web.action.countOfErrors:filter(and(or(in("dt.entity.application_method",entitySelector("type(application_method),entityName.equals(~"loading of page /easytravel/home~")"))))):splitBy():sum:auto:sort(value(sum,descending)):limit(20)
and B is the second operand:
builtin:apps.web.action.count.load.browser:filter(and(or(in("dt.entity.application_method",entitySelector("type(application_method),entityName.equals(~"loading of page /easytravel/home~")"))))):splitBy():sum:auto:sort(value(sum,descending)):limit(20)
Divide (A)/(B):
((builtin:apps.web.action.countOfErrors:filter(and(or(in("dt.entity.application_method",entitySelector("type(application_method),entityName.equals(~"loading of page /easytravel/home~")"))))):splitBy():sum:auto:sort(value(sum,descending)):limit(20))/(builtin:apps.web.action.count.load.browser:filter(and(or(in("dt.entity.application_method",entitySelector("type(application_method),entityName.equals(~"loading of page /easytravel/home~")"))))):splitBy():sum:auto:sort(value(sum,descending)):limit(20))
The result should be something like this:

Run the query.
Now you can add thresholds and pin the query to a dashboard.
Learn how to add context to line charts and other visualizations so you can identify normal values.
A line or single value can be difficult to interpret without context, especially for new users. Compare values over time to determine whether a spike or value is anomalous.
This example duplicates a metric and applies the :timeshift transformation to add context to your line charts.
Start with builtin:apps.web.largestContentfulPaint.load.browser, a built-in Core web vital metric that gives you the largest contentful paint measurements for all load actions for all your web applications.
You can create this query mostly through menu selections.
The final query code for A and B should look something like this (depending on your selections in step 1):
A without timeshift:
builtin:apps.web.largestContentfulPaint.load.browser:splitBy():percentile(75):auto:sort(value(percentile(75),descending)):limit(10)
B with a timeshift applied:
builtin:apps.web.largestContentfulPaint.load.browser:timeshift(-1w):splitBy():percentile(75):auto:sort(value(percentile(75),descending)):limit(10)
Learn how to use the entity selector and relationships to filter a metric by the values of a related entity.
In this example, you:
builtin:cloud.kubernetes.node.coresin filter for the Kubernetes nodes like dt.entity.kubernetes_nodeThe entity selector has these components:
type(KUBERNETES_NODE) defines the type of entity to select.toRelationships.IS_KUBERNETES_CLUSTER_OF_NODE( defines the relationship between the Kubernetes node on the left and the Kubernetes cluster on the right. It identifies Kubernetes clusters associated with the selected Kubernetes node.type(KUBERNETES_CLUSTER),entityId(KUBERNETES_CLUSTER-A943C5CF0A41A684))"))) identifies the Kubernetes cluster on the right side of the relationship by entity ID.The final query looks like this:
(builtin:cloud.kubernetes.node.cores:avg):filter(in("dt.entity.kubernetes_node",entitySelector("type(KUBERNETES_NODE),toRelationships.IS_KUBERNETES_CLUSTER_OF_NODE(type(KUBERNETES_CLUSTER),entityId(KUBERNETES_CLUSTER-A943C5CF0A41A684))")))
Learn how to analyze an explicit histogram metric in the Data Explorer using the histogram transformation and how to extract percentiles from it.
In this example, you:
_cws.otlp.metrics.generic.histogram1.histogram. Note the .histogram suffix.dt.kubernetes.cluster.id.histogram transformation to expose the buckets of the histogram metric as dimensions.The final query looks like this:
_cws.otlp.metrics.generic.histogram1.histogram:filter(eq("dt.kubernetes.cluster.id", "KUBERNETES_CLUSTER-1")):histogram

The value of the 'le' dimension denotes the upper boundary (less than or equal to) of each bucket. In this example, the upper boundary of bucket 55 is 5.
The histogram transformation returns cumulative bucket counts, which only increase from one bucket to the next. To get the count of a single bucket, subtract the cumulative count at the next-lower boundary from the cumulative count at its own boundary.
In a graph, the histogram transformation draws one series per le value, and the +Inf series is the total count, so a graph of the transformation is hard to read.
You can use the percentile transformation to extract a percentile from a histogram metric. For example, you can extract the median using percentile(50).
The final query looks like this:
_cws.otlp.metrics.generic.histogram1.histogram:filter(eq("dt.kubernetes.cluster.id", "KUBERNETES_CLUSTER-1")):percentile(50)

To see the distribution per bucket directly, select the Histogram visualization, which does this subtraction for you.