Skip to content

eBPF-based security platform

otel: export maps without signed decoders

John Fastabendf781e2b6a100
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 47fbcc83..6f8c57e7 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -453,13 +453,13 @@ check_diff() {
# Every type checkpatch knows, less the ignored ones.
list_types() {
perl "$checkpatch" --list-types | awk 'NR > 2 && NF >= 2 { print $2 }' |
- sort >"$work/all"
- ignored_types | grep -v '^#' | sort >"$work/ignored"
+ LC_ALL=C sort >"$work/all"
+ ignored_types | grep -v '^#' | LC_ALL=C sort >"$work/ignored"
{
- comm -23 "$work/all" "$work/ignored"
+ LC_ALL=C comm -23 "$work/all" "$work/ignored"
printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING \
STRING_COPY
- } | sort
+ } | LC_ALL=C sort
}
check_files() {
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 5e03965c..761ace98 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -202,7 +202,7 @@ static const struct shirudo_help_item upatch_map_options[] = {
{ "--json[=<path>]", "print JSON or append it to path" },
{ "--show-full", "lift the per-map 256-entry truncation cap" },
{ "--peek", "read serializer-backed maps without draining them" },
- { "--otel", "export through OTLP using each decoder's signed signal" },
+ { "--otel", "export maps through signed decoders or automatic classification" },
{ "--otel-endpoint=<url>", "OTLP base URL (signal path is appended)" },
{ "--tenant-id=<id>", "add X-Tenant-ID to OTLP requests" },
{ "--otel-insecure", "allow cleartext remote OTLP or disable HTTPS verification" },
@@ -2878,7 +2878,10 @@ static int export_one_map(const char *upatch, const char *map,
__u32 decoder_signal;
rc = upatch_decode_map_signal(&v, &decoder_signal);
- if (!rc)
+ if (rc == -ENODATA)
+ rc = shirudo_upatch_maps_otel_send(&v, SHIRUDO_OTEL_AUTO,
+ otel_opts);
+ else if (!rc)
rc = shirudo_upatch_maps_otel_send_decoded(&v, decoder_signal,
otel_opts);
if (rc) {
diff --git a/testing/otel/t1117-otel-serialized-metric.sh b/testing/otel/t1117-otel-serialized-metric.sh
index 8ac1f2d4..0513aec1 100644
--- a/testing/otel/t1117-otel-serialized-metric.sh
+++ b/testing/otel/t1117-otel-serialized-metric.sh
@@ -69,11 +69,36 @@ while len(bodies) < 4:
server.handle_request()
PY
+cat >"$WORK/service.py" <<'PY'
+import http.server, json, pathlib, sys, time
+port_file, body_file = map(pathlib.Path, sys.argv[1:3])
+requests = []
+class Handler(http.server.BaseHTTPRequestHandler):
+ def do_POST(self):
+ payload = self.rfile.read(int(self.headers["Content-Length"]))
+ requests.append((self.path, json.loads(payload)))
+ body_file.write_text(json.dumps(requests))
+ payload = b"{}"
+ self.send_response(200)
+ self.send_header("Content-Length", str(len(payload)))
+ self.end_headers()
+ self.wfile.write(payload)
+ def log_message(self, *args): pass
+server = http.server.HTTPServer(("127.0.0.1", 0), Handler)
+server.timeout = 1
+port_file.write_text(str(server.server_port))
+deadline = time.monotonic() + 30
+while len(requests) < 2 and time.monotonic() < deadline:
+ server.handle_request()
+if len(requests) != 2:
+ raise SystemExit("expected OTLP log and metric requests")
+PY
+
cat >"$WORK/check-service.py" <<'PY'
import json, sys
-meta = json.load(open(sys.argv[1], encoding="utf-8"))
-doc = json.load(open(sys.argv[2], encoding="utf-8"))
-assert meta["path"] == "/v1/metrics"
+requests = json.load(open(sys.argv[1], encoding="utf-8"))
+assert sorted(path for path, _ in requests) == ["/v1/logs", "/v1/metrics"]
+doc = next(body for path, body in requests if path == "/v1/metrics")
metrics = {m["name"]: m for m in doc["resourceMetrics"][0]["scopeMetrics"][0]["metrics"]}
assert metrics["shirudo.serialized.packets"]["sum"]["dataPoints"][0]["asInt"] == "10"
assert metrics["shirudo.serialized.bytes"]["sum"]["dataPoints"][0]["asInt"] == "150"
@@ -90,23 +115,25 @@ test_expect_success 'serialized multi-page metrics preserve peek and drain inter
"$SHIRUDO_BIN" upatch map "$NAME" counters --otel --otel-endpoint="http://127.0.0.1:$port" --peek &&
"$SHIRUDO_BIN" upatch map "$NAME" counters --otel --otel-endpoint="http://127.0.0.1:$port" &&
"$SHIRUDO_BIN" upatch map "$NAME" counters --otel --otel-endpoint="http://127.0.0.1:$port" &&
- wait "$SERVER"; SERVER="";
+ wait "$SERVER" || return 1;
+ SERVER="";
python3 "$WORK/check-batch.py" "$WORK/bodies.json" &&
"$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&
- "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1;
- python3 ../helper/otel-mock.py success "$WORK/service.port" "$WORK/service.body" "$WORK/service.meta" &
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 || return 1;
+ python3 "$WORK/service.py" "$WORK/service.port" "$WORK/service.body" &
SERVER=$!;
i=0; while test ! -s "$WORK/service.port" && test "$i" -lt 100; do i=$((i+1)); sleep .1; done;
test -s "$WORK/service.port" &&
port=$(cat "$WORK/service.port") &&
printf "%s\n" "map_export.otel_endpoint = http://127.0.0.1:$port" "map_export.otel_insecure = true" "map_export.otel_retries = 0" >"$CONFIG" &&
if ! systemctl start shirudo-map-export.service; then
- systemctl status shirudo-map-export.service --no-pager || true
- journalctl -u shirudo-map-export.service --no-pager -n 100 || true
+ systemctl status shirudo-map-export.service --no-pager >&2 || true
+ journalctl -u shirudo-map-export.service --no-pager -n 100 >&2 || true
return 1
- fi && wait "$SERVER"; SERVER="";
+ fi && wait "$SERVER" || return 1;
+ SERVER="";
systemctl show shirudo-map-export.service -p ExecMainStatus --value | grep -qx 0 &&
- python3 "$WORK/check-service.py" "$WORK/service.meta" "$WORK/service.body"
+ python3 "$WORK/check-service.py" "$WORK/service.body"
'
test_done
diff --git a/util/upatch_decode.c b/util/upatch_decode.c
index 4904c491..87724bc6 100644
--- a/util/upatch_decode.c
+++ b/util/upatch_decode.c
@@ -1267,7 +1267,7 @@ static int decode_map_file(const char *path, const struct shirudo_map_view *v,
rc = upatch_open(path, &u);
if (rc)
return rc == -ENOENT ? -ENOENT : rc;
- rc = -ENOENT;
+ rc = -ENODATA;
for (i = 0; i < u.nr_decoders; i++) {
struct upatch_decoder_view d;
@@ -1298,7 +1298,7 @@ int upatch_decode_map_signal(const struct shirudo_map_view *v,
rc = upatch_open(path, &u);
if (rc)
return rc == -ENOENT ? -ENOENT : rc;
- rc = -ENOENT;
+ rc = -ENODATA;
for (i = 0; i < u.nr_decoders; i++) {
struct upatch_decoder_view d;
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 8e90d1ed..5880a061 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -619,8 +619,8 @@ static void u64_decimal(__u64 value, char out[32])
snprintf(out, 32, "%llu", value);
}
-static __attribute__((unused)) void emit_log_map_data(struct shirudo_json_emit *e,
- const struct shirudo_map_view *v)
+static void emit_log_map_data(struct shirudo_json_emit *e,
+ const struct shirudo_map_view *v)
{
__u32 i, j;
@@ -746,7 +746,7 @@ static int build_log_payload(const char *path,
json_arr_open(&e, "resourceLogs");
json_obj_open(&e);
json_obj_open_key(&e, "resource");
- emit_resource_attributes(&e);
+ emit_resource_attributes(&e);
json_obj_close(&e);
json_arr_open(&e, "scopeLogs");
json_obj_open(&e);
@@ -761,15 +761,23 @@ static int build_log_payload(const char *path,
json_kv_int(&e, "severityNumber", 9);
json_kv_string(&e, "severityText", "INFO");
json_kv_string(&e, "eventName", "shirudo.bpf.map.snapshot");
- decode_rc = path ? upatch_decode_map_file_otel(path, v,
- &decoded, &decoded_len) :
- upatch_decode_map_otel(v, &decoded, &decoded_len);
+ decode_rc = path ? upatch_decode_map_file_otel(path, v,
+ &decoded, &decoded_len) :
+ upatch_decode_map_otel(v, &decoded, &decoded_len);
if (!decode_rc && record_max && decoded_len > record_max) {
xfree(decoded, decoded_len + 1);
decode_rc = -E2BIG;
}
if (decode_rc == 0) {
json_kv_raw(&e, "body", decoded, decoded_len);
+ } else if (decode_rc == -ENODATA) {
+ json_obj_open_key(&e, "body");
+ json_obj_open_key(&e, "kvlistValue");
+ json_arr_open(&e, "values");
+ emit_log_map_data(&e, v);
+ json_arr_close(&e);
+ json_obj_close(&e);
+ json_obj_close(&e);
} else {
xfree(buf, SHIRUDO_JSON_MAX_BYTES);
return decode_rc;