Skip to content

Commit

Permalink
Added regression tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrízio de Royes Mello <fabrizio@ongres.com>
  • Loading branch information
fabriziomello committed Sep 17, 2024
1 parent e92e73c commit 8caecb5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.so
*.o
*.bc
*.gc*
*.gc*
/log/
/tmp_check/
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ MODULE_big = pgotel

EXTENSION = pgotel

REGRESS = pgotel
REGRESS_OPTS = --temp-instance=tmp_check

DATA = pgotel--1.0.sql
OBJS = pgotel.o
PGFILEDESC = "pgotel - POC to interact with OpenTelemetry"
Expand Down
19 changes: 19 additions & 0 deletions expected/pgotel.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE EXTENSION pgotel;
SELECT pgotel_counter('foo', -1);
ERROR: counter value cannot be negative
SELECT pgotel_counter('foo', 0, '{}');
ERROR: invalid labels JSONB
SELECT pgotel_counter('foo', 0, NULL);
ERROR: labels cannot be NULL
SELECT pgotel_counter('foo', NULL);
ERROR: counter value cannot be negative
SELECT pgotel_counter(NULL, 0);
ERROR: counter name cannot be NULL
SELECT pgotel_counter(NULL, NULL);
ERROR: counter name cannot be NULL
SELECT pgotel_counter('foo', 0, '{"dbname": "foo", "schemaname": "bar", "relname": "baz"}');
pgotel_counter
----------------

(1 row)

10 changes: 9 additions & 1 deletion pgotel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Datum
pgotel_counter(PG_FUNCTION_ARGS)
{
char *counter_name = PG_ARGISNULL(0) ? NULL : text_to_cstring(PG_GETARG_TEXT_PP(0));
float8 value = PG_ARGISNULL(0) ? -1 : PG_GETARG_FLOAT8(1);
float8 value = PG_ARGISNULL(1) ? -1 : PG_GETARG_FLOAT8(1);
Jsonb *labels = PG_ARGISNULL(2) ? NULL : PG_GETARG_JSONB_P(2);
std::map<std::string, std::string> labels_map;

Expand All @@ -131,6 +131,9 @@ pgotel_counter(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("counter value cannot be negative")));

if (labels == NULL && PG_NARGS() == 3)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("labels cannot be NULL")));

/* iterate over the labels JSONB to include key/values to the C++ map data structure */
if (labels != NULL)
{
Expand All @@ -153,6 +156,11 @@ pgotel_counter(PG_FUNCTION_ARGS)
}
}

elog(DEBUG1, "labels_map size: %d", (int) labels_map.size());
if (labels != NULL && (int) labels_map.size() == 0)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid labels JSONB")));

/* Send the counter to the OTEL-Collector */
pgotel::counter(counter_name, value, labels_map);

PG_RETURN_NULL();
Expand Down
9 changes: 9 additions & 0 deletions sql/pgotel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE EXTENSION pgotel;

SELECT pgotel_counter('foo', -1);
SELECT pgotel_counter('foo', 0, '{}');
SELECT pgotel_counter('foo', 0, NULL);
SELECT pgotel_counter('foo', NULL);
SELECT pgotel_counter(NULL, 0);
SELECT pgotel_counter(NULL, NULL);
SELECT pgotel_counter('foo', 0, '{"dbname": "foo", "schemaname": "bar", "relname": "baz"}');

0 comments on commit 8caecb5

Please sign in to comment.