> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/use-continuous-delivery/verify-deployments/configure-cv/health-sources/loki.md).

# Grafana Loki

Add Grafana Loki as a health source in Harness CV to monitor log data and detect anomalies during deployments.

{% hint style="info" %}
**Feature flag required**

Grafana Loki requires the feature flag `SRM_ENABLE_GRAFANA_LOKI_LOGS`. Contact [Harness Support](mailto:support@harness.io) to enable the feature.
{% endhint %}

This topic explains how to add Grafana Loki as a health source for Continuous Verification (CV). Once configured, Harness queries your Loki instance for log data during deployment verification and flags anomalies that could indicate a degraded release.

***

## What you will learn from this topic

* **Grafana Loki connector setup:** How to create and configure a Grafana Loki connector, including the base URL, headers, and validation path.
* **Health source configuration:** How to add Grafana Loki as a health source in the AI Verify (v1) step.
* **Log query definition:** How to write a Loki log query and run it to confirm accuracy.
* **Field mapping:** How to map query results to Harness service instances for accurate verification.

***

## Before you begin

Before you add Grafana Loki as a health source, complete the following:

* **CV setup:** Set up an AI Verify (v1) step in your pipeline. Go to [Configure CV](/continuous-delivery/use-continuous-delivery/verify-deployments/configure-cv/verify-deployments.md) to complete the prerequisite configuration.
* **Grafana Loki instance:** You need a running Grafana Loki instance. By default, Loki exposes its API on port 3100 without authentication.
* **Connector permissions:** You need permission to create connectors in your Harness project.

***

## Add Grafana Loki as a health source

The following steps walk you through adding Grafana Loki as a health source in the AI Verify (v1) step.

### Define the health source

To configure the health source connection, do the following:

1. In the **Health Sources** section of the Verify screen, select **+ Add New Health Source**.

   The **Add New Health Source** dialog appears.
2. In the **Define Health Source** tab, do the following:
   1. In the **Define Health Source** section, select **GrafanaLoki** as the health source type.
   2. In the **Health Source Name** field, enter a name for the health source.
   3. In the **Connect Health Source** section, select **Select Connector**.

      The **Create or Select an Existing Connector** dialog appears.
   4. Select a connector for the Grafana Loki health source, then select **Apply Selected**.

      The selected connector appears in the **Select Connector** dropdown. The **Grafana Loki Logs** option is selected by default in the **Select Feature** field.

<details>

<summary>Create a new Grafana Loki connector</summary>

Follow these steps if you do not already have a Grafana Loki connector:

1. In the **Create or Select an Existing Connector** dialog, select **+ New Connector**.
2. In the **Overview** tab, enter a name for the connector, an optional description, and a tag, then select **Continue**. If you use multiple providers of the same type, give each provider a different name.
3. In the **Headers** tab, enter the following, then select **Next**:
   * **Base URL:** The base URL of your Grafana Loki account. By default, Grafana Loki exposes its API on port 3100 without authentication.
   * **Key** and **Value** (optional): A key-value pair for the Grafana Loki log stream you want to select in the query. Go to [Log stream selector](https://grafana.com/docs/loki/latest/logql/log_queries/#log-stream-selector) to review log stream selector syntax and key-value pairs.
4. In the **Parameters** tab (optional), enter the **Key** and **Value** pair.
5. In the **Validation Path** tab, select either the **GET** or **POST** request method and enter the **Validation Path**.

   If you select **POST**, you must also include the request body. The following is an example validation path for a **GET** request: `loki/api/v1/labels`.
6. In the **Delegates Setup** tab, choose one of the following:
   * **Use any available Delegate:** Harness automatically assigns an available delegate.
   * **Only use Delegates with all of the following tags:** Enter tags to ensure Harness selects only delegates assigned those specific tags.
7. Select **Save and Continue**.

   Harness verifies the connection.
8. Once the verification is successful, select **Finish**.

   The Grafana Loki connector is added to the list of connectors.

</details>

3. Select **Next**. The **Configuration** tab appears.

### Define log configuration settings

In the **Configuration** tab, do the following to add and name a query:

1. Select **+ Add Query**.

   The **Add Query** dialog appears.
2. Enter a name for the query, then select **Submit**.

   The query is listed under **Logs Group**. The query specification and mapping settings appear. These settings help you retrieve the desired logs from the Grafana Loki platform and map them to the Harness service.

### Define a query

After naming the query, configure the log query and field mappings:

1. In the **Query** field, enter a log query and select **Run Query** to execute it.

   A sample record appears in the **Records** field. This helps you confirm the accuracy of the query you constructed.
2. In the **Field Mapping** section, select **+** to map the **service instance identifiers** and select the data you want displayed from the logs. Go to [Service Instance Identifier (SII)](/continuous-delivery/use-continuous-delivery/verify-deployments/verify-deployments-with-the-verify-step.md#service-instance-identifier-sii) to understand how Harness uses this mapping.
3. Select **Get sample log messages**.

   Sample logs appear to help you verify that the query is correct.

<details>

<summary>Sample log query</summary>

The following query returns data from all filenames except syslog:

```
{filename=~".+",filename!="/var/log/syslog"}
```

![Grafana Loki sample query results showing matched log entries in the Records field](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-b8687a996341f0f4656830b34de388ad3f6a6643%2Fcv-loki-log-query.png?alt=media)

</details>

***

## Next steps

After adding Grafana Loki as a health source, run your pipeline to start collecting log data during deployment verification.

* [Configure CV](/continuous-delivery/use-continuous-delivery/verify-deployments/configure-cv/verify-deployments.md): Review the full CV configuration workflow to ensure all components are set up correctly.
* [Verify deployments with the Verify step](/continuous-delivery/use-continuous-delivery/verify-deployments/verify-deployments-with-the-verify-step.md): Understand how the AI Verify (v1) step evaluates health sources during a deployment.
* [Service Instance Identifier](/continuous-delivery/use-continuous-delivery/verify-deployments/verify-deployments-with-the-verify-step.md#service-instance-identifier-sii): Learn how Harness uses service instance identifiers to correlate log data with deployment instances.

{% @harness-feedback/feedback %}
