]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: allow custom date format in error-log-format
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Tue, 4 Mar 2025 15:35:05 +0000 (16:35 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Tue, 4 Mar 2025 17:57:29 +0000 (18:57 +0100)
Sample fetches %[accept_date] and %[request_date] with converters can be used
in error-log-format string. But in the most error cases they fetches nothing,
as error logs are produced on SSL handshake issues or when invalid PROXY
protocol header is used. Stream object is never allocated in such cases and
smp_fetch_accept_date() just simply returns 0.

There is a need to have a custom date format (ISO8601) also in the error logs,
along with normal logs. When sess_build_logline_orig() builds log line it
always copies the accept date to strm_logs structure. When stream is absent,
accept date is copied from the session object.

So, if the steam object wasn't allocated, let's use the session date info in
smp_fetch_accept_date(). This allows then, in sample_process(), to apply to the
fetched date different converters and formats.

This fixes the issue #2884.

src/tcp_sample.c

index 109119070edca20923fc61fcd292c6074c7e798c..a552ebd4f911bd46d7a95a9313a44a735b08e3fe 100644 (file)
@@ -499,18 +499,26 @@ smp_fetch_accept_date(const struct arg *args, struct sample *smp, const char *kw
        struct strm_logs *logs;
        struct timeval tv;
 
-       if (!smp->strm)
+       if (smp->strm) {
+               logs = &smp->strm->logs;
+
+               if (kw[0] == 'r') {  /* request_date */
+                       tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
+               } else {             /* accept_date */
+                       tv.tv_sec = logs->accept_date.tv_sec;
+                       tv.tv_usec = logs->accept_date.tv_usec;
+               }
+       /* case of error-log-format */
+       } else if (smp->sess) {
+               if (kw[0] == 'r') {  /* request_date */
+                       tv_ms_add(&tv, &smp->sess->accept_date, smp->sess->t_idle >= 0 ? smp->sess->t_idle + smp->sess->t_handshake : 0);
+               } else {             /* accept_date */
+                       tv.tv_sec = smp->sess->accept_date.tv_sec;
+                       tv.tv_usec = smp->sess->accept_date.tv_usec;
+               }
+       } else
                return 0;
 
-       logs = &smp->strm->logs;
-
-       if (kw[0] == 'r') {  /* request_date */
-               tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
-       } else {             /* accept_date */
-               tv.tv_sec = logs->accept_date.tv_sec;
-               tv.tv_usec = logs->accept_date.tv_usec;
-       }
-
        smp->data.u.sint = tv.tv_sec;
 
        /* report in milliseconds */