Skip to content

Commit

Permalink
json output: Make output more consistent (#874)
Browse files Browse the repository at this point in the history
Before, there were two JSON output formats:

    {"type": <foo>, "msg": <bar> }

and

    {"type": <foo>, "data": <bar> }

This makes parsing kind of weird as you have to keep maps of type:format
in your parsing code. That means you have to be extra vigilant about
watching for new json output types. It's true that you have to know the
type to parse `data` on a eg. a `map`, but I think the less specialized
parsing the better.

This patch makes the output consistent. Now every message looks like:

    {"type": <foo>, "data": <bar> }

This change requires that we nest some information. For example,
`attached_probes` and `lost_events` now look like:

    {"type": "attached_probes", "data": {"probes": 1}}
  • Loading branch information
danobi committed Aug 30, 2019
1 parent b3143a6 commit 9d1269b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,13 @@ void JsonOutput::map_stats(BPFtrace &bpftrace, IMap &map,

void JsonOutput::message(MessageType type, const std::string& msg, bool nl __attribute__((unused))) const
{
out_ << "{\"type\": \"" << type << "\", \"msg\": \"" << json_escape(msg) << "\"}" << std::endl;
out_ << "{\"type\": \"" << type << "\", \"data\": \"" << json_escape(msg) << "\"}" << std::endl;
}

void JsonOutput::message(MessageType type, const std::string& field, uint64_t value) const
{
out_ << "{\"type\": \"" << type << "\", \"" << field << "\": " << value << "}" << std::endl;
out_ << "{\"type\": \"" << type << "\", \"data\": " << "{\"" << field
<< "\": " << value << "}" << "}" << std::endl;
}

void JsonOutput::lost_events(uint64_t lost) const
Expand Down
12 changes: 6 additions & 6 deletions tests/runtime/json-output
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,30 @@ TIMEOUT 5

NAME printf
RUN bpftrace -f json -v -e 'BEGIN { printf("test %d", 5); exit(); }'
EXPECT ^{"type": "printf", "msg": "test 5"}$
EXPECT ^{"type": "printf", "data": "test 5"}$
TIMEOUT 5

NAME printf_escaping
RUN bpftrace -f json -v -e 'BEGIN { printf("test \r \n \t \\ \" bar"); exit(); }'
EXPECT ^{"type": "printf", "msg": "test \\r \\n \\t \\\\ \\\" bar"}$
EXPECT ^{"type": "printf", "data": "test \\r \\n \\t \\\\ \\\" bar"}$
TIMEOUT 5

NAME time
RUN bpftrace -f json -v -e 'BEGIN { time(); exit(); }'
EXPECT ^{"type": "time", "msg": "[0-9]*:[0-9]*:[0-9]*\\n"}$
EXPECT ^{"type": "time", "data": "[0-9]*:[0-9]*:[0-9]*\\n"}$
TIMEOUT 5

NAME syscall
RUN bpftrace --unsafe -v -f json -e 'BEGIN { system("echo a b c"); exit(); }'
EXPECT ^{"type": "syscall", "msg": "a b c\\n"}$
EXPECT ^{"type": "syscall", "data": "a b c\\n"}$
TIMEOUT 5

NAME join_delim
RUN bpftrace --unsafe -f json -e 'tracepoint:syscalls:sys_enter_execve { join(args->argv, ","); exit(); }' -c "/bin/echo 'A'"
EXPECT ^{"type": "join", "msg": "/bin/echo,'A'"}
EXPECT ^{"type": "join", "data": "/bin/echo,'A'"}
TIMEOUT 5

NAME cat
RUN bpftrace -v -f json -e 'BEGIN { cat("/proc/uptime"); exit(); }'
EXPECT ^{"type": "cat", "msg": "[0-9]*.[0-9]* [0-9]*.[0-9]*\\n"}$
EXPECT ^{"type": "cat", "data": "[0-9]*.[0-9]* [0-9]*.[0-9]*\\n"}$
TIMEOUT 5

0 comments on commit 9d1269b

Please sign in to comment.