Latest Dynatrace
Use variables to filter your dashboards, to act as variable values in code tiles, and as placeholders in tile titles and Markdown tile text.
To add a variable to a dashboard
In the upper-right corner of the dashboard, select > Variables.
The Variable panel is displayed.
Define the variable.
dt_
Set Display settings.
When you're finished, select < Variable at the top to go to the Variables panel, or close the panel to display the dashboard.
Variables in dashboards can be defined to depend on other variables.
The value of a variable is recalculated if its definition refers to another variable and the other variable's value changes.
For example, if the value of variable A changes, the value of any variable whose definition refers to variable A is recalculated.
Loops are not allowed.
For example, if the value of variable A depends on the value of variable B, the value of variable B can't depend on the value of variable A.
How you edit a variable depends on the Type. For details and examples, see Query variable, CSV variable, Code variable, or Text variable below.
After you create a variable, you're ready to use it in a query.
Always remember to prepend a variable name with $
in your queries.
For example, if you create a variable named MyTotal
, you need to refer to it as $MyTotal
in your query.
For example:
Open the Add menu and select Variables to add a variable to your dashboard.
Define the following variable:
Host
fetch dt.entity.host| fields entity.name
Turn on Multi-select so you can select more than one value at a time to show in your visualizations.
Your changes are saved automatically.
Close the Variable panel.
Open the and select Query Grail.
Copy and paste the following query into the Query box and select Run.
Notice that the query refers to our new variable as $Host
, with the dollar sign indicating that it's a variable name.
fetch logs, scanLimitGBytes: 20| filter in(host.name, array($Host))| makeTimeseries count(), by:{ host.name }
or when not using a multi-select variable you can reference it like
fetch logs, scanLimitGBytes: 20| filter host.name == $Host| makeTimeseries count(), by:{ host.name }
Because you added a multi-select variable, you can use the value selector to determine which values are displayed in your visualizations.
You can of course use your variable in multiple queries.
To use a variable in an Explore data tile
Open the Add menu and select Logs.
Select and then select host.name from the Available filters.
In the added filter field, type $
to get suggestions for all available variables and then select $Host
.
Note: Adding variables in Explore tiles only works for single-select variables in combination with the = operator.
After you add at least one variable to a dashboard, a icon displayed next to the existing variables in the upper-left corner of the dashboard.
How you edit a variable depends on the Type. For details and examples, see Query variable, CSV variable, Code variable, or Text variable below.
To delete a variable from a dashboard
If a dashboard has one or more variables, they are listed by name along the upper-left of the dashboard, under the dashboard name. When you change variable values, the dashboard contents are recalculated and displayed according to the new values.
To change the value of a variable
In the upper-left of the dashboard, locate the variable name in the upper-left of the dashboards.
Use the menu or edit box under the variable name to change the value.
To define a query variable
status
, myHosts
, or Variable01
) and it can't start with dt_
.Query
.summarize
and collectDistinct
to get distinct values from data sources such as logs.If you add both examples below to your dashboard, you can filter your dashboard by host and log severity level. Be sure to turn on Multi-select if you want to select more than one at a time.
fetch dt.entity.host| fields id
If you want to use a human-readable name, use | fields entity.name
instead of | fields id
.
fetch logs| fields loglevel| filterOut loglevel == "NONE"| summarize distinctLoglevels = collectDistinct(loglevel)| expand distinctLoglevels| sort distinctLoglevels asc
To define a CSV (comma-separated values) variable
Set Name to the name you want to give your variable.
status
, myHosts
, or Variable01
) and it can't start with dt_
.Set Type to CSV
.
In Definition, enter a comma-separated list of possible values for this variable, such as dog, cat, horse
.
Be sure to use the correct value type. Dynatrace distinguishes values as numeric or string depending on how you specify them—with or without quotes. For example, 111
is a numeric value, while "111"
is a string.
Inspect the results in the Preview section to make sure it works as expected.
If you want to be able to select more than one value at a time, turn on Multi-select.
Your changes are saved automatically.
Close the Variable panel.
This example would add a $Status
variable to your dashboard with the ability to select more than one status at a time, and with four possible values: WARN, ERROR, INFO, NONE.
WARN, ERROR, INFO, NONE
To define a code variable
Set Name the name you want to give your variable.
status
, myHosts
, or Variable01
) and it can't start with dt_
.Set Type to Code
.
In Definition, enter the JavaScript code.
For security reasons, when using variables in code tiles, you can only access them within the default function.
Select Run query and inspect the results in the Preview section to make sure it works as expected.
If you want to be able to select more than one value at a time, turn on Multi-select.
Your changes are saved automatically.
Close the Variable panel.
/** This will run JavaScript in the DYNATRACE* serverless environment.* To generate variable options return string array.*/export default async function () {return ["val1", "val2", "val3"]}
To define a text variable
status
, myHosts
, or Variable01
) and it can't start with dt_
.Text
.Straightforward usage of a $resolution
variable (as in the following query) doesn't work because resolution requires a predefined data format, while a variable returns a string value.
fetch logs| ...| summarize count(), by: {loglevel, bin(timestamp, $resolution)}
As a workaround, you can use the duration function together with a DQL conversion function. This provides the required output based on your $resolution
value.
fetch logs| ...| summarize count(), by: {loglevel, bin(timestamp, duration(toLong($resolution), unit:"m"))}
If you want to filter a numeric value but compare it with a string representation, you can use a native DQL conversion function such as toString.
fetch logs| filter amount = toString($amount)| ...