-
Notifications
You must be signed in to change notification settings - Fork 10
/
redislog.c
1416 lines (1193 loc) · 33.3 KB
/
redislog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* redislog.c
*
* Extension that allows PostgreSQL to send log entries to a
* Redis server directly in JSON format.
* Requires the Hiredis library (https://github.com/redis/hiredis)
*
* One of the goals of redislog is to allow administrators
* to tap PostgreSQL directly into the Logstash pipeline
* for real-time monitoring, by acting as a "Shipper" component
* that sends events to a "Broker", such as Redis.
*
* Copyright (c) 1996-2015, PostgreSQL Global Development Group
*
* Authors:
* Marco Nenciarini <[email protected]>
* Gabriele Bartolini <[email protected]>
*
* Partially based on jsonlog by Michael Paquier
* https://github.com/michaelpq/pg_plugins/blob/master/jsonlog/jsonlog.c
*
* IDENTIFICATION
* redislog/redislog.c
*
*-------------------------------------------------------------------------
*/
#include <unistd.h>
#include <sys/time.h>
#include "postgres.h"
#include "libpq/libpq.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "access/xact.h"
#include "access/transam.h"
#include "lib/stringinfo.h"
#include "postmaster/syslogger.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/json.h"
#include "utils/ps_status.h"
#include "hiredis/hiredis.h"
#define REDIS_DEFAULT_PORT 6379
/* Allow load of this module in shared libs */
PG_MODULE_MAGIC;
void _PG_init(void);
void _PG_fini(void);
/* Hold previous logging hook */
static emit_log_hook_type prev_log_hook = NULL;
/* GUC Variables */
char *Redislog_hosts = NULL;
int Redislog_timeout = 1000;
char *Redislog_key = NULL;
int Redislog_min_error_statement = ERROR;
int Redislog_min_messages = WARNING;
bool Redislog_ship_to_redis_only = TRUE;
bool Redislog_shuffle_hosts = TRUE;
char *Redislog_fields = NULL;
static MemoryContext redislog_cfg_memory_context;
typedef struct redis_server_info {
char *host_name; /* Hold the Redis server hostname */
int port; /* Hold the server port */
} redis_server_info;
typedef struct json_field_mapping {
int field_index; /* The field index */
char *custom_field_name; /* The name choosen by the user */
} json_field_mapping;
/*
* The redis server list. This list is terminated by an element
* with host_name==NULL
*/
static redis_server_info *Redislog_server_info = NULL;
/*
* The json field list. This list is terminated by an element
* which has field_index == FIELD_INVALID and custom_field_name==NULL
*/
static json_field_mapping *Redislog_json_field_mapping = NULL;
#define FIELD_INVALID -1
#define FIELD_USER_NAME 1
#define FIELD_USER_NAME_DESC "user_name"
#define FIELD_DATABASE_NAME 2
#define FIELD_DATABASE_NAME_DESC "database_name"
#define FIELD_PROCESS_ID 3
#define FIELD_PROCESS_ID_DESC "process_id"
#define FIELD_REMOTE_HOST 4
#define FIELD_REMOTE_HOST_DESC "remote_host"
#define FIELD_REMOTE_PORT 5
#define FIELD_REMOTE_PORT_DESC "remote_port"
#define FIELD_SESSION_ID 6
#define FIELD_SESSION_ID_DESC "session_id"
#define FIELD_SESSION_LINE_NUM 7
#define FIELD_SESSION_LINE_NUM_DESC "session_line_num"
#define FIELD_COMMAND_TAG 8
#define FIELD_COMMAND_TAG_DESC "command_tag"
#define FIELD_SESSION_START_TIME 9
#define FIELD_SESSION_START_TIME_DESC "session_start_time"
#define FIELD_VIRTUAL_TRANSACTION_ID 10
#define FIELD_VIRTUAL_TRANSACTION_ID_DESC "virtual_transaction_id"
#define FIELD_TRANSACTION_ID 11
#define FIELD_TRANSACTION_ID_DESC "transaction_id"
#define FIELD_ERROR_SEVERITY 12
#define FIELD_ERROR_SEVERITY_DESC "error_severity"
#define FIELD_SQL_STATE_CODE 13
#define FIELD_SQL_STATE_CODE_DESC "sql_state_code"
#define FIELD_DETAIL_LOG 14
#define FIELD_DETAIL_LOG_DESC "detail_log"
#define FIELD_DETAIL 15
#define FIELD_DETAIL_DESC "detail"
#define FIELD_HINT 16
#define FIELD_HINT_DESC "hint"
#define FIELD_INTERNAL_QUERY 17
#define FIELD_INTERNAL_QUERY_DESC "internal_query"
#define FIELD_INTERNAL_QUERY_POS 18
#define FIELD_INTERNAL_QUERY_POS_DESC "internal_query_pos"
#define FIELD_CONTEXT 19
#define FIELD_CONTEXT_DESC "context"
#define FIELD_QUERY 20
#define FIELD_QUERY_DESC "query"
#define FIELD_QUERY_POS 21
#define FIELD_QUERY_POS_DESC "query_pos"
#define FIELD_FILE_LOCATION 22
#define FIELD_FILE_LOCATION_DESC "file_location"
#define FIELD_APPLICATION_NAME 23
#define FIELD_APPLICATION_NAME_DESC "application_name"
#define FIELD_MESSAGE 24
#define FIELD_MESSAGE_DESC "message"
/* Log timestamp */
#define LOG_TIMESTAMP_LEN 128
static char formatted_log_time[LOG_TIMESTAMP_LEN];
/* Session start timestamp */
static char formatted_start_time[LOG_TIMESTAMP_LEN];
/* Redis context */
static redisContext *redis_context = NULL;
/* Used to detect if values inherited over fork need resetting. */
static int lastPid = 0;
/* String mapper for error severity */
static const char *error_severity(int elevel);
/* Configuration options management */
static bool guc_check_hosts_list(char **newvalue, void **extra, GucSource source);
static void guc_assign_hosts_list(const char *newval, void *extra);
static bool guc_check_fields(char **newvalue, void **extra, GucSource source);
static void guc_assign_fields(const char *newval, void *extra);
static bool guc_check_field_entry(const char *str);
static bool guc_field_name_is_valid(const char *str);
static int guc_field_name_get_idx(const char *str);
static void split_host_port(const char *token, char **hostName, int *port);
static void split_field_name(const char *str, char **field, char **name);
static char **create_host_list(char *hosts_string, int *hosts_count);
static bool host_port_pair_is_correct(const char *str);
static void free_redis_server_info(void);
static void free_json_field_mapping(void);
static void shuffle_redis_server_list(void);
/* Redis specific prototypes */
static void redis_log_hook(ErrorData *edata);
static void redis_close_connection(void);
static bool redis_open_connection(void);
static bool redis_log_shipper(char *data, int len);
/*
* Enum definition for redislog.min_error_statement and redislog.min_messages
*/
static const struct config_enum_entry server_message_level_options[] = {
{"debug", DEBUG2, true},
{"debug5", DEBUG5, false},
{"debug4", DEBUG4, false},
{"debug3", DEBUG3, false},
{"debug2", DEBUG2, false},
{"debug1", DEBUG1, false},
{"info", INFO, false},
{"notice", NOTICE, false},
{"warning", WARNING, false},
{"error", ERROR, false},
{"log", LOG, false},
{"fatal", FATAL, false},
{"panic", PANIC, false},
{NULL, 0, false}
};
/*
* Useful for HUP triggered host reassignment: close the connection, a new one
* will be opened on next event.
*/
static void
guc_assign_hosts_list(const char *newval, void *extra)
{
MemoryContext oldcontext;
char **server_lookup;
char *hosts_string;
int hosts_count;
int i;
redis_close_connection();
oldcontext = MemoryContextSwitchTo(redislog_cfg_memory_context);
free_redis_server_info();
hosts_string = pstrdup(newval);
server_lookup = create_host_list(hosts_string, &hosts_count);
Redislog_server_info = palloc(sizeof(redis_server_info)*(hosts_count+1));
for(i=0; i<hosts_count; i++)
{
char *tok = server_lookup[i];
char *hostname = NULL;
int port;
split_host_port(tok, &hostname, &port);
Redislog_server_info[i].host_name = hostname;
Redislog_server_info[i].port = port;
}
Redislog_server_info[i].host_name = NULL;
Redislog_server_info[i].port = 0;
pfree(hosts_string);
MemoryContextSwitchTo(oldcontext);
}
/*
* Deallocate the redis server struct
*/
static void free_redis_server_info() {
int i = 0;
if (Redislog_server_info==NULL)
{
return;
}
while(true)
{
if (Redislog_server_info[i].host_name==NULL)
break;
else
pfree(Redislog_server_info[i].host_name);
i++;
}
pfree(Redislog_server_info);
}
/*
* Check if the hosts list is syntactically correct
*/
static bool guc_check_hosts_list(char **newvalue, void **extra, GucSource source)
{
char *hosts_string;
int hosts_count;
int i;
char **server_lookup;
hosts_string = pstrdup(*newvalue);
server_lookup = create_host_list(hosts_string, &hosts_count);
if (server_lookup==NULL)
{
GUC_check_errdetail("redislog.hosts list syntax is invalid");
pfree(hosts_string);
return false;
}
if (hosts_count==0)
{
GUC_check_errdetail("redislog.hosts must not be empty");
pfree(server_lookup);
pfree(hosts_string);
return false;
}
for (i=0; i<hosts_count; i++)
{
if (!host_port_pair_is_correct(server_lookup[i]))
{
GUC_check_errdetail("redislog.hosts \"%s\" entry must be of form HOST[:PORT]", server_lookup[i]);
pfree(server_lookup);
pfree(hosts_string);
return false;
}
}
pfree(server_lookup);
pfree(hosts_string);
return true;
}
static void
guc_assign_fields(const char *newval, void *extra)
{
List *field_list;
ListCell *l;
char *field_string;
MemoryContext oldcontext;
int i;
oldcontext = MemoryContextSwitchTo(redislog_cfg_memory_context);
free_json_field_mapping();
field_string = pstrdup(newval);
if (!SplitIdentifierString(field_string, ',', &field_list))
{
list_free(field_list);
pfree(field_string);
}
else
{
Redislog_json_field_mapping = palloc(
sizeof(json_field_mapping) * (list_length(field_list)+1));
i = 0;
foreach(l, field_list)
{
char *field;
char *name;
split_field_name(lfirst(l), &field, &name);
Redislog_json_field_mapping[i].field_index = guc_field_name_get_idx(field);
if (name==NULL)
{
Redislog_json_field_mapping[i].custom_field_name = pstrdup(field);
}
else
{
Redislog_json_field_mapping[i].custom_field_name = name;
}
pfree(field);
i++;
}
Redislog_json_field_mapping[i].field_index = FIELD_INVALID;
Redislog_json_field_mapping[i].custom_field_name = NULL;
}
pfree(field_string);
list_free(field_list);
MemoryContextSwitchTo(oldcontext);
}
static void
free_json_field_mapping(void)
{
int i;
if (Redislog_json_field_mapping==NULL) return;
i = 0;
while (true)
{
if (Redislog_json_field_mapping[i].custom_field_name==NULL &&
Redislog_json_field_mapping[i].field_index==FIELD_INVALID) break;
pfree(Redislog_json_field_mapping[i].custom_field_name);
i = i+1;
}
pfree(Redislog_json_field_mapping);
}
static bool
guc_check_fields(char **newvalue, void **extra, GucSource source)
{
List *field_list;
ListCell *l;
char *field_string;
field_string = pstrdup(*newvalue);
if (!SplitIdentifierString(field_string, ',', &field_list))
{
GUC_check_errdetail("redislog.fields list syntax is invalid");
list_free(field_list);
pfree(field_string);
return false;
}
foreach(l, field_list)
{
char *field;
if (!guc_check_field_entry(lfirst(l)))
{
list_free(field_list);
pfree(field_string);
return false;
}
split_field_name(lfirst(l), &field, NULL);
if (!guc_field_name_is_valid(field))
{
GUC_check_errdetail("redislog.field: Field \"%s\" is unknown", field);
pfree(field);
list_free(field_list);
pfree(field_string);
return false;
}
pfree(field);
}
pfree(field_string);
list_free(field_list);
return true;
}
/*
* Check if the field name is valid
*/
static bool
guc_check_field_entry(const char *str)
{
char *p_colon;
Assert(str!=NULL);
if(strlen(str)==0)
{
return false;
}
p_colon = strchr(str, ':');
if (p_colon==str)
{
/* missing field name */
GUC_check_errdetail("redislog \"%s\".fields entry must be of the form FIELD[:NAME]", str);
return false;
}
if (p_colon==NULL)
{
/* will use the default field name */
return true;
}
if (p_colon[1]=='\x0')
{
/* missing custom field name */
return false;
}
return true;
}
/*
* Split field from its custom name
*/
static void
split_field_name(const char *str, char **field, char **name)
{
const char *p_colon;
Assert(str!=NULL);
p_colon = strchr(str, ':');
if (name!=NULL)
{
if (p_colon==NULL)
*name = NULL;
else
*name = pstrdup(p_colon+1);
}
if (field!=NULL)
{
*field = pstrdup(str);
if (p_colon!=NULL)
(*field)[p_colon-str]='\0';
}
}
/*
* Check if a field name is known
*/
static bool
guc_field_name_is_valid(const char *str)
{
Assert(str!=NULL);
return guc_field_name_get_idx(str) != FIELD_INVALID;
}
/*
* Check if a field name is known
*/
static int
guc_field_name_get_idx(const char *str)
{
Assert(str!=NULL);
if (0==strcmp(str, FIELD_USER_NAME_DESC)) return FIELD_USER_NAME;
if (0==strcmp(str, FIELD_DATABASE_NAME_DESC)) return FIELD_DATABASE_NAME;
if (0==strcmp(str, FIELD_PROCESS_ID_DESC)) return FIELD_PROCESS_ID;
if (0==strcmp(str, FIELD_REMOTE_HOST_DESC)) return FIELD_REMOTE_HOST;
if (0==strcmp(str, FIELD_REMOTE_PORT_DESC)) return FIELD_REMOTE_PORT;
if (0==strcmp(str, FIELD_SESSION_ID_DESC)) return FIELD_SESSION_ID;
if (0==strcmp(str, FIELD_SESSION_LINE_NUM_DESC)) return FIELD_SESSION_LINE_NUM;
if (0==strcmp(str, FIELD_COMMAND_TAG_DESC)) return FIELD_COMMAND_TAG;
if (0==strcmp(str, FIELD_SESSION_START_TIME_DESC)) return FIELD_SESSION_START_TIME;
if (0==strcmp(str, FIELD_VIRTUAL_TRANSACTION_ID_DESC)) return FIELD_VIRTUAL_TRANSACTION_ID;
if (0==strcmp(str, FIELD_TRANSACTION_ID_DESC)) return FIELD_TRANSACTION_ID;
if (0==strcmp(str, FIELD_ERROR_SEVERITY_DESC)) return FIELD_ERROR_SEVERITY;
if (0==strcmp(str, FIELD_SQL_STATE_CODE_DESC)) return FIELD_SQL_STATE_CODE;
if (0==strcmp(str, FIELD_DETAIL_LOG_DESC)) return FIELD_DETAIL_LOG;
if (0==strcmp(str, FIELD_DETAIL_DESC)) return FIELD_DETAIL;
if (0==strcmp(str, FIELD_HINT_DESC)) return FIELD_HINT;
if (0==strcmp(str, FIELD_INTERNAL_QUERY_DESC)) return FIELD_INTERNAL_QUERY;
if (0==strcmp(str, FIELD_INTERNAL_QUERY_POS_DESC)) return FIELD_INTERNAL_QUERY_POS;
if (0==strcmp(str, FIELD_CONTEXT_DESC)) return FIELD_CONTEXT;
if (0==strcmp(str, FIELD_QUERY_DESC)) return FIELD_QUERY;
if (0==strcmp(str, FIELD_QUERY_POS_DESC)) return FIELD_QUERY_POS;
if (0==strcmp(str, FIELD_FILE_LOCATION_DESC)) return FIELD_FILE_LOCATION;
if (0==strcmp(str, FIELD_APPLICATION_NAME_DESC)) return FIELD_APPLICATION_NAME;
if (0==strcmp(str, FIELD_MESSAGE_DESC)) return FIELD_MESSAGE;
return FIELD_INVALID;
}
/*
* Check if the string is of the form HOST[:PORT]
*/
static bool
host_port_pair_is_correct(const char *str)
{
char *p_colon;
int port;
Assert(str!=NULL);
if (strlen(str)==0)
{
/* the string is empty */
return false;
}
p_colon = strchr(str, ':');
if (p_colon==str)
{
/* missing hostname */
return false;
}
if (p_colon==NULL)
{
/* will use the default port */
return true;
}
port = pg_atoi(p_colon+1, sizeof(int32), '\0');
if (port==0)
{
/* port is not valid */
return false;
}
return true;
}
/*
* error_severity
* Print string showing error severity based on integer level.
* Taken from elog.c.
*/
static const char *
error_severity(int elevel)
{
const char *prefix;
switch (elevel)
{
case DEBUG1:
case DEBUG2:
case DEBUG3:
case DEBUG4:
case DEBUG5:
prefix = _("DEBUG");
break;
case LOG:
case COMMERROR:
prefix = _("LOG");
break;
case INFO:
prefix = _("INFO");
break;
case NOTICE:
prefix = _("NOTICE");
break;
case WARNING:
prefix = _("WARNING");
break;
case ERROR:
prefix = _("ERROR");
break;
case FATAL:
prefix = _("FATAL");
break;
case PANIC:
prefix = _("PANIC");
break;
default:
prefix = "???";
break;
}
return prefix;
}
/*
* redis_close_connection
* Close the remote Redis connection.
*/
static void
redis_close_connection()
{
if (redis_context)
redisFree(redis_context);
redis_context = NULL;
}
/*
* shuffle_redis_server_list
* Shuffle the redis server list
*/
static void
shuffle_redis_server_list()
{
int i, j;
int server_count;
if (!Redislog_shuffle_hosts)
return;
for (i=0; Redislog_server_info[i].host_name!=NULL; i++);
server_count = i;
for (i=server_count-1; i>=1; i--) {
char *tmp_host_name;
int tmp_port;
j = random() % (i+1);
tmp_host_name = Redislog_server_info[i].host_name;
tmp_port = Redislog_server_info[i].port;
Redislog_server_info[i].host_name = Redislog_server_info[j].host_name;
Redislog_server_info[i].port = Redislog_server_info[j].port;
Redislog_server_info[j].host_name = tmp_host_name;
Redislog_server_info[j].port = tmp_port;
}
}
/*
* redis_open_connection
* Connect to remote Redis server, returns false on failure.
*/
static bool
redis_open_connection()
{
struct timeval timeout;
int i;
if (redis_context)
{
/*
* The connection is already opened
*/
return true;
}
i = 0;
while (true)
{
if (Redislog_server_info[i].host_name==NULL)
break;
timeout.tv_sec = Redislog_timeout / 1000;
timeout.tv_usec = Redislog_timeout % 1000 * 1000;
if (Redislog_server_info[i].host_name[0] == '/')
redis_context = redisConnectUnixWithTimeout(
Redislog_server_info[i].host_name,
timeout);
else
redis_context = redisConnectWithTimeout(
Redislog_server_info[i].host_name,
Redislog_server_info[i].port,
timeout);
if (redis_context == NULL || redis_context->err)
{
/*
* Something went wrong.
*/
redis_close_connection();
}
else
{
/*
* target server found
*/
break;
}
i++;
}
return Redislog_server_info[i].host_name!=NULL;
}
/*
* Create the host list array, that must be freed by the
* caller. Returns NULL if the redislog.hosts list is
* syntactitally incorrect.
*/
static char **
create_host_list(char *hosts_string, int *hosts_count)
{
List *hosts_list;
ListCell *l;
char **server_lookup;
int i;
Assert(hosts_count!=NULL);
Assert(hosts_string!=NULL);
*hosts_count = 0;
if (!SplitIdentifierString(hosts_string, ',', &hosts_list))
{
/*
* The hosts list have been checked in the GUC's check hook
*/
list_free(hosts_list);
return NULL;
}
*hosts_count = list_length(hosts_list);
server_lookup = palloc(*hosts_count * sizeof(char *));
i = 0;
foreach(l, hosts_list)
{
server_lookup[i] = lfirst(l);
i++;
}
list_free(hosts_list);
return server_lookup;
}
/*
* redis_log_shipper
* Ship log events to Redis. In case of network issues, retry once.
*/
static bool
redis_log_shipper(char *data, int len)
{
redisReply *reply;
unsigned attempts = 0;
Assert(len > 0);
while(attempts <= 1)
{
if (!redis_open_connection())
{
/*
* Connection failed. This message will not be sent.
*/
return false;
}
/* Push the event using binary safe API */
reply = redisCommand(redis_context, "RPUSH %s %b", Redislog_key, data, (size_t) len);
if (reply != NULL || !redis_context->err)
{
/* The event have been sent correctly, so we are done */
freeReplyObject(reply);
return true;
}
/* something occurred, close the connection and try again once */
attempts++;
/* Frees the reply object in Redis */
if (reply)
freeReplyObject(reply);
/* Close the Redis connection */
redis_close_connection();
}
return false;
}
/*
* Parse a string in the form HOST[:PORT]
* The default port is REDIS_DEFAULT_PORT
*/
static void
split_host_port(const char *token, char **hostName, int *port)
{
char *colon;
*hostName = pstrdup(token);
colon = strchr(*hostName, ':');
if (colon==NULL)
{
*port = REDIS_DEFAULT_PORT;
}
else
{
*port = pg_atoi(colon+1, sizeof(int32), '\0');
*colon = '\x0';
}
}
/*
* setup formatted_start_time
* (taken from backend/utils/error/elog.c)
*/
static void
setup_formatted_start_time(void)
{
pg_time_t stamp_time = (pg_time_t) MyStartTime;
/*
* Note: we expect that guc.c will ensure that log_timezone is set up (at
* least with a minimal GMT value) before Log_line_prefix can become
* nonempty or CSV mode can be selected.
*
* Note: we don't have the exact millisecond here.
*/
pg_strftime(formatted_start_time, LOG_TIMESTAMP_LEN,
"%Y-%m-%dT%H:%M:%S%z",
pg_localtime(&stamp_time, log_timezone));
}
/*
* setup_formatted_log_time
* (taken from jsonlog.c)
*/
static void
setup_formatted_log_time(void)
{
struct timeval tv;
pg_time_t stamp_time;
char msbuf[8];
gettimeofday(&tv, NULL);
stamp_time = (pg_time_t) tv.tv_sec;
/*
* Note: we expect that guc.c will ensure that log_timezone is set up (at
* least with a minimal GMT value) before Log_line_prefix can become
* nonempty or CSV mode can be selected.
*/
pg_strftime(formatted_log_time, LOG_TIMESTAMP_LEN,
/* leave room for milliseconds... */
"%Y-%m-%dT%H:%M:%S %z",
pg_localtime(&stamp_time, log_timezone));
/* 'paste' milliseconds into place... */
sprintf(msbuf, ".%03d", (int) (tv.tv_usec / 1000));
strncpy(formatted_log_time + 19, msbuf, 4);
}
/*
* is_log_level_output -- is elevel logically >= log_min_level?
*
* We use this for tests that should consider LOG to sort out-of-order,
* between ERROR and FATAL. Generally this is the right thing for testing
* whether a message should go to the postmaster log, whereas a simple >=
* test is correct for testing whether the message should go to the client.
* (taken from backend/utils/elog.c)
*/
static bool
is_log_level_output(int elevel, int log_min_level)
{
if (elevel == LOG || elevel == COMMERROR)
{
if (log_min_level == LOG || log_min_level <= ERROR)
return true;
}
else if (log_min_level == LOG)
{
/* elevel != LOG */
if (elevel >= FATAL)
return true;
}
/* Neither is LOG */
else if (elevel >= log_min_level)
return true;
return false;
}
/*
* append_json_literal
* Append to given StringInfo a JSON with a given key and a value
* not yet made literal.
* (taken from jsonlog.c)
*/
static void
append_json_literal(StringInfo buf, const char *key, const char *value, bool is_comma)
{
StringInfoData literal_json;
initStringInfo(&literal_json);
Assert(key && value);
/*
* Call in-core function able to generate wanted strings, there is
* no need to reinvent the wheel.
*/
if (value==NULL) {
appendStringInfo(&literal_json, "null");
} else {
escape_json(&literal_json, value);
}
/* Now append the field */
appendStringInfo(buf, "\"%s\":%s", key, literal_json.data);
/* Add comma if necessary */
if (is_comma)
appendStringInfoChar(buf, ',');
/* Clean up */
pfree(literal_json.data);
}
/*
* redis_log_hook
* Hook for shipping log events to Redis
* (based on jsonlog.c)
*/
static void
redis_log_hook(ErrorData *edata)
{
StringInfoData buf;
TransactionId txid = GetTopTransactionIdIfAny();
bool print_stmt = false;
bool send_status = false;
int i;
/* static counter for line numbers */
static long log_line_number = 0;