Skip to content

Commit

Permalink
Merge pull request #981 from criblio/feat-proc-metrics
Browse files Browse the repository at this point in the history
Move proc metrics into report and reflect changes in start.msg schema from #975
  • Loading branch information
jrcheli authored Jun 7, 2022
2 parents 8587d84 + 5772300 commit 884efd0
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 61 deletions.
32 changes: 19 additions & 13 deletions cli/libscope/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ type ScopeCriblConfig struct {

// ScopeMetricConfig represents how to output metrics
type ScopeMetricConfig struct {
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"`
Format ScopeOutputFormat `mapstructure:"format" json:"format" yaml:"format"`
Transport ScopeTransport `mapstructure:"transport,omitempty" json:"transport,omitempty" yaml:"transport,omitempty"`
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"`
Format ScopeOutputFormat `mapstructure:"format" json:"format" yaml:"format"`
Transport ScopeTransport `mapstructure:"transport,omitempty" json:"transport,omitempty" yaml:"transport,omitempty"`
Watch []ScopeMetricWatchConfig `mapstructure:"watch" json:"watch" yaml:"watch"`
}

// ScopeEventConfig represents how to output events
type ScopeEventConfig struct {
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"`
Format ScopeOutputFormat `mapstructure:"format" json:"format" yaml:"format"`
Transport ScopeTransport `mapstructure:"transport,omitempty" json:"transport,omitempty" yaml:"transport,omitempty"`
Watch []ScopeWatchConfig `mapstructure:"watch" json:"watch" yaml:"watch"`
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"`
Format ScopeOutputFormat `mapstructure:"format" json:"format" yaml:"format"`
Transport ScopeTransport `mapstructure:"transport,omitempty" json:"transport,omitempty" yaml:"transport,omitempty"`
Watch []ScopeEventWatchConfig `mapstructure:"watch" json:"watch" yaml:"watch"`
}

// ScopePayloadConfig represents how to capture payloads
Expand All @@ -37,12 +38,17 @@ type ScopePayloadConfig struct {
Dir string `mapstructure:"dir" json:"dir" yaml:"dir"`
}

// ScopeWatchConfig represents a watch configuration
type ScopeWatchConfig struct {
WatchType string `mapstructure:"type" json:"type" yaml:"type"`
Name string `mapstructure:"name" json:"name" yaml:"name"`
Field string `mapstructure:"field,omitempty" json:"field,omitempty" yaml:"field,omitempty"`
Value string `mapstructure:"value" json:"value" yaml:"value"`
// ScopeMetricWatchConfig represents a metric watch configuration
type ScopeMetricWatchConfig struct {
WatchType string `mapstructure:"type" json:"type" yaml:"type"`
}

// ScopeEventWatchConfig represents a event watch configuration
type ScopeEventWatchConfig struct {
WatchType string `mapstructure:"type" json:"type" yaml:"type"`
Name string `mapstructure:"name" json:"name" yaml:"name"`
Field string `mapstructure:"field,omitempty" json:"field,omitempty" yaml:"field,omitempty"`
Value string `mapstructure:"value" json:"value" yaml:"value"`
AllowBinary bool `mapstructure:"allowbinary,omitempty" json:"allowbinary,omitempty" yaml:"allowbinary,omitempty"`
}

Expand Down
22 changes: 21 additions & 1 deletion cli/run/scopeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ func (c *Config) SetDefault() error {
Path: filepath.Join(c.WorkDir, "metrics.json"),
Buffering: "line",
},
Watch: []libscope.ScopeMetricWatchConfig{
{
WatchType: "fs",
},
{
WatchType: "net",
},
{
WatchType: "http",
},
{
WatchType: "dns",
},
{
WatchType: "process",
},
{
WatchType: "statsd",
},
},
},
Event: libscope.ScopeEventConfig{
Enable: true,
Expand All @@ -42,7 +62,7 @@ func (c *Config) SetDefault() error {
Path: filepath.Join(c.WorkDir, "events.json"),
Buffering: "line",
},
Watch: []libscope.ScopeWatchConfig{
Watch: []libscope.ScopeEventWatchConfig{
{
WatchType: "file",
Name: scopeLogRegex(),
Expand Down
7 changes: 7 additions & 0 deletions cli/run/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ metric:
enable: false
validateserver: false
cacertpath: ""
watch:
- type: fs
- type: net
- type: http
- type: dns
- type: process
- type: statsd
event:
enable: true
format:
Expand Down
32 changes: 31 additions & 1 deletion docs/schemas/definitions/data.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,37 @@
"type": "string",
"const": "net"
},
"watch_type_metric" : {
"watch_type_metric_fs" : {
"title": "type",
"description": "TBD type",
"type": "string",
"const": "fs"
},
"watch_type_metric_net" : {
"title": "type",
"description": "TBD type",
"type": "string",
"const": "net"
},
"watch_type_metric_http" : {
"title": "type",
"description": "TBD type",
"type": "string",
"const": "http"
},
"watch_type_metric_dns" : {
"title": "type",
"description": "TBD type",
"type": "string",
"const": "dns"
},
"watch_type_metric_proc" : {
"title": "type",
"description": "TBD type",
"type": "string",
"const": "process"
},
"watch_type_metric_statsd" : {
"title": "type",
"description": "TBD type",
"type": "string",
Expand Down
45 changes: 43 additions & 2 deletions docs/schemas/event_start_msg.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "object",
"title": "AppScope Start message",
"description": "TBD Structure of the start message",
"examples": [{"format":"ndjson","info":{"process":{"libscopever":"v1.1.0","pid":35673,"ppid":3390,"gid":1000,"groupname":"test_user","uid":1000,"username":"test_user","hostname":"test_user","procname":"ls","cmd":"ls --color=auto","id":"test_user-ls-ls --color=auto"},"configuration":{"current":{"metric":{"enable":"true","transport":{"type":"udp","host":"127.0.0.1","port":"8125","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"statsd","statsdprefix":"","statsdmaxlen":512,"verbosity":4},"watch":[{"type":"statsd"}]},"libscope":{"log":{"level":"info","transport":{"type":"file","path":"/tmp/scope.log","buffering":"line"}},"configevent":"true","summaryperiod":10,"commanddir":"/tmp"},"event":{"enable":"true","transport":{"type":"tcp","host":"127.0.0.1","port":"9109","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"ndjson","maxeventpersec":10000,"enhancefs":"true"},"watch":[{"type":"file","name":"(\\/logs?\\/)|(\\.log$)|(\\.log[.\\d])","field":".*","value":".*"},{"type":"console","name":"(stdout)|(stderr)","field":".*","value":".*","allowbinary":"true"},{"type":"http","name":".*","field":".*","value":".*","headers":[]},{"type":"net","name":".*","field":".*","value":".*"},{"type":"fs","name":".*","field":".*","value":".*"},{"type":"dns","name":".*","field":".*","value":".*"}]},"payload":{"enable":"false","dir":"/tmp"},"tags":{},"protocol":[],"cribl":{"enable":"false","transport":{"type":"edge"},"authtoken":""}}},"environment":{}}}],
"examples": [{"format":"ndjson","info":{"process":{"libscopever":"v1.1.0","pid":35673,"ppid":3390,"gid":1000,"groupname":"test_user","uid":1000,"username":"test_user","hostname":"test_user","procname":"ls","cmd":"ls --color=auto","id":"test_user-ls-ls --color=auto"},"configuration":{"current":{"metric":{"enable":"true","transport":{"type":"udp","host":"127.0.0.1","port":"8125","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"statsd","statsdprefix":"","statsdmaxlen":512,"verbosity":4},"watch":[{"type":"fs"},{"type":"net"},{"type":"http"},{"type":"dns"},{"type":"process"},{"type":"statsd"}]},"libscope":{"log":{"level":"info","transport":{"type":"file","path":"/tmp/scope.log","buffering":"line"}},"configevent":"true","summaryperiod":10,"commanddir":"/tmp"},"event":{"enable":"true","transport":{"type":"tcp","host":"127.0.0.1","port":"9109","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"ndjson","maxeventpersec":10000,"enhancefs":"true"},"watch":[{"type":"file","name":"(\\/logs?\\/)|(\\.log$)|(\\.log[.\\d])","field":".*","value":".*"},{"type":"console","name":"(stdout)|(stderr)","field":".*","value":".*","allowbinary":"true"},{"type":"http","name":".*","field":".*","value":".*","headers":[]},{"type":"net","name":".*","field":".*","value":".*"},{"type":"fs","name":".*","field":".*","value":".*"},{"type":"dns","name":".*","field":".*","value":".*"}]},"payload":{"enable":"false","dir":"/tmp"},"tags":{},"protocol":[],"cribl":{"enable":"false","transport":{"type":"edge"},"authtoken":""}}},"environment":{}}}],
"required": [
"format",
"info"
Expand Down Expand Up @@ -163,9 +163,50 @@
"type": "array",
"items": [
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_fs"
}
}
},
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_net"
}
}
},
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_http"
}
}
},
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_dns"
}
}
},
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_proc"
}
}
},
{
"type": "object",
"properties": {
"type": {
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric"
"$ref": "definitions/data.schema.json#/$defs/watch_type_metric_statsd"
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions os/linux/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,17 @@ osGetGroupName(unsigned gid)
return scope_strdup(grp.gr_name);
return NULL;
}

// Return process specific CPU usage in microseconds
long long
osGetProcCPU(void) {
struct rusage ruse;

if (scope_getrusage(RUSAGE_SELF, &ruse) != 0) {
return -1;
}

return
(((long long)ruse.ru_utime.tv_sec + (long long)ruse.ru_stime.tv_sec) * 1000 * 1000) +
((long long)ruse.ru_utime.tv_usec + (long long)ruse.ru_stime.tv_usec);
}
1 change: 1 addition & 0 deletions os/linux/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ extern char *osGetFileMode(mode_t);
extern int osNeedsConnect(int);
extern char *osGetUserName(unsigned);
extern char *osGetGroupName(unsigned);
extern long long osGetProcCPU(void);

#endif //__OS_H__
5 changes: 5 additions & 0 deletions os/macOS/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,8 @@ osGetGroupName(unsigned gid)
{
return NULL;
}

long long
osGetProcCPU(void) {
return -1;
}
1 change: 1 addition & 0 deletions os/macOS/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ extern char *osGetFileMode(mode_t);
extern int osNeedsConnect(int);
extern const char *osGetUserName(unsigned);
extern const char *osGetGroupName(unsigned);
extern long long osGetProcCPU(void);
30 changes: 29 additions & 1 deletion src/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "utils.h"
#include "runtimecfg.h"
#include "cfg.h"
#include "os.h"
#include "scopestdlib.h"

#ifndef AF_NETLINK
Expand Down Expand Up @@ -1769,11 +1770,19 @@ doDNSMetricName(metric_t type, net_info *net)
}

void
doProcMetric(metric_t type, long long measurement)
doProcMetric(metric_t type)
{
long long measurement;
switch (type) {
case PROC_CPU:
{
static long long cpuState = 0;
long long cpu = osGetProcCPU();
if (cpu == -1) {
return;
}
measurement = cpu - cpuState;
cpuState = cpu;
{
event_field_t fields[] = {
PROC_FIELD(g_proc.procname),
Expand Down Expand Up @@ -1811,6 +1820,10 @@ doProcMetric(metric_t type, long long measurement)

case PROC_MEM:
{
measurement = osGetProcMemory(g_proc.pid);
if (measurement == -1) {
return;
}
event_field_t fields[] = {
PROC_FIELD(g_proc.procname),
PID_FIELD(g_proc.pid),
Expand All @@ -1825,6 +1838,11 @@ doProcMetric(metric_t type, long long measurement)

case PROC_THREAD:
{
measurement = osGetNumThreads(g_proc.pid);
if (measurement == -1) {
return;
}

event_field_t fields[] = {
PROC_FIELD(g_proc.procname),
PID_FIELD(g_proc.pid),
Expand All @@ -1839,6 +1857,11 @@ doProcMetric(metric_t type, long long measurement)

case PROC_FD:
{
measurement = osGetNumFds(g_proc.pid);
if (measurement == -1) {
return;
}

event_field_t fields[] = {
PROC_FIELD(g_proc.procname),
PID_FIELD(g_proc.pid),
Expand All @@ -1853,6 +1876,11 @@ doProcMetric(metric_t type, long long measurement)

case PROC_CHILD:
{
measurement = osGetNumChildProcs(g_proc.pid);
if (measurement < 0) {
measurement = 0;
}

event_field_t fields[] = {
PROC_FIELD(g_proc.procname),
PID_FIELD(g_proc.pid),
Expand Down
2 changes: 1 addition & 1 deletion src/report.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extern ctl_t *g_ctl;
void initReporting(void);
void setReportingInterval(int);
void doErrorMetric(metric_t, control_type_t, const char *, const char *, void *);
void doProcMetric(metric_t, long long);
void doProcMetric(metric_t);
void doStatMetric(const char *, const char *, void *);
void doTotal(metric_t);
void doTotalDuration(metric_t);
Expand Down
46 changes: 5 additions & 41 deletions src/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,20 +820,6 @@ stopTimer(void)
threadNow(0);
}

// Return process specific CPU usage in microseconds
static long long
doGetProcCPU() {
struct rusage ruse;

if (scope_getrusage(RUSAGE_SELF, &ruse) != 0) {
return (long long)-1;
}

return
(((long long)ruse.ru_utime.tv_sec + (long long)ruse.ru_stime.tv_sec) * 1000 * 1000) +
((long long)ruse.ru_utime.tv_usec + (long long)ruse.ru_stime.tv_usec);
}

static void
setProcId(proc_id_t *proc)
{
Expand Down Expand Up @@ -904,41 +890,19 @@ doReset()
static void
reportPeriodicStuff(void)
{
long mem;
int nthread, nfds, children;
long long cpu = 0;
static long long cpuState = 0;

// aggregate and send http metrics
doHttpAgg();

// empty the event queues
doEvent();
doPayload();

// TODO: move the code inside if below to report.c
if (cfgMtcWatchEnable(g_cfg.staticfg, CFG_MTC_PROC)) {
// We report CPU time for this period.
cpu = doGetProcCPU();
if (cpu != -1) {
doProcMetric(PROC_CPU, cpu - cpuState);
cpuState = cpu;
}

mem = osGetProcMemory(g_proc.pid);
if (mem != -1) doProcMetric(PROC_MEM, mem);

nthread = osGetNumThreads(g_proc.pid);
if (nthread != -1) doProcMetric(PROC_THREAD, nthread);

nfds = osGetNumFds(g_proc.pid);
if (nfds != -1) doProcMetric(PROC_FD, nfds);

children = osGetNumChildProcs(g_proc.pid);
if (children < 0) {
children = 0;
}
doProcMetric(PROC_CHILD, children);
doProcMetric(PROC_CPU);
doProcMetric(PROC_MEM);
doProcMetric(PROC_THREAD);
doProcMetric(PROC_FD);
doProcMetric(PROC_CHILD);
}

// report totals (not by file descriptor/socket descriptor)
Expand Down
Loading

0 comments on commit 884efd0

Please sign in to comment.