diff --git a/.gitignore b/.gitignore index 0bd19f8..08f721f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.so *.o *.bc -*.gc* \ No newline at end of file +*.gc* +/log/ +/tmp_check/ diff --git a/Makefile b/Makefile index c589f72..db70be3 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/expected/pgotel.out b/expected/pgotel.out new file mode 100644 index 0000000..80cb59a --- /dev/null +++ b/expected/pgotel.out @@ -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) + diff --git a/pgotel.cpp b/pgotel.cpp index ff455c6..19754a9 100644 --- a/pgotel.cpp +++ b/pgotel.cpp @@ -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 labels_map; @@ -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) { @@ -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(); diff --git a/sql/pgotel.sql b/sql/pgotel.sql new file mode 100644 index 0000000..8df6a91 --- /dev/null +++ b/sql/pgotel.sql @@ -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"}'); \ No newline at end of file