# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

public_url: "https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/third-party/postgresql"
app_url: "http://www.postgresql.org/"
short_name: PostgreSQL
long_name: PostgreSQL
logo_path: /images/launcher/logos/postgresql.png # supplied by google technical writer
description: |-
  The PostgreSQL integration collects database-usage metrics, such as the database
  size, the number of backends, or the number of operations. The integration also
  collects PostgreSQL logs and parses them into a JSON payload. This result
  includes fields for role, user, level, and message.
configure_integration: |-
  The `postgresql` receiver connects by default to a local `postgresql`
  server using a Unix socket and Unix authentication as the `root` user.
minimum_supported_agent_version:
  metrics: 2.21.0
  logging: 2.9.0
supported_operating_systems: linux
platforms_to_skip:
  # postgres is not supported on various distros.
  - rocky-linux-cloud:rocky-linux-8-optimized-gcp-arm64
  - rocky-linux-cloud:rocky-linux-9
  - rocky-linux-cloud:rocky-linux-9-optimized-gcp
  - rocky-linux-cloud:rocky-linux-9-arm64
  - rocky-linux-cloud:rocky-linux-9-optimized-gcp-arm64
  - suse-cloud:sles-15-arm64
supported_app_version: ["10.18+"]
expected_metrics:
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.backends
    value_type: INT64
    representative: true
  - kind: CUMULATIVE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.bgwriter.buffers.allocated
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: source
        value_regex: backend|backend_fsync|checkpoints|bgwriter
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.bgwriter.buffers.writes
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: type
        value_regex: requested|scheduled
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.bgwriter.checkpoint.count
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: type
        value_regex: sync|write
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.bgwriter.duration
    value_type: INT64
  - kind: CUMULATIVE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.bgwriter.maxwritten
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
      - name: source
        value_regex: heap_read|heap_hit|idx_read|idx_hit|toast_read|toast_hit|tidx_read|tidx_hit
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.blocks_read
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.commits
    value_type: INT64
  - kind: GAUGE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.connection.max
    value_type: INT64
  - kind: GAUGE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.database.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.db_size
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
      - name: index
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.index.scans
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
      - name: index
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.index.size
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
      - name: operation
        value_regex: ins|upd|del|hot_upd
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.operations
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: replication_client
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.replication.data_delay
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.rollbacks
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
      - name: state
        value_regex: dead|live
      - name: table
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.rows
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.table.count
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.table.size
    value_type: INT64
  - kind: CUMULATIVE
    labels:
      - name: database
        value_regex: .*
      - name: table
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.table.vacuum.count
    value_type: INT64
  - kind: GAUGE
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.wal.age
    value_type: INT64
  - kind: GAUGE
    labels:
      - name: operation
        value_regex: flush|replay|write
      - name: replication_client
        value_regex: .*
    monitored_resources: [gce_instance]
    type: workload.googleapis.com/postgresql.wal.lag
    value_type: INT64
expected_logs:
  - log_name: postgresql_general
    fields:
      - name: jsonPayload.message
        # There should be a line break between INTO and test2, this regex tests our multiline parsing
        value_regex: '.*INSERT INTO\s+test2 \(id\) VALUES\(''1970-01-01 00:00:00\.123 UTC''\).*'
        type: string
        description: Log of the database action
      - name: jsonPayload.level
        value_regex: STATEMENT
        type: string
        description: Log severity or type of database interaction type for some logs
      - name: jsonPayload.tid
        type: number
        description: Thread ID where the log originated
      - name: jsonPayload.user
        type: string
        optional: true
        description: Authenticated user for the action being logged when relevant
      - name: jsonPayload.database
        type: string
        description: Database name for the action being logged when relevant
        optional: true
      - name: severity
        type: string
        description: ""
configuration_options:
  logs:
    - type: postgresql_general
      fields:
        - name: type
          default: null
          description: This value must be `postgresql_general`.
        - name: include_paths
          default: "[/var/log/postgresql/postgresql*.log, /var/lib/pgsql/data/log/postgresql*.log, /var/lib/pgsql/*/data/log/postgresql*.log]"
          description: A list of filesystem paths to read by tailing each file. A wild card (`*`) can be used in the paths.
        - name: exclude_paths
          default: null
          description: A list of filesystem path patterns to exclude from the set matched by `include_paths`.
        - name: record_log_file_path
          default: false
          description: If set to `true`, then the path to the specific file from which the log record was obtained appears in the output log entry as the value of the `agent.googleapis.com/log_file_path` label. When using a wildcard, only the path of the file from which the record was obtained is recorded.
        - name: wildcard_refresh_interval
          default: 60s
          description: The interval at which wildcard file paths in `include_paths` are refreshed. Given as a [time duration](https://pkg.go.dev/time#ParseDuration), for example `30s` or `2m`. This property might be useful under high logging throughputs where log files are rotated faster than the default interval.
  metrics:
    - type: postgresql
      fields:
        - name: type
          default: null
          description: This value must be `postgresql`.
        - name: endpoint
          default: /var/run/postgresql/.s.PGSQL.5432
          description: The `hostname:port` or Unix socket path starting with `/` used to connect to the PostgreSQL server.
        - name: collection_interval
          default: 60s
          description: A [time duration](https://pkg.go.dev/time#ParseDuration) value, such as `30s` or `5m`.
        - name: username
          default: null
          description: The username used to connect to the server.
        - name: password
          default: null
          description: The password used to connect to the server.
        - name: insecure
          default: true
          description: Sets whether or not to use a secure TLS connection. If set to `false`, then TLS is enabled.
        - name: insecure_skip_verify
          default: false
          description: Sets whether or not to skip verifying the certificate. If `insecure` is set to `true`, then the `insecure_skip_verify` value is not used.
        - name: cert_file
          default: null
          description: Path to the TLS certificate to use for mTLS-required connections.
        - name: key_file
          default: null
          description: Path to the TLS key to use for mTLS-required connections.
        - name: ca_file
          default: null
          description: Path to the CA certificate. As a client, this verifies the server certificate. If empty, the receiver uses the system root CA.
