Add the client host IP address and port to application_name.
authorAndrew Dunstan <andrew@dunslane.net>
Tue, 8 Apr 2014 15:28:43 +0000 (11:28 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Tue, 8 Apr 2014 15:28:43 +0000 (11:28 -0400)
This is enabled by a config parameter which defaults to 'on'

If the client subsequently overwrites the application_name parameter
during the session then this won't be sticky, but it does let you see
(and log) where queries are actually coming from instead of just seeing
pgbouncer as the source address and port.

doc/config.txt
include/bouncer.h
src/client.c
src/main.c

index 49e84807df5592a3177e68b80f38b9b6ceb12e6c..b3af81446ad113a08b6d9bd2a15857f86aa0368f 100644 (file)
@@ -227,6 +227,15 @@ exclusively use Extended Query protocol will stay working.
 
 Default: 0
 
+==== application_name_add_host ====
+
+Add the client host address and port to the application name setting set on connection start.
+This helps in identifying the source of bad queries etc. The setting will be overwritten
+without any detection if the application does SET APPLICATION_NAME after connecting.
+Note that this is on by default.
+
+Default: 1
+
 === Log settings ===
 
 ==== syslog ====
index efc608a27a308acb60d8dfe789a103195c906cd1..14e6087e89606bd3d3732b1ed1884b3337aee2f7 100644 (file)
@@ -427,6 +427,7 @@ extern int cf_tcp_defer_accept;
 extern int cf_log_connections;
 extern int cf_log_disconnections;
 extern int cf_log_pooler_errors;
+extern int cf_application_name_add_host;
 
 extern const struct CfLookup pool_mode_map[];
 
index c8a2574d594260aae9ad12d0c4ffc579af3ba465..0fa8ad501f10ca11987912c0275d078ea251b693 100644 (file)
@@ -328,6 +328,7 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt)
        const char *username = NULL, *dbname = NULL;
        const char *key, *val;
        bool ok;
+       bool appname_found = false;
 
        while (1) {
                ok = mbuf_get_string(&pkt->data, &key);
@@ -343,6 +344,17 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt)
                } else if (strcmp(key, "user") == 0) {
                        slog_debug(client, "got var: %s=%s", key, val);
                        username = val;
+               } else if (cf_application_name_add_host &&
+                                  strcmp(key, "application_name") == 0) {
+                       int port = pga_port(&client->remote_addr);
+                       static char ipbuf[PGADDR_BUF], buf[1024];
+                       const char *ip;
+
+                       ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf));
+                       snprintf(buf, sizeof(buf), "%s (%s:%d)", val, ip, port);
+                       slog_debug(client,"using application name %s",buf);
+                       varcache_set(&client->vars, key, buf);
+                       appname_found = true;
                } else if (varcache_set(&client->vars, key, val)) {
                        slog_debug(client, "got var: %s=%s", key, val);
                } else if (strlist_contains(cf_ignore_startup_params, key)) {
@@ -362,6 +374,18 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt)
        if (!dbname || !dbname[0])
                dbname = username;
 
+       /* default application_name to "client at <addr>:<port>" */
+       if (!appname_found && cf_application_name_add_host) {
+               int port = pga_port(&client->remote_addr);
+               static char ipbuf[PGADDR_BUF], buf[1024];
+               const char *ip;
+
+               ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf));
+               snprintf(buf, sizeof(buf), "client at %s:%d", ip, port);
+               slog_debug(client,"using default application name %s",buf);
+               varcache_set(&client->vars, key, buf);
+       }
+
        /* check if limit allows, don't limit admin db
           nb: new incoming conn will be attached to PgSocket, thus
           get_active_client_count() counts it */
index 1cd5f3e6643f8fb3d8550fd39a950b8d971ce96c..691d4af5f8ca52484f2aa48ad8e91c9ecf7cd26e 100644 (file)
@@ -134,6 +134,7 @@ int cf_stats_period;
 int cf_log_connections;
 int cf_log_disconnections;
 int cf_log_pooler_errors;
+int cf_application_name_add_host;
 
 /*
  * config file description
@@ -231,6 +232,7 @@ CF_ABS("stats_period", CF_INT, cf_stats_period, 0, "60"),
 CF_ABS("log_connections", CF_INT, cf_log_connections, 0, "1"),
 CF_ABS("log_disconnections", CF_INT, cf_log_disconnections, 0, "1"),
 CF_ABS("log_pooler_errors", CF_INT, cf_log_pooler_errors, 0, "1"),
+CF_ABS("application_name_add_host", CF_INT, cf_application_name_add_host, 0, "1"),
 {NULL}
 };