-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic setup to automate * add indexing and partitioning function * add basic setup steps as postgresql functions * update function signature * basic mkdocs setup * release new version * Revert "basic mkdocs setup" This reverts commit 8106280.
- Loading branch information
Showing
5 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "prometheus_fdw" | ||
version = "0.1.4" | ||
version = "0.1.5" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE FUNCTION basic_setup(base_url text) | ||
RETURNS void | ||
AS 'MODULE_PATHNAME', 'basic_setup' | ||
LANGUAGE C STRICT; | ||
|
||
CREATE FUNCTION create_tables() | ||
RETURNS void | ||
AS 'MODULE_PATHNAME', 'create_tables' | ||
LANGUAGE C STRICT; | ||
|
||
CREATE FUNCTION create_indexes() | ||
RETURNS void | ||
AS 'MODULE_PATHNAME', 'create_indexes' | ||
LANGUAGE C STRICT; | ||
|
||
CREATE FUNCTION create_partitions(retention_period text) | ||
RETURNS void | ||
AS 'MODULE_PATHNAME', 'create_partitions' | ||
LANGUAGE C STRICT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use pgrx::prelude::*; | ||
use std::error::Error; | ||
|
||
#[pg_extern] | ||
fn basic_setup(base_url: &str) -> Result<(), Box<dyn Error>> { | ||
let queries = format!( | ||
r#" | ||
-- Enable the extensions | ||
CREATE EXTENSION IF NOT EXISTS prometheus_fdw CASCADE; | ||
CREATE EXTENSION IF NOT EXISTS pg_partman CASCADE; | ||
CREATE EXTENSION IF NOT EXISTS pg_cron CASCADE; | ||
-- Create the FDW | ||
CREATE FOREIGN DATA WRAPPER prometheus_wrapper | ||
HANDLER prometheus_fdw_handler | ||
VALIDATOR prometheus_fdw_validator; | ||
-- Configure connection to server | ||
CREATE SERVER my_prometheus_server | ||
FOREIGN DATA WRAPPER prometheus_wrapper | ||
OPTIONS ( | ||
base_url '{}'); | ||
-- Create FDW table we can query to get metrics | ||
CREATE FOREIGN TABLE metrics ( | ||
metric_name TEXT, | ||
metric_labels JSONB, | ||
metric_time BIGINT, | ||
metric_value FLOAT8 | ||
) | ||
SERVER my_prometheus_server | ||
OPTIONS ( | ||
object 'metrics', | ||
step '{}' | ||
); | ||
"#, | ||
base_url, "10m" | ||
); | ||
|
||
Spi::run(&queries); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Creates the necessary tables for metric tracking. | ||
#[pg_extern] | ||
fn create_tables() -> Result<(), Box<dyn Error>> { | ||
let queries = r#" | ||
CREATE TABLE IF NOT EXISTS metric_labels ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
labels jsonb NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS metric_values ( | ||
label_id INTEGER REFERENCES metric_labels (id), | ||
time TIMESTAMP NOT NULL, | ||
value DOUBLE PRECISION NOT NULL | ||
) PARTITION BY RANGE (time); | ||
"#; | ||
|
||
Spi::run(&queries); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Creates indexes to optimize query performance. | ||
#[pg_extern] | ||
fn create_indexes() -> Result<(), Box<dyn Error>> { | ||
let queries = r#" | ||
CREATE INDEX idx_metric_labels_name ON metric_labels (name); | ||
CREATE INDEX idx_metric_labels_labels ON metric_labels USING GIN (labels); | ||
CREATE INDEX idx_metric_values_time ON metric_values (time); | ||
CREATE INDEX idx_metric_values_label_id ON metric_values (label_id); | ||
"#; | ||
|
||
Spi::run(queries); | ||
Ok(()) | ||
} | ||
|
||
/// Sets up partitioning for the metric_values table and configures retention policy. | ||
#[pg_extern] | ||
fn create_partitions(retention_period: &str) -> Result<(), Box<dyn Error>> { | ||
let setup_partitioning = r#" | ||
SELECT create_parent('public.metric_values', 'time', 'native', '1 day'); | ||
"#; | ||
|
||
// Execute the partition setup query | ||
Spi::run(setup_partitioning); | ||
|
||
let setup_retention = format!( | ||
r#" | ||
UPDATE part_config | ||
SET retention = '{}', | ||
retention_keep_table = false, | ||
retention_keep_index = false, | ||
infinite_time_partitions = true | ||
WHERE parent_table = 'public.metric_values'; | ||
"#, | ||
retention_period | ||
); | ||
|
||
// Execute the retention setup query | ||
Spi::run(&setup_retention); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters