

# Correlations in Grafana version 12
<a name="v12-correlations"></a>

****  
This documentation topic is designed for Grafana workspaces that support **Grafana version 12.x**.  
For Grafana workspaces that support Grafana version 10.x, see [Working in Grafana version 10](using-grafana-v10.md).  
For Grafana workspaces that support Grafana version 9.x, see [Working in Grafana version 9](using-grafana-v9.md).  
For Grafana workspaces that support Grafana version 8.x, see [Working in Grafana version 8](using-grafana-v8.md).

You can create interactive links for Explore visualizations to run queries related to presented data by setting up Correlations.

A correlation defines how data in one data source is used to query data in another data source. Some examples:
+ An application name returned in a logs data source can be used to query metrics related to that application in a metrics data source.
+ A user name returned by an SQL data source can be used to query logs related to that particular user in a logs data source.

Explore takes user-defined correlations to display links inside the visualizations. You can click on a link to run the related query and see results in Explore Split View.

Explore visualizations that currently support showing links based on correlations:
+ [Logs](v12-panels-logs.md)
+ [Table](v12-panels-table.md)

You can configure correlations using the **Administration > Plugins and data > Correlations** page in Grafana or directly in [Explore](v12-explore-correlations.md). You can also add correlations to external URLs directly from Explore, enabling seamless navigation between data sources and external systems.

**Topics**
+ [Correlation configuration](v12-correlations-config.md)
+ [Create a new correlation](v12-correlations-create.md)

# Correlation configuration
<a name="v12-correlations-config"></a>

****  
This documentation topic is designed for Grafana workspaces that support **Grafana version 12.x**.  
For Grafana workspaces that support Grafana version 10.x, see [Working in Grafana version 10](using-grafana-v10.md).  
For Grafana workspaces that support Grafana version 9.x, see [Working in Grafana version 9](using-grafana-v9.md).  
For Grafana workspaces that support Grafana version 8.x, see [Working in Grafana version 8](using-grafana-v8.md).

 Each correlation is configured with the following options: 

**Label**  
Link label, shown in the visualization.

**Description**  
Optional description.

**Source data source**  
The source of results that have links displayed.

**Results field**  
Defines where the link is shown in a visualization.

**Target query**  
The target query run when a link is clicked.

**Transformations**  
Optional manipulations applied to the source data before it is passed to the target query.

For details about creating a correlation, see [Create a correlation](v12-correlations-create.md).

## Source data source and result field
<a name="v12-correlations-config-source-and-result"></a>

Links are shown in Explore visualizations for the results from the correlation’s source data source. A link is assigned to one of the fields from the result provided in the correlation configuration (the results field). Each visualization displays fields with links in a different way. 

## Target query
<a name="v12-correlations-config-target-query"></a>

The target query is run when a link is clicked in the visualization. You can use the query editor of the selected target data source to specify the target query. Source data results can be accessed inside the target query with variables.

### Correlation Variables
<a name="v12-correlations-config-variables"></a>

You can use variables inside the target query to access the source data related to the query. Correlations use [Grafana variable syntax](v12-dash-variable-syntax.md). Variables are filled with values from the source results when the link is chosen. There are two types of variables you can use:
+ [Field variables](v12-panels-configure-data-links.md#v12-panels-data-link-variables) (to access field values and labels).
+ Correlation variables (to access field values and transformations).

Example: If source results contain a field called `employee`, the value of the field can be accessed with:
+ A field variable `${__data.fields.employee}`.
+ A correlation variable that maps the field value above to `${employee}`.

In addition to mapping field values to shorter variable names, more correlation variables can be created by applying transformations to existing fields.

Correlation creates a data link only if all variables have values in the selected data row. [Global variables](v12-dash-variable-add.md#v12-dash-variable-add-global) are the exception to this rule and are not required to be filled in from the returned data. These variables are interpolated automatically by data sources.

### Correlation Transformations
<a name="v12-correlations-config-transformations"></a>

Transformations provide a way to extract more variables out of field values. The output of transformations is a set of new variables that can be accessed as any other variable.

There are two types of transformations: logfmt and regular expression.

Each transformation uses a selected field value as the input. The output of a transformation is a set of new variables based on the type and options of the transformation.

### Logfmt transformation
<a name="v12-correlations-config-logfmt-transformation"></a>

The logfmt transformation deconstructs a field value containing text formatted with [logfmt key/value pairs](https://brandur.org/logfmt). Each pair becomes a variable with the key being the name of the variable.

The logfmt transformation only requires specifying the input field name if you would like the transformation to apply to a different field than the results field. Example output variables for `field = "host=srv001 endpoint=/test app=foo"`:


| name | value | 
| --- | --- | 
| host | srv001 | 
| endpoint | /test | 
| app | foo | 

### Regular expression transformation
<a name="v12-correlations-config-regular-expression-transformation"></a>

The regular expression transformation deconstructs a field value based on the provided regular expression.

Regular expression transformation options:

**field**  
Input field name

**expression**  
Regular expression. Named capture groups are mapped to variables matching the group name. If non-named matching groups are used a variable is created out of the first match. The value overrides the variable matching the input field or a new variable is created if mapValue is provided (see examples in the following table).

**mapValue**  
Used with simple regex groups without named matching groups. By default, the first match overrides the variable with the name of the field that is used as the input. To change that default behavior you can specify the mapValue property. The provided name is used to create a new variable. This can be useful if your target query requires both the exact value and a part of the value extracted with the transformation.

Example: Assuming the selected field name is `employee` and the field value is `John Doe`.

Various output variables based on expression and mapValue options:


| expression | mapValue | output variables | comment | 
| --- | --- | --- | --- | 
| /\$1w\$1 (\$1w\$1)/ | - | employee=Doe | No mapValue provided. The first matching is mapped to the existing field name variable (employee). | 
| /(\$1w\$1) (\$1w\$1)/ | name | name=John | The first matching is mapped to a new variable called name.  | 
| /(?\$1w\$1) (?\$1w\$1)/ | - | firstName=John, lastName=Doe | When named groups are used they are the names of the output variables and mapValue is ignored. | 
| /(?\$1w\$1) (?\$1w\$1)/ | name | firstName=John, lastName=Doe | Same as above | 

# Create a new correlation
<a name="v12-correlations-create"></a>

****  
This documentation topic is designed for Grafana workspaces that support **Grafana version 12.x**.  
For Grafana workspaces that support Grafana version 10.x, see [Working in Grafana version 10](using-grafana-v10.md).  
For Grafana workspaces that support Grafana version 9.x, see [Working in Grafana version 9](using-grafana-v9.md).  
For Grafana workspaces that support Grafana version 8.x, see [Working in Grafana version 8](using-grafana-v8.md).

You can create correlations in the Explore correlations editor, or using the Grafana **Administration** page in your Amazon Managed Grafana workspace.

**Prerequisites**

You must have permission to add new correlations. Only users with write permissions to data sources can define new correlations.

## Creating a correlation in Explore’s correlations editor
<a name="v12-correlations-create-explore"></a>

You can create a correlation in the Explore correlation editor. For more details, see [Creating a correlation](v12-explore-correlations.md#v12-explore-corr-create-a-correlation).

## Creating a correlation in the Administration page
<a name="v12-correlations-create-administration"></a>

You can use the Grafana console **Administration** page to create a correlation.

**To create a correlation in the Administration page**

1. Go to the **Administration** section in Grafana.

1. Under **Plugins and data**, open the **Correlations** page.

1. Choose the **Add** button in the top right corner.

1. Provide a **label** for the correlation.

1. (Optional) Provide an **description**.

1. Go to the next page.

1. Provide **target data source**.

1. Provide **target query** using variables.

1. Go to the next page.

1. Provide **source data source**.

1. Provide **results field**.

1. Add transformations if you need variables that are not fields in the source data source.

1. Choose **Add** to add a new transformation.

1. Select the type of a transformation.

1. Configure transformation depending on the selected type.

1. Save the correlation.

You can edit a correlation in the same way, but when editing, you can't change the selected data sources.