MINOR: logs: Add support of checks as session origin to format lf strings
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 14 Apr 2021 12:01:41 +0000 (14:01 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 21 Apr 2021 08:13:37 +0000 (10:13 +0200)
When a log-format string is built from an health-check, the session origin
is the health-check itself and not a connection. In addition, there is no
stream. It means for now some formats are not supported: %s, %sc, %b, %bi,
%bp, %si and %sp.

Thanks to this patch, the session origin is converted to a check. So it is
possible to retrieve the backend and the backend connection. Note this
session have no listener, thus %ft format must be guarded.

This patch is light and standalone, thus it may be backported as far as 2.2
if required. However, because the error is human, it is probably better to
wait a bit to be sure everything is properly protected.

(cherry picked from commit fd81848c2210fd88fdaa088723240930f3db4d63)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/log.c

index b18e612..8366cb7 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2120,9 +2120,9 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                 * A request error is reported as it's the only element we have
                 * here and which justifies emitting such a log.
                 */
-               be = fe;
+               be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
                txn = NULL;
-               be_conn = NULL;
+               be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
                s_flags = SF_ERR_PRXCOND | SF_FINST_R;
                uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
 
@@ -2418,7 +2418,7 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                                if (iret == 0)
                                        goto out;
                                tmplog += iret;
-                               if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
+                               if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
                                        LOGCHAR('~');
                                if (tmp->options & LOG_OPT_QUOTE)
                                        LOGCHAR('"');
@@ -2461,13 +2461,18 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                                break;
 
                        case LOG_FMT_SERVER: // %s
-                               switch (obj_type(s ? s->target : NULL)) {
+                               switch (obj_type(s ? s->target : sess->origin)) {
                                case OBJ_TYPE_SERVER:
                                        src = __objt_server(s->target)->id;
                                        break;
                                case OBJ_TYPE_APPLET:
                                        src = __objt_applet(s->target)->name;
                                        break;
+                               case OBJ_TYPE_CHECK:
+                                       src = (__objt_check(sess->origin)->server
+                                              ? __objt_check(sess->origin)->server->id
+                                              : "<NOSRV>");
+                                       break;
                                default:
                                        src = "<NOSRV>";
                                        break;
@@ -2668,9 +2673,21 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                                break;
 
                        case LOG_FMT_SRVCONN:  // %sc
-                               ret = ultoa_o(objt_server(s ? s->target : NULL) ?
-                                                objt_server(s->target)->cur_sess :
-                                                0, tmplog, dst + maxsize - tmplog);
+                               switch (obj_type(s ? s->target : sess->origin)) {
+                               case OBJ_TYPE_SERVER:
+                                       ret = ultoa_o(__objt_server(s->target)->cur_sess,
+                                                     tmplog, dst + maxsize - tmplog);
+                                       break;
+                               case OBJ_TYPE_CHECK:
+                                       ret = ultoa_o(__objt_check(sess->origin)->server
+                                                     ? __objt_check(sess->origin)->server->cur_sess
+                                                     : 0, tmplog, dst + maxsize - tmplog);
+                                       break;
+                               default:
+                                       ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
+                                       break;
+                               }
+
                                if (ret == NULL)
                                        goto out;
                                tmplog = ret;