]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: sink: inherit from caller fmt in ring_write() when rings didn't set one
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 25 Aug 2023 07:50:27 +0000 (09:50 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 13 Oct 2023 08:05:06 +0000 (10:05 +0200)
implicit rings were automatically forced to the parent logger format, but
this was done upon ring creation.

This is quite restrictive because we might want to choose the desired
format right before generating the log header (ie: when producing the
log message), depending on the logger (log directive) that is
responsible for the log message, and with current logic this is not
possible. (To this day, we still have dedicated implicit ring per log
directive, but this might change)

In ring_write(), we check if the sink->fmt is specified:
 - defined: we use it since it is the most precise format
   (ie: for named rings)
 - undefined: then we fallback to the format from the logger

With this change, implicit rings' format is now set to UNSPEC upon
creation. This is safe because the log header building function
automatically enforces the "raw" format when UNSPEC is set. And since
logger->format also defaults to "raw", no change of default behavior
should be expected.

include/haproxy/sink.h
src/sink.c

index bed687fe3c64876e94d27fb845ec78a8b9924ebf..e88208de8cc5017ad89b82931b11567a996acc21 100644 (file)
@@ -38,7 +38,8 @@ int sink_announce_dropped(struct sink *sink, struct log_header hdr);
 
 
 /* tries to send <nmsg> message parts from message array <msg> to sink <sink>.
- * Formatting according to the sink's preferences is done here.
+ * Formatting according to the sink's preference is done here, unless sink->fmt
+ * is unspecified, in which case the caller formatting will be used instead.
  *
  * It will stop writing at <maxlen> instead of sink->maxlen if <maxlen> is
  * positive and inferior to sink->maxlen.
index 569b4c8d275203a6b4f068aa43697e0acc3d4674..ef9e3b158680fea0790b3bf67347d9200b45c080 100644 (file)
@@ -165,9 +165,11 @@ struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt,
 }
 
 /* tries to send <nmsg> message parts from message array <msg> to sink <sink>.
- * Formatting according to the sink's preference is done here. Lost messages
- * are NOT accounted for. It is preferable to call sink_write() instead which
- * will also try to emit the number of dropped messages when there are any.
+ * Formatting according to the sink's preference is done here, unless sink->fmt
+ * is unspecified, in which case the caller formatting will be used instead.
+ * Lost messages are NOT accounted for. It is preferable to call sink_write()
+ * instead which will also try to emit the number of dropped messages when there
+ * are any.
  *
  * It will stop writing at <maxlen> instead of sink->maxlen if <maxlen> is
  * positive and inferior to sink->maxlen.
@@ -183,7 +185,8 @@ struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt,
        if (sink->fmt == LOG_FORMAT_RAW)
                goto send;
 
-       hdr.format = sink->fmt; /* sink format prevails over log one */
+       if (sink->fmt != LOG_FORMAT_UNSPEC)
+               hdr.format = sink->fmt; /* sink format prevails over log one */
        pfx = build_log_header(hdr, &npfx);
 
 send:
@@ -1191,11 +1194,16 @@ struct sink *sink_new_from_logger(struct logger *logger)
                goto error;
        }
 
-       /* disable sink->maxlen, we already have logger->maxlen */
-       sink->maxlen = 0;
+       /* ring format normally defaults to RAW, but here we set ring format
+        * to UNSPEC to inherit from caller format in sink_write() since we
+        * cannot customize implicit ring settings
+        */
+       sink->fmt = LOG_FORMAT_UNSPEC;
 
-       /* set ring format from logger format */
-       sink->fmt = logger->format;
+       /* for the same reason, we disable sink->maxlen to inherit from caller
+        * maxlen in sink_write()
+        */
+       sink->maxlen = 0;
 
        /* Set default connect and server timeout for sink forward proxy */
        sink->forward_px->timeout.connect = MS_TO_TICKS(1000);