Ottomata has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/95473


Change subject: Writing JSON statistics to log file rather than syslog or stderr
......................................................................

Writing JSON statistics to log file rather than syslog or stderr

syslog imposes a default limit of 2K on log lines.  Some
of these JSON formatted statistics lines can be larger than
that.  I am seeing lines over 3K.

Rather than try to use syslog for statistics reporting, we now
write directly to a log file.  The lines that are written there
are now fully valid JSON.  Each line may be parsed as its own
JSON object, without any extra non-JSON metadata prepending it.
(syslog prepends facility, timestamp, etc.)

The logfile defaults to /var/cache/varnishkafka.stats.json, and
is configurable via the log.statistics.file property.

Change-Id: I501fe6b421325ae98e21f910a7ae367762075371
---
M config.c
M varnishkafka.c
M varnishkafka.h
3 files changed, 40 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/operations/software/varnish/varnishkafka 
refs/changes/73/95473/1

diff --git a/config.c b/config.c
index 6b10b41..2a3d70d 100644
--- a/config.c
+++ b/config.c
@@ -161,6 +161,8 @@
                        conf.log_to &= ~VK_LOG_SYSLOG;
        } else if (!strcmp(name, "log.kafka.msg.error"))
                conf.log_kafka_msg_error = conf_tof(val);
+       else if (!strcmp(name, "log.statistics.file"))
+               conf.stats_file = strdup(val);
        else if (!strcmp(name, "log.statistics.interval"))
                conf.stats_interval = atoi(val);
        else if (!strcmp(name, "log.rate.max"))
diff --git a/varnishkafka.c b/varnishkafka.c
index 32689eb..bf3c94b 100644
--- a/varnishkafka.c
+++ b/varnishkafka.c
@@ -73,6 +73,8 @@
        [FMT_CONF_KEY]  = "Key"
 };
 
+/* JSON statistics file pointer, configured via log.statistics.file */
+FILE *stats_fp = 0;
 
 /**
  * Logline cache
@@ -102,8 +104,7 @@
 } cnt;
 
 static void print_stats (void) {
-       vk_log("\"stats\"", LOG_INFO,
-              "{ "
+       vk_log_stats("{ \"varnishkafka\": { "
               "\"tx\":%"PRIu64", "
               "\"txerr\":%"PRIu64", "
               "\"kafka_drerr\":%"PRIu64", "
@@ -112,7 +113,7 @@
               "\"scratch_tmpbufs\":%"PRIu64", "
               "\"lp_curr\":%i, "
               "\"seq\":%"PRIu64" "
-              "}",
+              "} }",
               cnt.tx,
               cnt.txerr,
               cnt.kafka_drerr,
@@ -1357,7 +1358,7 @@
  */
 static int kafka_stats_cb (rd_kafka_t *rk, char *json, size_t json_len,
                            void *opaque) {
-       vk_log("\"kafkastats\"", LOG_INFO, "%.*s", (int)json_len, json);
+       vk_log_stats("{ \"kafka\": %s } ", json);
        return 0;
 }
 
@@ -1761,6 +1762,23 @@
                fprintf(stderr, "%%%i %s: %s\n", level, facility, buf);
 }
 
+/**
+ * Appends a formatted string to the stats_fp file.
+ * stats_fp is configured using the log.statistics.file property.
+ */
+void vk_log_stats(const char *fmt, ...) {
+       va_list ap;
+       static char buf[8192];
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
+
+       /* If stats_fp is 0, vk_log_stats() shouldn't have been called */
+       if (stats_fp)
+               fprintf(stats_fp, "%s\n", buf);
+}
+
 
 /**
  * Termination signal handler.
@@ -1827,6 +1845,7 @@
        conf.loglines_hmax  = 5;
        conf.scratch_size   = 4096;
        conf.stats_interval = 60;
+       conf.stats_file     = "/var/cache/varnishkafka/stats.json";
        conf.log_kafka_msg_error = 1;
        conf.rk_conf = rd_kafka_conf_new();
        rd_kafka_conf_set(conf.rk_conf, "client.id", "varnishkafka", NULL, 0);
@@ -1893,6 +1912,12 @@
 
        /* Set up statistics gathering in librdkafka, if enabled. */
        if (conf.stats_interval) {
+               if (!(stats_fp = fopen(conf.stats_file, "a+"))) {
+                       fprintf(stderr, "Failed to open statistics log file %s: 
%s\n",
+                               conf.stats_file, strerror(errno));
+                       exit(1);
+               }
+
                char tmp[30];
                snprintf(tmp, sizeof(tmp), "%i", conf.stats_interval*1000);
                rd_kafka_conf_set_stats_cb(conf.rk_conf, kafka_stats_cb);
@@ -2015,6 +2040,12 @@
        loglines_term();
        print_stats();
 
+       /* if this was set, stats_fp should be open, close it. */
+       if (conf.stats_interval) {
+               fclose(stats_fp);
+               stats_fp = 0;
+       }
+
        rate_limiters_rollover(time(NULL));
 
        VSM_Close(vd);
diff --git a/varnishkafka.h b/varnishkafka.h
index dd90806..a2b4d42 100644
--- a/varnishkafka.h
+++ b/varnishkafka.h
@@ -191,6 +191,7 @@
                                      * truncating it. */
 
        int         stats_interval;  /* Statistics output interval */
+       char       *stats_file;      /* Statistics output log file */
        time_t      t_last_stats;    /* Last stats output */
 
        /* Kafka config */
@@ -230,6 +231,8 @@
 
 #define _DBG(fmt...) vk_log("DEBUG", LOG_DEBUG, fmt)
 
+void vk_log_stats(const char *fmt, ...)
+       __attribute__((format (printf, 1, 2)));
 
 void out_kafka (struct fmt_conf *fconf, struct logline *lp,
                const char *buf, size_t len);

-- 
To view, visit https://gerrit.wikimedia.org/r/95473
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I501fe6b421325ae98e21f910a7ae367762075371
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/varnish/varnishkafka
Gerrit-Branch: master
Gerrit-Owner: Ottomata <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to