Skip to content

Service discovery

Mikhail Grigorev edited this page Jan 27, 2025 · 4 revisions

Starting with pgSCV 0.10.0, it has become possible to split the /metrics endpoint into a per-service version in the form of /metrics?target=service_name endpoints, and the /targets endpoint is also available, which contains a list of all endpoints in json format for setting up automatic endpoint discovery in using VictoriaMetrics (http_sd_configs).

Example configuration for VictoriaMetric:

global:
  scrape_interval: 30s

scrape_configs:
  - job_name: 'pgscv'
    http_sd_configs:
      - url: http://X.X.X.X:9890/targets

The output of the /targets endpoint will be a list of all active URLs in json format for each service. For example, for a configuration /etc/pgscv.yaml of this type:

listen_address: 0.0.0.0:9890
services:
  "postgres1":
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5432/postgres"
  "postgres2": 
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5433/postgres"
  "postgres3": 
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5434/postgres"

For example, a /targets output

# curl -sL http://127.0.0.1:9890/targets | jq .
[
  {
    "targets": [
      "127.0.0.1:9890/metrics?target=system:0",
      "127.0.0.1:9890/metrics?target=postgres1",
      "127.0.0.1:9890/metrics?target=postgres2",
      "127.0.0.1:9890/metrics?target=postgres3"
    ]
  }
]

You may notice that the json output contains the address 127.0.0.1 This address was determined automatically detecting on the request host address. If we try to get data from the external IP address of the server, we will see other information, for example:

# curl -sL http://10.200.10.1:9890/targets | jq .
[
  {
    "targets": [
      "10.200.10.1:9890/metrics?target=system:0",
      "10.200.10.1:9890/metrics?target=postgres1",
      "10.200.10.1:9890/metrics?target=postgres2",
      "10.200.10.1:9890/metrics?target=postgres3"
    ]
  }
]

You can define your address or hostname that will be output in json, for this purpose the url_prefix setting is used in the /etc/pgscv.yaml configuration file, for example:

listen_address: 0.0.0.0:9890
url_prefix: "example.com"
services:
  "postgres1":
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5432/postgres"
  "postgres2": 
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5433/postgres"
  "postgres3": 
    service_type: "postgres"
    conninfo: "postgres://pgscv:[email protected]:5434/postgres"

For example, a /targets output

# curl -sL http://10.200.10.1:9890/targets | jq .
[
  {
    "targets": [
      "example.com/metrics?target=system:0",
      "example.com/metrics?target=postgres1",
      "example.com/metrics?target=postgres2",
      "example.com/metrics?target=postgres3"
    ]
  }
]