Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .schema/pgdog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"mirror_exposure": 1.0,
"mirror_queue": 128,
"omnisharded_sticky": false,
"openmetrics_host": "0.0.0.0",
"openmetrics_namespace": null,
"openmetrics_port": null,
"passthrough_auth": "disabled",
Expand Down Expand Up @@ -983,6 +984,11 @@
"type": "boolean",
"default": false
},
"openmetrics_host": {
"description": "The IP address of the local network interface the OpenMetrics HTTP endpoint will bind to.\n\n**Note:** This setting cannot be changed at runtime.\n\n_Default:_ `0.0.0.0`\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#openmetrics_host>",
"type": "string",
"default": "0.0.0.0"
},
"openmetrics_namespace": {
"description": "Prefix added to all metric names exposed via the OpenMetrics endpoint.\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#openmetrics_namespace>",
"type": [
Expand Down
4 changes: 4 additions & 0 deletions example.pgdog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ shutdown_termination_timeout = 60_000
#
# Default: not set
openmetrics_port = 9090
# The IP address the OpenMetrics HTTP endpoint will bind to.
#
# Default: 0.0.0.0
openmetrics_host = "0.0.0.0"
# Namespace to use for Prometheus metrics.
#
# Disambiguates metric names from metrics scraped from other apps.
Expand Down
24 changes: 24 additions & 0 deletions pgdog-config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ pub struct General {
#[serde(default = "General::query_size_limit_action")]
pub query_size_limit_action: QuerySizeLimitAction,

/// The IP address of the local network interface the OpenMetrics HTTP endpoint will bind to.
///
/// **Note:** This setting cannot be changed at runtime.
///
/// _Default:_ `0.0.0.0`
///
/// <https://docs.pgdog.dev/configuration/pgdog.toml/general/#openmetrics_host>
#[serde(default = "General::openmetrics_host")]
pub openmetrics_host: String,

/// The port used for the OpenMetrics HTTP endpoint.
///
/// <https://docs.pgdog.dev/configuration/pgdog.toml/general/#openmetrics_port>
Expand Down Expand Up @@ -903,6 +913,7 @@ impl Default for General {
log_query_sample_length: Self::log_query_sample_length(),
query_size_limit: Self::default_query_size_limit(),
query_size_limit_action: Self::query_size_limit_action(),
openmetrics_host: Self::openmetrics_host(),
openmetrics_port: Self::openmetrics_port(),
openmetrics_namespace: Self::openmetrics_namespace(),
prepared_statements: Self::prepared_statements(),
Expand Down Expand Up @@ -1352,6 +1363,10 @@ impl General {
Self::env_enum_or_default("PGDOG_QUERY_SIZE_LIMIT_ACTION")
}

pub fn openmetrics_host() -> String {
Self::env_string_or_default("PGDOG_OPENMETRICS_HOST", "0.0.0.0")
}

pub fn openmetrics_port() -> Option<u16> {
Self::env_option("PGDOG_OPENMETRICS_PORT")
}
Expand Down Expand Up @@ -1781,6 +1796,15 @@ mod tests {
assert_eq!(General::port(), 6432);
}

#[test]
fn test_env_openmetrics_host() {
let _guard = set_env_var("PGDOG_OPENMETRICS_HOST", "127.0.0.1");
assert_eq!(General::openmetrics_host(), "127.0.0.1");

let _guard = remove_env_var("PGDOG_OPENMETRICS_HOST");
assert_eq!(General::openmetrics_host(), "0.0.0.0");
}

#[test]
fn test_lsn_checks_enabled() {
let mut general = General::default();
Expand Down
3 changes: 2 additions & 1 deletion pgdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ async fn pgdog(command: Option<Commands>) -> Result<(), Box<dyn std::error::Erro
}

if let Some(openmetrics_port) = general.openmetrics_port {
let openmetrics_host = general.openmetrics_host.clone();
pgdog::tasks::spawn("openmetrics server", async move {
stats::http_server::server(openmetrics_port).await
stats::http_server::server(&openmetrics_host, openmetrics_port).await
});
}

Expand Down
9 changes: 4 additions & 5 deletions pgdog/src/stats/http_server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::convert::Infallible;
use std::net::SocketAddr;

use http_body_util::Full;
use hyper::body::Bytes;
Expand Down Expand Up @@ -68,10 +67,10 @@ async fn metrics(_: Request<hyper::body::Incoming>) -> Result<Response<Full<Byte
Ok(response)
}

pub async fn server(port: u16) -> std::io::Result<()> {
info!("OpenMetrics endpoint http://0.0.0.0:{}", port);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let listener = TcpListener::bind(addr).await?;
pub async fn server(host: &str, port: u16) -> std::io::Result<()> {
let addr = format!("{}:{}", host, port);
info!("OpenMetrics endpoint http://{}", addr);
let listener = TcpListener::bind(&addr).await?;
let shutdown = tasks::shutdown_signal();

loop {
Expand Down
Loading