Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Add simple function to retrieve a labels_array
Browse files Browse the repository at this point in the history
Retrieves a labels_array from a series_id.
  • Loading branch information
cevian committed Oct 6, 2020
1 parent 9ce651e commit 76518ed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/sql_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ simply `cpu_usage` view). The view contains a the following column:

A label value can be retrieved from the label name id using the `val`
function. A full json for the series can be retrieved with `jsonb(labels)`.
The array of labels can be retrieved from a series_id with `labels(series_id)`.

For example:
```
Expand Down
9 changes: 9 additions & 0 deletions pkg/pgmodel/end_to_end_tests/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,16 @@ func TestSQLJsonLabelArray(t *testing.T) {

if labelSet.Fingerprint() != fingerprintRes {
t.Fatalf("Json not equal: id %v\n got\n%v\nexpected\n%v", seriesID, string(jsonRes), string(jsonOrig))
}

err = db.QueryRow(context.Background(), "SELECT jsonb(labels($1))", seriesID).Scan(&jsonRes)
if err != nil {
t.Fatal(err)
}
fingerprintRes = getFingerprintFromJSON(t, jsonRes)

if labelSet.Fingerprint() != fingerprintRes {
t.Fatalf("Json not equal: id %v\n got\n%v\nexpected\n%v", seriesID, string(jsonRes), string(jsonOrig))
}

err = db.QueryRow(context.Background(), "SELECT (key_value_array(labels)).* FROM _prom_catalog.series WHERE id=$1",
Expand Down
4 changes: 2 additions & 2 deletions pkg/pgmodel/migrations/migration_files_generated.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pkg/pgmodel/migrations/sql/idempotent/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,20 @@ COMMENT ON FUNCTION SCHEMA_PROM.jsonb(labels SCHEMA_PROM.label_array)
IS 'converts a labels array to a JSONB object';
GRANT EXECUTE ON FUNCTION SCHEMA_PROM.jsonb(SCHEMA_PROM.label_array) TO prom_reader;

--Returns the label_array given a series_id
CREATE OR REPLACE FUNCTION SCHEMA_PROM.labels(series_id BIGINT)
RETURNS SCHEMA_PROM.label_array AS $$
SELECT
labels
FROM
SCHEMA_CATALOG.series
WHERE id = series_id
$$
LANGUAGE SQL STABLE PARALLEL SAFE;
COMMENT ON FUNCTION SCHEMA_PROM.labels(series_id BIGINT)
IS 'fetches labels array for the given series id';
GRANT EXECUTE ON FUNCTION SCHEMA_PROM.labels(series_id BIGINT) TO prom_reader;

--Do not call before checking that the series does not yet exist
CREATE OR REPLACE FUNCTION SCHEMA_CATALOG.create_series(
metric_id int,
Expand Down

0 comments on commit 76518ed

Please sign in to comment.