Skip to content

Commit

Permalink
Send stats through socket rather than through logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryblakley committed Apr 29, 2016
1 parent 0db7fe2 commit c24982b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 173 deletions.
68 changes: 56 additions & 12 deletions src/fusedav-statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

// e.g. fusedav.valhallayolo1b.server-104_130_221_144.exceeded-time-small-GET-latency (82 characters)
#define STATS_MSG_LEN 128
// Needs to hold the prefix for a stat, e.g. fusedav.valhalla2.server-
#define STATS_PREFIX_LEN 64
// Needs to hold the prefix for a stat, e.g. fusedav
#define STATS_PREFIX_LEN 32

/* Socket and server for function sendto */
/* server is a sockaddr_storage so it can hold either IPV4 or IPV6 */
Expand Down Expand Up @@ -188,8 +188,7 @@ int stats_init(const char *domain, const char *port) {

memset(&server, 0, sizeof(server));
// stats_prefix is, after being set here, an unchanging global string
// fusedav keeps many stats by filesystem cluster, so include it
snprintf(server.stats_prefix, STATS_PREFIX_LEN, "fusedav.%s.server-", get_filesystem_cluster());
snprintf(server.stats_prefix, STATS_PREFIX_LEN, "fusedav");

if (set_addr(domain, port, &server)) {
// FAILURE; pass it on
Expand Down Expand Up @@ -238,49 +237,94 @@ static int stats_send(const char *statmsg) {
return 0;
}

static int compose_message(const char *statname, const signed int value, char *type, char *msg) {
return snprintf(msg, STATS_MSG_LEN, "%s%s.%s:%d|%s\n", server.stats_prefix, get_nodeaddr(), statname, value, type);
static int compose_message(const char *statname, const signed int value, char *type, char *msg, const char *cluster, const char *node) {
if (node) {
return snprintf(msg, STATS_MSG_LEN, "%s.%s.server-%s.%s:%d|%s\n",
server.stats_prefix, cluster, node, statname, value, type);
} else if (cluster) {
return snprintf(msg, STATS_MSG_LEN, "%s.%s.%s:%d|%s\n",
server.stats_prefix, cluster, statname, value, type);
} else {
return snprintf(msg, STATS_MSG_LEN, "%s.%s:%d|%s\n",
server.stats_prefix, statname, value, type);
}

}

int stats_counter(const char *statname, const int value) {
static int stats_counter_common(const char *statname, const int value, const char *cluster, const char *node) {
int error;
int res;
char msg[STATS_MSG_LEN];
char type[] = "c";
res = compose_message(statname, value, type, msg);
res = compose_message(statname, value, type, msg, cluster, node);
if (res < 0) {
return res;
}
error = stats_send(msg);
return error;
}

int stats_gauge(const char *statname, const int value) {
int stats_counter(const char *statname, const int value) {
return stats_counter_common(statname, value, get_filesystem_cluster(), get_nodeaddr());
}

int stats_counter_cluster(const char *statname, const int value) {
return stats_counter_common(statname, value, get_filesystem_cluster(), NULL);
}

int stats_counter_local(const char *statname, const int value) {
return stats_counter_common(statname, value, NULL, NULL);
}

static int stats_gauge_common(const char *statname, const int value, const char *cluster, const char *node) {
int error;
int res;
char msg[STATS_MSG_LEN];
char type[] = "g";
res = compose_message(statname, value, type, msg);
res = compose_message(statname, value, type, msg, cluster, node);
if (res < 0) {
return res;
}
error = stats_send(msg);
return error;
}

int stats_timer(const char *statname, const int value) {
int stats_gauge(const char *statname, const int value) {
return stats_gauge_common(statname, value, get_filesystem_cluster(), get_nodeaddr());
}

int stats_gauge_cluster(const char *statname, const int value) {
return stats_gauge_common(statname, value, get_filesystem_cluster(), NULL);
}

int stats_gauge_local(const char *statname, const int value) {
return stats_gauge_common(statname, value, NULL, NULL);
}

static int stats_timer_common(const char *statname, const int value, const char *cluster, const char *node) {
int error;
int res;
char msg[STATS_MSG_LEN];
char type[] = "ms";
res = compose_message(statname, value, type, msg);
res = compose_message(statname, value, type, msg, cluster, node);
if (res < 0) {
return res;
}
error = stats_send(msg);
return error;
}

int stats_timer(const char *statname, const int value) {
return stats_timer_common(statname, value, get_filesystem_cluster(), get_nodeaddr());
}

int stats_timer_cluster(const char *statname, const int value) {
return stats_timer_common(statname, value, get_filesystem_cluster(), NULL);
}

int stats_timer_local(const char *statname, const int value) {
return stats_timer_common(statname, value, NULL, NULL);
}

/* For reference:
*
Expand Down
6 changes: 6 additions & 0 deletions src/fusedav-statsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
int stats_init(const char *domain, const char *port);
int stats_close(void);
int stats_counter(const char *statname, const int value);
int stats_counter_cluster(const char *statname, const int value);
int stats_counter_local(const char *statname, const int value);
int stats_gauge(const char *statname, const int value);
int stats_gauge_cluster(const char *statname, const int value);
int stats_gauge_local(const char *statname, const int value);
int stats_timer(const char *statname, const int value);
int stats_timer_cluster(const char *statname, const int value);
int stats_timer_local(const char *statname, const int value);

#endif
4 changes: 2 additions & 2 deletions src/fusedav.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ static void get_stat(const char *path, struct stat *stbuf, GError **gerr) {
if (parent_children_update_ts < (time(NULL) - STAT_CACHE_NEGATIVE_TTL)) {
GError *subgerr = NULL;

stats_counter("propfind-nonnegative-cache", 1);
stats_counter_local("propfind-nonnegative-cache", 1);
log_print(LOG_INFO, SECTION_FUSEDAV_STAT, "get_stat: Calling update_directory: %s; attempt_progressive_update will be %d",
parent_path, (parent_children_update_ts > 0));
// If parent_children_update_ts is 0, there are no entries for updated_children in statcache
Expand All @@ -722,7 +722,7 @@ static void get_stat(const char *path, struct stat *stbuf, GError **gerr) {
goto fail;
}
} else {
stats_counter("propfind-negative-cache", 1);
stats_counter_local("propfind-negative-cache", 1);
BUMP(propfind_negative_cache);
}

Expand Down
2 changes: 1 addition & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void set_dynamic_logging(void) {
else {
dynamic_logging_start = now.tv_sec;
log_print(LOG_NOTICE, SECTION_FUSEDAV_DEFAULT,
"Setting dynamic_logging for %lu seconds. fusedav.dynamic_logging:1|c", dynamic_logging_duration);
"Setting dynamic_logging for %lu seconds.", dynamic_logging_duration);
LOG_DYNAMIC = LOG_INFO - 1;
}
}
Expand Down
Loading

0 comments on commit c24982b

Please sign in to comment.