Grafana Cloud

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.

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:

Group byUse case
NoneSee all individual queries
InstanceCompare query patterns across database servers
SchemaAnalyze query distribution across schemas
DatabaseSee 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
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
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:

LabelPurposeExample values
appApplication nameorder-service, user-api, inventory
teamOwning teamcheckout, platform, fulfillment
environmentDeployment environmentproduction, staging, development
regionGeographic regionus-east-1, eu-west-1