Configure Grafana Alloy

In this milestone, you’ll install Grafana Alloy and add the Database Observability configuration blocks for PostgreSQL. Alloy collects both metrics and structured logs from your database and forwards them to Grafana Cloud.

  1. To configure Grafana Alloy to connect your PostgreSQL database to Grafana Cloud, complete the following steps:

  2. Install Grafana Alloy 1.15.0 or later. Refer to the Alloy installation documentation for your platform.

  3. Create the Data Source Name secret file. Replace the placeholders with your values:

    Bash
    echo 'postgresql://db-o11y:<DB_PASSWORD>@<DB_HOST>:<DB_PORT>/postgres?sslmode=require' > /var/lib/alloy/postgres_secret

    Replace:

    • DB_PASSWORD — Password for the db-o11y user you created
    • DB_HOST — Hostname or IP of your PostgreSQL server
    • DB_PORT — PostgreSQL port (default: 5432)
  4. Add the core Database Observability blocks to your Alloy configuration file. These configure the PostgreSQL exporter for metrics and the Database Observability component for query-level logs:

    Alloy
    local.file "postgres_secret" {
      filename  = "/var/lib/alloy/postgres_secret"
      is_secret = true
    }
    
    prometheus.exporter.postgres "postgres" {
      data_source_names  = [local.file.postgres_secret.content]
      enabled_collectors = ["stat_statements"]
    }
    
    database_observability.postgres "postgres" {
      data_source_name  = local.file.postgres_secret.content
      forward_to        = [loki.relabel.database_observability_postgres.receiver]
      targets           = prometheus.exporter.postgres.postgres.targets
      enable_collectors = ["query_details", "query_samples", "schema_details", "explain_plans"]
      exclude_users     = ["db-o11y"]
    }
  5. Add the relabeling rules and Prometheus scrape configuration:

    Alloy
    loki.relabel "database_observability_postgres" {
      forward_to = [loki.write.logs_service.receiver]
    
      rule {
        target_label = "instance"
        replacement  = "<DB_HOST>:<DB_PORT>"
      }
    }
    
    discovery.relabel "database_observability_postgres" {
      targets = database_observability.postgres.postgres.targets
    
      rule {
        target_label = "job"
        replacement  = "integrations/db-o11y"
      }
      rule {
        target_label = "instance"
        replacement  = "<DB_HOST>:<DB_PORT>"
      }
    }
    
    prometheus.scrape "database_observability_postgres" {
      targets    = discovery.relabel.database_observability_postgres.output
      forward_to = [prometheus.remote_write.metrics_service.receiver]
    }
  6. In the relabeling blocks you just added, replace the DB_HOST and DB_PORT placeholder values with your PostgreSQL host and port.

  7. Add the Prometheus remote write and Loki write blocks. Find your Grafana Cloud credentials in your stack’s configuration page:

    Alloy
    prometheus.remote_write "metrics_service" {
      endpoint {
        url = "https://prometheus-<region>.grafana.net/api/prom/push"
        basic_auth {
          username = "<METRICS_USER>"
          password = "<METRICS_API_KEY>"
        }
      }
    }
    
    loki.write "logs_service" {
      endpoint {
        url = "https://logs-<region>.grafana.net/loki/api/v1/push"
        basic_auth {
          username = "<LOGS_USER>"
          password = "<LOGS_API_KEY>"
        }
      }
    }
  8. Replace the region, usernames, and API keys with your Grafana Cloud stack values.

  9. You’ve configured Grafana Alloy with all the Database Observability components for PostgreSQL.

In the next milestone, you’ll verify that telemetry is flowing to Grafana Cloud.


page 5 of 8