]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: "drop" support for log-profile steps
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 19 Aug 2024 16:06:19 +0000 (18:06 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Mon, 19 Aug 2024 16:53:01 +0000 (18:53 +0200)
It is now possible to use "drop" keyword for "on" lines under a
log-profile section to specify that no log at all should be emitted for
the specified step (setting an empty format was not sufficient to do so
because only the log payload would be empty, not the log header, thus the
log would still be emitted).

It may be useful to selectively disable logging at specific steps for a
given log target (since the log profile may be set on log directives):

log-profile myprof
  on request format "blabla" sd "custom sd"
  on response drop

New testcase was added to reg-tests/log/log_profiles.vtc

doc/configuration.txt
include/haproxy/log-t.h
reg-tests/log/log_profiles.vtc
src/log.c

index 9f553556f4ccdc4cb6fc56944a85e9fa16005b28..05e8d22ca150ff41b7ef70edd0d4933f3e851201 100644 (file)
@@ -26572,11 +26572,17 @@ log-profile <name>
 log-tag <string>
   Override syslog log tag set globally or per-proxy using "log-tag" directive.
 
-on <step> [format <fmt>] [sd <sd_fmt>]
+on <step> [drop] [format <fmt>] [sd <sd_fmt>]
   Override the log-format string normally used to build the log line at
   <step> logging step. <fmt> is used to override "log-format" or
   "error-log-format" strings (depending on the <step>) whereas <sd_fmt> is
-  used to override "log-format-sd" string. Possible values for <step> are:
+  used to override "log-format-sd" string (both can be combined).
+
+  "drop" special keyword may be used to specify that no log should be
+  emitted for the given <step>. It takes precedence over "format" and
+  "sd" if previously defined.
+
+  Possible values for <step> are:
 
   - "accept"   : override log-format if the log is generated right after
                  frontend conn was accepted
@@ -26604,6 +26610,7 @@ on <step> [format <fmt>] [sd <sd_fmt>]
       log-tag "custom-tag"
 
       on error format "%ci: error"
+      on connect drop
       on any sd "custom-sd"
 
     listen myproxy
index 11161d24123f87d39ed468a22502203d0d08a206..0bc12b2060531ad7e26632952a0cfb3e027483ea 100644 (file)
@@ -272,9 +272,16 @@ enum log_orig {
        LOG_ORIG_TXN_CLOSE,          /* during stream termination */
 };
 
+/* log profile step flags */
+enum log_ps_flags {
+       LOG_PS_FL_NONE = 0,
+       LOG_PS_FL_DROP,              /* don't emit log for this step */
+};
+
 struct log_profile_step {
        struct lf_expr logformat;
        struct lf_expr logformat_sd;
+       enum log_ps_flags flags;     /* LOG_PS_FL_* */
 };
 
 struct log_profile {
index fc09639a1c8ee94b4b4ab37b9c4ccacd5b8603ba..552fb32439630e4f1a70e3b8c5d700e052971b0c 100644 (file)
@@ -66,6 +66,13 @@ syslog Slg3 -level info {
     barrier b4 sync
 } -start
 
+syslog Slg4 -level info {
+    recv
+    #rfc5424, logprof3, tcp error (other steps should be dropped)
+    expect ~ ".* custom ${h1_pid} .* error"
+    barrier b4 sync
+} -start
+
 haproxy h1 -conf {
        defaults
                timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
@@ -92,6 +99,7 @@ haproxy h1 -conf {
                log udp@${Slg1_addr}:${Slg1_port} format rfc5424 local0
                log udp@${Slg2_addr}:${Slg2_port} format rfc5424 profile logprof1 local0
                log udp@${Slg3_addr}:${Slg3_port} format rfc5424 profile logprof2 local0
+               log udp@${Slg4_addr}:${Slg4_port} format rfc5424 profile logprof3 local0
 
                default_backend be_tcp
 
@@ -119,6 +127,10 @@ haproxy h1 -conf {
                on error format "error"
                on any format "%OG"
 
+       log-profile logprof3
+               on error format "error"
+               on any drop
+
        backend be
                mode http
                server app1 ${s1_addr}:${s1_port}
index fd8be525fd21e0bf659f40ae15ec3967a83baa2b..d338e1ec160f4baa2605974edf4fa3a087b00be0 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2927,6 +2927,9 @@ static inline void _process_send_log_override(struct process_send_log_ctx *ctx,
                step = prof->any;
 
        if (ctx && ctx->sess && step) {
+               if (step->flags & LOG_PS_FL_DROP)
+                       goto end; // skip logging
+
                /* we may need to rebuild message using lf_expr from profile
                 * step and possibly sd metadata if provided on the profile
                 */
@@ -6065,6 +6068,7 @@ static inline void log_profile_step_init(struct log_profile_step *lprof_step)
 {
        lf_expr_init(&lprof_step->logformat);
        lf_expr_init(&lprof_step->logformat_sd);
+       lprof_step->flags = LOG_PS_FL_NONE;
 }
 
 static inline void log_profile_step_free(struct log_profile_step *lprof_step)
@@ -6285,14 +6289,24 @@ int cfg_parse_log_profile(const char *file, int linenum, char **args, int kwm)
                cur_arg = 2;
 
                while (*(args[cur_arg]) != 0) {
+                       /* drop logs ? */
+                       if (strcmp(args[cur_arg], "drop") == 0) {
+                               if (cur_arg != 2)
+                                       break;
+                               (*target_step)->flags |= LOG_PS_FL_DROP;
+                               cur_arg += 1;
+                               continue;
+                       }
                        /* regular format or SD (structured-data) one? */
-                       if (strcmp(args[cur_arg], "format") == 0)
+                       else if (strcmp(args[cur_arg], "format") == 0)
                                target_lf = &(*target_step)->logformat;
                        else if (strcmp(args[cur_arg], "sd") == 0)
                                target_lf = &(*target_step)->logformat_sd;
                        else
                                break;
 
+                       (*target_step)->flags &= ~LOG_PS_FL_DROP;
+
                        /* parse and assign logformat expression */
                        lf_expr_deinit(target_lf); /* if already configured */
 
@@ -6319,7 +6333,7 @@ int cfg_parse_log_profile(const char *file, int linenum, char **args, int kwm)
                        cur_arg += 2;
                }
                if (cur_arg == 2 || *(args[cur_arg]) != 0) {
-                       ha_alert("parsing [%s:%d] : '%s %s' expects 'format' and/or 'sd'.\n",
+                       ha_alert("parsing [%s:%d] : '%s %s' expects 'drop', 'format' or 'sd'.\n",
                                 file, linenum, args[0], args[1]);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;