Logo

Postgres

OpenMeter can integrate with your Postgres database to collect usage data directly from your database. By leveraging your existing data source for usage metering, you can significantly decrease the upfront cost of changing your data collection strategy.

Powered by Redpanda Connect

Configuration

First, create a new YAML file for the collector configuration. You will have to use the sql_select Redpanda Connect input:

input:
  sql_select:
    driver: postgres
    dsn: 'postgres://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]'
    table: YOUR_TABLE
    columns:
      - id
      - data_column
      - subject_column
      - time
    where: time >= ?
    args_mapping: root = [ now().ts_unix() - 30 ]

The above section will tell Redpanda Connect how to read data from your database.

Next, you need to configure the mapping from your database schema to CloudEvents using bloblang:

pipeline:
  processors:
    - mapping: |
        root = {
          "id": this.id,
          "specversion": "1.0",
          "type": "your-usage-event-type",
          "source": "postgres",
          "time": this.time,
          "subject": this.subject_column,
          "data": {
            "data": this.data_column,
          },
        }

Finally, you need to configure the OpenMeter output:

output:
  label: 'openmeter'
  http_client:
    url: '${OPENMETER_URL:https://openmeter.cloud}/api/v1/events'
    verb: POST
    headers:
      # Optional: API key for OpenMeter Cloud
      Authorization: 'Bearer ${OPENMETER_TOKEN:}'
      Content-Type: 'application/json'
    # Batch settings for efficient API usage
    batching:
      # Send up to 100 events in a single request
      count: 100
      # Or send after 1 second, whichever comes first
      period: 1s
      processors:
        # Convert batch to JSON array format
        - archive:
            format: json_array
    dump_request_log_level: DEBUG

Read more about configuring Redpanda Connect in the OpenMeter Collector guide.

Installation

Check out the OpenMeter Collector guide for installation instructions.