---
title: "Find your application's queries | Database Observability documentation"
description: "Identify and trace database queries back to your application code using filtering and labels."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# Find your application’s queries

When you need to understand how your application uses the database, Database Observability helps you identify specific queries and filter by application components.

## Overview

This guide covers two approaches to finding your application’s queries:

1. **Filtering**: Use the Queries Overview dashboard filters to narrow down by instance, schema, or custom labels
2. **Labels**: Organize queries by application, team, or environment using Alloy labels

## Filter queries in the dashboard

The Queries Overview dashboard provides several filtering options to find specific queries.

### Use the search box

The search box at the top of the dashboard filters queries by SQL text:

1. Navigate to **Database Observability** in Grafana Cloud.
2. In the search box, enter part of your query text.
3. The table filters to show only matching queries.

**Search examples:**

- `SELECT * FROM users` - Find queries that select from the users table
- `INSERT INTO orders` - Find insert operations on orders
- `JOIN products` - Find queries that join with the products table

### Filter by instance

To view queries for a specific database server:

1. Click the **Filters** dropdown and select the **instance** label.
2. Select one database instance.
3. The dashboard updates to show only queries from the selected instance.

### Filter by schema or database

To narrow down to a specific schema:

1. Click the **Filters** dropdown and select the **schema** label (MySQL) or **datname** label (PostgreSQL).
2. Select the schema or database name.
3. View queries that execute against that schema.

### Group queries

Use the **Group by** control to organize queries:

Expand table

| Group by | Use case                                       |
|----------|------------------------------------------------|
| None     | See all individual queries                     |
| Instance | Compare query patterns across database servers |
| Schema   | Analyze query distribution across schemas      |
| Database | See queries grouped by logical database        |

## Use labels to identify applications

Configure Alloy to add custom labels that identify which application or service generates each query.

### Add application labels in Alloy

In your Alloy configuration, add labels in the `loki.relabel` component:

Alloy ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
loki.relabel "db_o11y" {
  forward_to = [loki.write.default.receiver]

  rule {
    target_label = "app"
    replacement  = "order-service"
  }

  rule {
    target_label = "team"
    replacement  = "checkout"
  }

  rule {
    target_label = "environment"
    replacement  = "production"
  }
}
```

Ensure the same labels are added to your `discovery.relabel` component for metrics:

Alloy ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
discovery.relabel "db_o11y" {
  targets = prometheus.exporter.postgres.default.targets

  rule {
    target_label = "app"
    replacement  = "order-service"
  }

  rule {
    target_label = "team"
    replacement  = "checkout"
  }

  rule {
    target_label = "environment"
    replacement  = "production"
  }
}
```

### Filter by custom labels

After adding labels, use them in the dashboard:

1. Click **Add filter** in the Queries Overview dashboard.
2. Select your custom label (for example, `app`).
3. Choose the value to filter by (for example, `order-service`).

### Label strategy for multi-application environments

When multiple applications share a database, use a consistent labeling strategy:

Expand table

| Label         | Purpose                | Example values                           |
|---------------|------------------------|------------------------------------------|
| `app`         | Application name       | `order-service`, `user-api`, `inventory` |
| `team`        | Owning team            | `checkout`, `platform`, `fulfillment`    |
| `environment` | Deployment environment | `production`, `staging`, `development`   |
| `region`      | Geographic region      | `us-east-1`, `eu-west-1`                 |
