Configure Grafana Alloy

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

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

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

  3. Run Alloy with the public preview stability flag:

    Bash
    alloy run config.alloy --stability.level=public-preview
  4. Create the Data Source Name secret file. Replace the placeholders with your values:

    Bash
    echo 'db-o11y:<DB_PASSWORD>@tcp(<DB_HOST>:<DB_PORT>)/' > /var/lib/alloy/mysql_secret

    Replace:

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

    Alloy
    local.file "mysql_secret" {
      filename  = "/var/lib/alloy/mysql_secret"
      is_secret = true
    }
    
    prometheus.exporter.mysql "mysql" {
      data_source_name  = local.file.mysql_secret.content
      enable_collectors = ["perf_schema.eventsstatements"]
      perf_schema.eventsstatements {
        text_limit = 0
      }
    }
    
    database_observability.mysql "mysql" {
      data_source_name = local.file.mysql_secret.content
      forward_to       = [loki.relabel.database_observability_mysql.receiver]
      targets          = prometheus.exporter.mysql.mysql.targets
    }
  6. Add the relabeling rules and Prometheus scrape configuration:

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

  8. 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>"
        }
      }
    }
  9. Replace the region, usernames, and API keys with your Grafana Cloud stack values.

  10. You’ve configured Grafana Alloy with all the Database Observability components for MySQL.

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


page 5 of 8