# Google Cloud SQL

> Monitor Google Cloud SQL database metrics for MySQL, PostgreSQL, and SQL Server using OpenTelemetry collector with Last9

Source: https://last9.io/docs/integrations/databases/google-cloud-sql/

Use OpenTelemetry to collect Google CloudSQL metrics and send telemetry data to Last9.

This integration supports following CloudSQL databases:

- MySQL
- PostgreSQL
- SQL Server

## Prerequisites

Before setting up Google Cloud SQL monitoring, ensure you have:

- Google Cloud account with administrative privileges or CloudSQL Admin privileges
- Access to a project in Google Cloud Platform (GCP)
- Google CloudSQL APIs enabled - follow [this guide](https://cloud.google.com/endpoints/docs/openapi/enable-api) to enable APIs
- Virtual Machine where you can run OpenTelemetry Collector to collect data from Google CloudSQL and send to Last9

1.  **Setting up Service Account for Monitoring**

    **Create a Service Account:**

    - Navigate to Google Cloud Console: https://console.cloud.google.com/
    - Go to "IAM & Admin" > "Service Accounts" in the left sidebar
    - Click "CREATE SERVICE ACCOUNT" at the top
    - Enter a name (e.g., "otel-collector")
    - Optionally add a description (e.g., "Service account for OpenTelemetry Collector")
    - Click "CREATE AND CONTINUE"

    **Assign Required Roles:**

    - Add the following roles:
      - "Monitoring Viewer" (for reading metrics)
      - "Monitoring Metric Reader" (for accessing metric data)
    - Click "CONTINUE" and then "DONE"

    **Attach Service Account to VM Instance:**

    - Navigate to "Compute Engine" > "VM instances"
    - For an existing VM:
      - Select the instance, click "STOP"
      - Once stopped, click "EDIT"
      - Under "Service account", select your newly created service account
      - Click "SAVE" and restart the instance
    - For a new VM:
      - When creating the instance, select your service account under the "Identity and API access" section

    **Alternative: Using Application Default Credentials:**

    - If you cannot change the VM's service account, you can configure Application Default Credentials
    - In the Service Account page, select your service account
    - Click on "KEYS" tab
    - Click "ADD KEY" > "Create new key"
    - Select JSON format and click "CREATE"
    - Upload the key file to your VM
    - Set the environment variable before starting the collector:
      ```bash
      export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
      ```

    For more detailed information about authentication using service accounts, refer to [Google Cloud's Application Default Credentials documentation](https://cloud.google.com/docs/authentication/application-default-credentials).

2.  **Install OpenTelemetry Collector**

    You can install either `rpm` or `deb` package depending on flavor of operating system you are using.

    > Note: systemd is required for automatic service configuration.

**RPM Package**

    Installing rpm package:

    ```bash
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.rpm
    sudo rpm -ivh otelcol-contrib_0.118.0_linux_amd64.rpm
    ```

**DEB Package**

    Installing deb package:

    ```bash
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.deb
    sudo dpkg -i otelcol-contrib_0.118.0_linux_amd64.deb
    ```

    More installation options can be found [here](https://opentelemetry.io/docs/collector/installation/#linux).

3.  **Configure Collector**

    Use the following configuration for Otel Collector:

    ```yaml
    receivers:
      googlecloudmonitoring:
        collection_interval: 60s
        project_id: gcp-project-id
        metrics_list:
          # Filter for specific metrics such cloudsql
          - metric_descriptor_filter: 'metric.type = starts_with("cloudsql.googleapis.com")'

    processors:
      batch:
        timeout: 20s
        send_batch_size: 100000
        send_batch_max_size: 100000
      resourcedetection/gcp:
        detectors: ["gcp"]

    exporters:
      otlp/last9:
        endpoint: "{{ .Logs.WriteURL }}"
        headers:
          "Authorization": "{{ .Logs.AuthValue }}"
      debug:
        verbosity: detailed

    service:
      pipelines:
        metrics:
          receivers: [googlecloudmonitoring]
          processors: [batch, resourcedetection/gcp]
          exporters: [otlp/last9]
    ```

    **Note**: Replace `gcp-project-id` with your actual Google Cloud project ID.

    The complete list of supported metrics for CloudSQL MySQL can be found [here](https://cloud.google.com/sql/docs/mysql/admin-api/metrics).

    The complete list of supported metrics for CloudSQL PostgreSQL can be found [here](https://cloud.google.com/sql/docs/postgres/admin-api/metrics).

    The complete list of supported metrics for CloudSQL SQL Server can be found [here](https://cloud.google.com/sql/docs/sqlserver/admin-api/metrics).

    Read more about the Google Cloud monitoring receiver configuration options [here](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/googlecloudmonitoringreceiver#configuration).

4.  **Start Collector**

    Run the otel collector using `systemctl` command:

    ```bash
    sudo systemctl start otelcol-contrib
    sudo systemctl status otelcol-contrib
    sudo systemctl restart otelcol-contrib
    ```

5.  **Check Collector Logs**

    Monitor the collector logs to ensure it's working correctly:

    ```bash
    sudo journalctl -u otelcol-contrib -f
    ```

## Verification

To verify the setup is working:

1. Check that the collector service is running
2. Review the collector logs for any errors
3. Log into your Last9 account to confirm CloudSQL metrics are being received

## Need Help?

If you encounter any issues or have questions:

- Join our [Discord community](https://discord.com/channels/652153247672729619/652153247672729621) for real-time support
- Contact our support team at [support@last9.io](mailto:support@last9.io)
