eBPF-based security platform
otel: grow a log payload as its AnyValues are written
Daniel Borkmannc7362063c30a
diff --git a/testing/functional/otel/t8004-status-otel.sh b/testing/functional/otel/t8004-status-otel.shindex 34036364..a57e7e6d 100644--- a/testing/functional/otel/t8004-status-otel.sh+++ b/testing/functional/otel/t8004-status-otel.sh@@ -100,6 +100,31 @@ PYdone'+test_expect_success 'a status body many times larger as AnyValues is sent whole' '+ work=$(mktemp -d) || return 1+ python3 helper/otel-mock.py success "$work/port" "$work/body" \+ "$work/meta" &+ server=$!+ trap "kill $server 2>/dev/null || true; wait $server 2>/dev/null || true; rm -rf $work" EXIT+ i=0+ while test ! -s "$work/port" && test "$i" -lt 100; do+ sleep .02+ i=$((i + 1))+ done+ port=$(cat "$work/port") &&+ "$H" send-wide-status "http://127.0.0.1:$port" 100000 &&+ wait "$server" &&+ python3 - "$work/body" <<'\''PY'\'' &&+import json, sys+rl = json.load(open(sys.argv[1]))["resourceLogs"][0]+record = rl["scopeLogs"][0]["logRecords"][0]+body = {item["key"]: item["value"] for item in record["body"]["kvlistValue"]["values"]}+values = body["conditions"]["arrayValue"]["values"]+assert len(values) == 100000 and values[-1] == {"intValue": "1"}, len(values)+PY+ rm -rf "$work"+'+test_expect_success 'status --json --otel keeps JSON output available' 'work=$(mktemp -d)python3 helper/otel-mock.py success "$work/port" "$work/body" \diff --git a/testing/fuzz/fuzz-otel.c b/testing/fuzz/fuzz-otel.cindex 41bf4807..6bc9e8c8 100644--- a/testing/fuzz/fuzz-otel.c+++ b/testing/fuzz/fuzz-otel.c@@ -89,10 +89,10 @@ static size_t deepest_nesting(const char *s, size_t len)int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){- struct json_emit e = {};+ struct json_emit e = { .max = JSON_MAX_BYTES };struct otel_response r = {};struct otel_state st = {};- size_t off, n, cap;+ size_t off, n;char *body, *line;int ret;@@ -120,27 +120,25 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)check_partial_success(OTEL_LOG, body, size);check_partial_success(OTEL_METRIC, body, size);- cap = generic_log_capacity(size);- e.buf = xmalloc(cap);- if (e.buf) {- e.cap = cap;- json_obj_open(&e);- /*- * What the conversion writes reads back as JSON. An input level- * takes up to four in the output, which can so nest deeper than- * the walk goes; such output must read back up to that depth.- */- if (!json_anyvalue_from_document(&e, body, size, false)) {- json_obj_close(&e);- if (!e.err) {- ret = json_walk(e.buf, e.len, &any_value, NULL);- if (deepest_nesting(e.buf, e.len) <= JSON_MAX_DEPTH ?- ret : ret != -E2BIG)- abort();- }+ json_obj_open(&e);+ /*+ * What the conversion writes reads back as JSON, in a buffer that+ * grows for it. An input level takes up to four in the output,+ * which can so nest deeper than the walk goes; such output must read+ * back up to that depth.+ */+ if (!json_anyvalue_from_document(&e, body, size, false)) {+ json_obj_close(&e);+ if (e.err == -ENOSPC)+ abort();+ if (!e.err) {+ ret = json_walk(e.buf, e.len, &any_value, NULL);+ if (deepest_nesting(e.buf, e.len) <= JSON_MAX_DEPTH ?+ ret : ret != -E2BIG)+ abort();}- xfree(e.buf, cap);}+ xfree(e.buf, e.cap);if (!memchr(data, '\0', size)) {if (!add_configured_headers(&st, body)) {diff --git a/testing/helper/test-otel-map.c b/testing/helper/test-otel-map.cindex cb889ac2..ba44902d 100644--- a/testing/helper/test-otel-map.c+++ b/testing/helper/test-otel-map.c@@ -423,6 +423,42 @@ static int send_status(const char *url, const char *status)body, (size_t)n) != 0;}+/*+ * A status body of a list of count ones, which the AnyValue form spells+ * in some eight times the bytes.+ */+static int send_wide_status(const char *url, const char *count)+{+ static const char head[] =+ "{\"schema\":1,\"status\":\"ok\",\"conditions\":[";+ struct otel_opts opts = {+ .endpoint = url,+ .retries = 0,+ };+ size_t n = strtoul(count, NULL, 10), cap, len, i;+ char *body;+ int ret;++ cap = sizeof(head) + 2 * n + 2;+ body = xmalloc(cap);+ if (!body)+ return 1;+ memcpy(body, head, sizeof(head) - 1);+ len = sizeof(head) - 1;+ for (i = 0; i < n; i++) {+ if (i)+ body[len++] = ',';+ body[len++] = '1';+ }+ body[len++] = ']';+ body[len++] = '}';+ ret = otel_send_log_json(&opts, "shirudo.keepalive",+ "shirudo.keepalive", 9, "INFO", "ok",+ "test-version", 0, body, len);+ xfree(body, cap);+ return ret != 0;+}+static int send_metric_env(void){struct otel_opts opts = {@@ -665,6 +701,8 @@ int main(int argc, char **argv)return send_metric_env();if (argc == 4 && strcmp(argv[1], "send-status") == 0)return send_status(argv[2], argv[3]);+ if (argc == 4 && strcmp(argv[1], "send-wide-status") == 0)+ return send_wide_status(argv[2], argv[3]);if (argc == 4 && strcmp(argv[1], "retry-after") == 0) {__u64 seconds;time_t now = (time_t)strtoll(argv[3], NULL, 10);diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.cindex 39997cf8..69e821e5 100644--- a/util/upatch_maps_otel.c+++ b/util/upatch_maps_otel.c@@ -1549,13 +1549,6 @@ static int otel_close(void *state)return 0;}-static size_t generic_log_capacity(size_t body_len)-{- size_t cap = size_add(size_mul(body_len, 4), 65536);-- return cap == SIZE_MAX ? 0 : cap;-}-static int build_generic_log_payload(const char *scope_name,const char *event_name,int severity_number,@@ -1566,29 +1559,18 @@ static int build_generic_log_payload(const char *scope_name,char **out, size_t *out_len,size_t *alloc_len){- struct json_emit e = {};- char *buf;+ struct json_emit e = { .max = JSON_MAX_BYTES };int ret;- size_t cap;if (!scope_name || !event_name || !severity_text || !body_json ||!out || !out_len || !alloc_len)return -EINVAL;- cap = generic_log_capacity(body_len);- if (!cap)- return -EOVERFLOW;- buf = xmalloc(cap);- if (!buf)- return -ENOMEM;- e.buf = buf;- e.cap = cap;-log_open(&e, scope_name, severity_number, severity_text, event_name);json_obj_open_key(&e, "body");ret = json_anyvalue_from_document(&e, body_json, body_len, false);json_obj_close(&e);if (ret) {- xfree(buf, cap);+ xfree(e.buf, e.cap);return ret;}json_arr_open(&e, "attributes");@@ -1601,12 +1583,12 @@ static int build_generic_log_payload(const char *scope_name,json_arr_close(&e);log_close(&e);if (e.err) {- xfree(buf, cap);+ xfree(e.buf, e.cap);return e.err;}- *out = buf;+ *out = e.buf;*out_len = e.len;- *alloc_len = cap;+ *alloc_len = e.cap;return 0;}