Skip to content

Commit

Permalink
v0.0.1 working
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayko001 committed Oct 6, 2023
1 parent a950852 commit 2d9ce4e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 64 deletions.
154 changes: 91 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## Prometheus_fdw
NOTE: ask the user to enter the link to prometheus
NOTE: update step value
NOTE: add a cron job to make this automatic

### Pre-requisistes

- have the v0.2.2 of `prometheus_fdw` extension enabled in your instance
- have the latest version of `prometheus_fdw` extension enabled in your instance

`create extension prometheus_fdw;`

Expand All @@ -14,99 +17,124 @@ create foreign data wrapper prometheus_wrapper
validator prometheus_fdw_validator;
```

Create the server:

```
create server my_prometheus_server
foreign data wrapper prometheus_wrapper;
```

Create Foreign Table:

### Metric Labels Table

```
CREATE FOREIGN TABLE IF NOT EXISTS metric_labels (
metric_id BIGINT,
metric_name TEXT NOT NULL,
metric_name_label TEXT NOT NULL,
metric_labels jsonb
)
SERVER my_prometheus_server
OPTIONS (
object 'metric_labels'
);
```

### Metrics Value Table

NOTE: NEED TO ADD PARTIONTION TO THIS TABLE

```
CREATE FOREIGN TABLE IF NOT EXISTS metric_values (
metric_id BIGINT,
CREATE FOREIGN TABLE IF NOT EXISTS metrics (
metric_name TEXT,
metric_labels JSONB,
metric_time BIGINT,
metric_value FLOAT8
metric_value FLOAT8
)
server my_prometheus_server
options (
object 'metric_values'
object 'metrics'
);
```

CREATE FOREIGN TABLE IF NOT EXISTS metrics (

Create tables to store information locally:

```
CREATE TABLE IF NOT EXISTS metrics_local (
metric_name TEXT,
metric_labels JSONB,
metric_time BIGINT,
metric_value FLOAT8
)
server my_prometheus_server
options (
object 'metrics'
);

-- Create metric_labels table
CREATE TABLE public.metric_labels_local (
metric_id BIGINT NOT NULL,
metric_name TEXT NOT NULL,
metric_name_label TEXT NOT NULL,
metric_labels JSONB,
PRIMARY KEY (metric_id),
UNIQUE (metric_name, metric_labels)
CREATE TABLE public.metric_labels (
id BIGSERIAL NOT NULL,
name TEXT NOT NULL,
labels JSONB,
PRIMARY KEY (id),
UNIQUE (name, labels)
);
-- Create indexes for metric_labels table
CREATE INDEX metric_labels_labels_idx ON public.metric_labels USING gin (metric_labels);
-- Create partitioned metric_values table
CREATE TABLE public.metric_values (
id BIGINT NOT NULL,
time BIGINT,
value DOUBLE PRECISION NOT NULL
);
```

-- Create partitioned metric_values_local table
CREATE TABLE public.metric_values_local (
metric_id BIGINT NOT NULL,
metric_time BIGINT,
metric_value DOUBLE PRECISION NOT NULL
) PARTITION BY RANGE (metric_time);
NOTE: need to index and partition the tables

-- Create indexes for metric_values table
CREATE INDEX metric_values_id_time_idx ON public.metric_values (metric_id, metric_time DESC);
CREATE INDEX metric_values_time_idx ON public.metric_values (metric_time DESC);
## Queries

-- You can create a partition of metric_values table for a specific date range like so:
CREATE TABLE public.metric_values_20231002 PARTITION OF public.metric_values
FOR VALUES FROM ('2023-10-02 00:00:00+00') TO ('2023-10-03 00:00:00+00');
To simply run the fdw and look at values

```
select * from metrics where metric_name='container_cpu_usage_seconds_total' AND metric_time > 1696046800 AND metric_time < 1696133000;
```

To store the information in your local database for future use
```
INSERT INTO metrics_local
select * from metrics where metric_name='container_cpu_usage_seconds_total' AND metric_time > 1696046800 AND metric_time < 1696046800;
```

To save information for long term and/or analysis
```
INSERT INTO public.metric_labels (name, labels)
SELECT
metric_name,
metric_labels
FROM metrics_local
WHERE
metric_time > 1696046800 AND metric_time < 1696133000
AND metric_name = 'container_cpu_usage_seconds_total'
ON CONFLICT (name, labels) DO NOTHING;
```

SELECT * FROM metric_values WHERE metric_time > 1696046400 AND metric_time < 1696132800;
To store values for long term and/or analysis

```
INSERT INTO public.metric_labels (name, labels)
SELECT
label.metric_name AS metric_label,
value.metric_time,
value.metric_value
FROM
metric_labels AS label
JOIN
metric_values AS value
ON
label.metric_id = value.metric_id
metric_name,
metric_labels
FROM metrics_local
WHERE
label.metric_name = 'container_threads' AND
value.metric_time < 1696046400 AND value.metric_time > 1696132800;
metric_time > 1696046800 AND metric_time < 1696133000
AND metric_name = 'container_cpu_usage_seconds_total'
ON CONFLICT (name, labels) DO NOTHING;
```

To store values for long term and/or analysis

```
ALTER TABLE metric_values
ADD CONSTRAINT metric_values_unique UNIQUE (id, time);
```

```
INSERT INTO metric_values (id, time, value)
SELECT
mlab.id,
ml.metric_time,
ml.metric_value
FROM
metrics_local ml
INNER JOIN
metric_labels mlab
ON
ml.metric_name = mlab.name AND ml.metric_labels = mlab.labels
ON CONFLICT (id, time) DO NOTHING;
```







2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) struct PrometheusFdw {

impl PrometheusFdw {
const DEFAULT_BASE_URL: &'static str =
"https://prometheus-control-1.use1.dev.plat.cdb-svc.com/api/v1/query";
"https://prometheus-data-1.use1.prod.plat.cdb-svc.com//api/v1/query";

fn value_to_promql_string(value: &supabase_wrappers::interface::Value) -> String {
match value {
Expand Down

0 comments on commit 2d9ce4e

Please sign in to comment.