-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathflb_output.c
1420 lines (1233 loc) · 41.6 KB
/
flb_output.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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_env.h>
#include <fluent-bit/flb_coro.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_io.h>
#include <fluent-bit/flb_uri.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_proxy.h>
#include <fluent-bit/flb_http_client_debug.h>
#include <fluent-bit/flb_output_thread.h>
#include <fluent-bit/flb_mp.h>
#include <fluent-bit/flb_pack.h>
FLB_TLS_DEFINE(struct flb_out_flush_params, out_flush_params);
void flb_output_prepare()
{
FLB_TLS_INIT(out_flush_params);
}
/* Validate the the output address protocol */
static int check_protocol(const char *prot, const char *output)
{
int len;
char *p;
p = strstr(output, "://");
if (p && p != output) {
len = p - output;
}
else {
len = strlen(output);
}
if (strlen(prot) != len) {
return 0;
}
/* Output plugin match */
if (strncasecmp(prot, output, len) == 0) {
return 1;
}
return 0;
}
/* Invoke pre-run call for the output plugin */
void flb_output_pre_run(struct flb_config *config)
{
struct mk_list *head;
struct flb_output_instance *ins;
struct flb_output_plugin *p;
mk_list_foreach(head, &config->outputs) {
ins = mk_list_entry(head, struct flb_output_instance, _head);
p = ins->p;
if (p->cb_pre_run) {
p->cb_pre_run(ins->context, config);
}
}
}
static void flb_output_free_properties(struct flb_output_instance *ins)
{
flb_kv_release(&ins->properties);
flb_kv_release(&ins->net_properties);
#ifdef FLB_HAVE_TLS
if (ins->tls_vhost) {
flb_sds_destroy(ins->tls_vhost);
}
if (ins->tls_ca_path) {
flb_sds_destroy(ins->tls_ca_path);
}
if (ins->tls_ca_file) {
flb_sds_destroy(ins->tls_ca_file);
}
if (ins->tls_crt_file) {
flb_sds_destroy(ins->tls_crt_file);
}
if (ins->tls_key_file) {
flb_sds_destroy(ins->tls_key_file);
}
if (ins->tls_key_passwd) {
flb_sds_destroy(ins->tls_key_passwd);
}
#endif
}
void flb_output_flush_prepare_destroy(struct flb_output_flush *out_flush)
{
struct flb_output_instance *ins = out_flush->o_ins;
struct flb_out_thread_instance *th_ins;
/* Move output coroutine context from active list to the destroy one */
if (flb_output_is_threaded(ins) == FLB_TRUE) {
th_ins = flb_output_thread_instance_get();
pthread_mutex_lock(&th_ins->flush_mutex);
mk_list_del(&out_flush->_head);
mk_list_add(&out_flush->_head, &th_ins->flush_list_destroy);
pthread_mutex_unlock(&th_ins->flush_mutex);
}
else {
mk_list_del(&out_flush->_head);
mk_list_add(&out_flush->_head, &ins->flush_list_destroy);
}
}
int flb_output_flush_id_get(struct flb_output_instance *ins)
{
int id;
int max = (2 << 13) - 1; /* max for 14 bits */
struct flb_out_thread_instance *th_ins;
if (flb_output_is_threaded(ins) == FLB_TRUE) {
th_ins = flb_output_thread_instance_get();
id = th_ins->flush_id;
th_ins->flush_id++;
/* reset once it reach the maximum allowed */
if (th_ins->flush_id > max) {
th_ins->flush_id = 0;
}
}
else {
id = ins->flush_id;
ins->flush_id++;
/* reset once it reach the maximum allowed */
if (ins->flush_id > max) {
ins->flush_id = 0;
}
}
return id;
}
void flb_output_coro_add(struct flb_output_instance *ins, struct flb_coro *coro)
{
struct flb_output_flush *out_flush;
out_flush = (struct flb_output_flush *) FLB_CORO_DATA(coro);
mk_list_add(&out_flush->_head, &ins->flush_list);
}
/*
* Queue a task to be flushed at a later time
* Deletes retry context if enqueue fails
*/
static int flb_output_task_queue_enqueue(struct flb_task_queue *queue,
struct flb_task_retry *retry,
struct flb_task *task,
struct flb_output_instance *out_ins,
struct flb_config *config)
{
struct flb_task_enqueued *queued_task;
queued_task = flb_malloc(sizeof(struct flb_task_enqueued));
if (!queued_task) {
flb_errno();
if (retry) {
flb_task_retry_destroy(retry);
}
return -1;
}
queued_task->retry = retry;
queued_task->out_instance = out_ins;
queued_task->task = task;
queued_task->config = config;
mk_list_add(&queued_task->_head, &queue->pending);
return 0;
}
/*
* Pop task from pending queue and flush it
* Will delete retry context if flush fails
*/
static int flb_output_task_queue_flush_one(struct flb_task_queue *queue)
{
struct flb_task_enqueued *queued_task;
int ret;
int is_empty;
is_empty = mk_list_is_empty(&queue->pending) == 0;
if (is_empty) {
flb_error("Attempting to flush task from an empty in_progress queue");
return -1;
}
queued_task = mk_list_entry_first(&queue->pending, struct flb_task_enqueued, _head);
mk_list_del(&queued_task->_head);
mk_list_add(&queued_task->_head, &queue->in_progress);
/*
* Remove temporary user now that task is out of singleplex queue.
* Flush will add back the user representing queued_task->out_instance if it succeeds.
*/
flb_task_users_dec(queued_task->task, FLB_FALSE);
ret = flb_output_task_flush(queued_task->task,
queued_task->out_instance,
queued_task->config);
/* Destroy retry context if needed */
if (ret == -1) {
if (queued_task->retry) {
flb_task_retry_destroy(queued_task->retry);
}
/* Flush the next task */
flb_output_task_singleplex_flush_next(queue);
return -1;
}
return ret;
}
/*
* Will either run or queue running a single task
* Deletes retry context if enqueue fails
*/
int flb_output_task_singleplex_enqueue(struct flb_task_queue *queue,
struct flb_task_retry *retry,
struct flb_task *task,
struct flb_output_instance *out_ins,
struct flb_config *config)
{
int ret;
int is_empty;
/*
* Add temporary user to preserve task while in singleplex queue.
* Temporary user will be removed when task is removed from queue.
*
* Note: if we fail to increment now, then the task may be prematurely
* deleted if the task's users go to 0 while we are waiting in the
* queue.
*/
flb_task_users_inc(task);
/* Enqueue task */
ret = flb_output_task_queue_enqueue(queue, retry, task, out_ins, config);
if (ret == -1) {
return -1;
}
/* Launch task if nothing is running */
is_empty = mk_list_is_empty(&out_ins->singleplex_queue->in_progress) == 0;
if (is_empty) {
return flb_output_task_queue_flush_one(out_ins->singleplex_queue);
}
return 0;
}
/*
* Clear in progress task and flush a single queued task if exists
* Deletes retry context on next flush if flush fails
*/
int flb_output_task_singleplex_flush_next(struct flb_task_queue *queue)
{
int is_empty;
struct flb_task_enqueued *ended_task;
/* Remove in progress task */
is_empty = mk_list_is_empty(&queue->in_progress) == 0;
if (!is_empty) {
ended_task = mk_list_entry_first(&queue->in_progress,
struct flb_task_enqueued, _head);
mk_list_del(&ended_task->_head);
flb_free(ended_task);
}
/* Flush if there is a pending task queued */
is_empty = mk_list_is_empty(&queue->pending) == 0;
if (!is_empty) {
return flb_output_task_queue_flush_one(queue);
}
return 0;
}
/*
* Flush a task through the output plugin, either using a worker thread + coroutine
* or a simple co-routine in the current thread.
*/
int flb_output_task_flush(struct flb_task *task,
struct flb_output_instance *out_ins,
struct flb_config *config)
{
int ret;
struct flb_output_flush *out_flush;
if (flb_output_is_threaded(out_ins) == FLB_TRUE) {
flb_task_users_inc(task);
/* Dispatch the task to the thread pool */
ret = flb_output_thread_pool_flush(task, out_ins, config);
if (ret == -1) {
flb_task_users_dec(task, FLB_FALSE);
/* If we are in synchronous mode, flush one waiting task */
if (out_ins->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_output_task_singleplex_flush_next(out_ins->singleplex_queue);
}
}
}
else {
/* Queue co-routine handling */
out_flush = flb_output_flush_create(task,
task->i_ins,
out_ins,
config);
if (!out_flush) {
return -1;
}
flb_task_users_inc(task);
ret = flb_pipe_w(config->ch_self_events[1], &out_flush,
sizeof(struct flb_output_flush*));
if (ret == -1) {
flb_errno();
flb_output_flush_destroy(out_flush);
flb_task_users_dec(task, FLB_FALSE);
/* If we are in synchronous mode, flush one waiting task */
if (out_ins->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_output_task_singleplex_flush_next(out_ins->singleplex_queue);
}
return -1;
}
}
return 0;
}
int flb_output_instance_destroy(struct flb_output_instance *ins)
{
if (ins->alias) {
flb_sds_destroy(ins->alias);
}
/* Remove URI context */
if (ins->host.uri) {
flb_uri_destroy(ins->host.uri);
}
flb_sds_destroy(ins->host.name);
flb_sds_destroy(ins->host.address);
flb_sds_destroy(ins->host.listen);
flb_sds_destroy(ins->match);
#ifdef FLB_HAVE_REGEX
if (ins->match_regex) {
flb_regex_destroy(ins->match_regex);
}
#endif
#ifdef FLB_HAVE_TLS
if (ins->use_tls == FLB_TRUE) {
if (ins->tls) {
flb_tls_destroy(ins->tls);
}
}
if (ins->tls_config_map) {
flb_config_map_destroy(ins->tls_config_map);
}
#endif
/* Remove metrics */
#ifdef FLB_HAVE_METRICS
if (ins->cmt) {
cmt_destroy(ins->cmt);
}
if (ins->metrics) {
flb_metrics_destroy(ins->metrics);
}
#endif
/* destroy callback context */
if (ins->callback) {
flb_callback_destroy(ins->callback);
}
/* destroy config map */
if (ins->config_map) {
flb_config_map_destroy(ins->config_map);
}
if (ins->net_config_map) {
flb_config_map_destroy(ins->net_config_map);
}
if (ins->ch_events[0] > 0) {
mk_event_closesocket(ins->ch_events[0]);
}
if (ins->ch_events[1] > 0) {
mk_event_closesocket(ins->ch_events[1]);
}
/* release properties */
flb_output_free_properties(ins);
/* free singleplex queue */
if (ins->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_task_queue_destroy(ins->singleplex_queue);
}
mk_list_del(&ins->_head);
/* processor */
if (ins->processor) {
flb_processor_destroy(ins->processor);
}
flb_free(ins);
return 0;
}
/* Invoke exit call for the output plugin */
void flb_output_exit(struct flb_config *config)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_output_instance *ins;
struct flb_output_plugin *p;
void *params;
mk_list_foreach_safe(head, tmp, &config->outputs) {
ins = mk_list_entry(head, struct flb_output_instance, _head);
p = ins->p;
/* Stop any worker thread */
if (flb_output_is_threaded(ins) == FLB_TRUE) {
flb_output_thread_pool_destroy(ins);
}
/* Check a exit callback */
if (p->cb_exit) {
p->cb_exit(ins->context, config);
}
flb_output_instance_destroy(ins);
}
params = FLB_TLS_GET(out_flush_params);
if (params) {
flb_free(params);
}
}
static inline int instance_id(struct flb_config *config)
{
struct flb_output_instance *ins;
if (mk_list_size(&config->outputs) == 0) {
return 0;
}
ins = mk_list_entry_last(&config->outputs, struct flb_output_instance,
_head);
return (ins->id + 1);
}
struct flb_output_instance *flb_output_get_instance(struct flb_config *config,
int out_id)
{
struct mk_list *head;
struct flb_output_instance *ins;
mk_list_foreach(head, &config->outputs) {
ins = mk_list_entry(head, struct flb_output_instance, _head);
if (ins->id == out_id) {
break;
}
ins = NULL;
}
if (!ins) {
return NULL;
}
return ins;
}
/*
* Invoked everytime a flush callback has finished (returned). This function
* is called from the event loop.
*/
int flb_output_flush_finished(struct flb_config *config, int out_id)
{
struct mk_list *tmp;
struct mk_list *head;
struct mk_list *list;
struct flb_output_instance *ins;
struct flb_output_flush *out_flush;
struct flb_out_thread_instance *th_ins;
ins = flb_output_get_instance(config, out_id);
if (!ins) {
return -1;
}
if (flb_output_is_threaded(ins) == FLB_TRUE) {
th_ins = flb_output_thread_instance_get();
list = &th_ins->flush_list_destroy;
}
else {
list = &ins->flush_list_destroy;
}
/* Look for output coroutines that needs to be destroyed */
mk_list_foreach_safe(head, tmp, list) {
out_flush = mk_list_entry(head, struct flb_output_flush, _head);
flb_output_flush_destroy(out_flush);
}
return 0;
}
/*
* It validate an output type given the string, it return the
* proper type and if valid, populate the global config.
*/
struct flb_output_instance *flb_output_new(struct flb_config *config,
const char *output, void *data,
int public_only)
{
int ret = -1;
int flags = 0;
struct mk_list *head;
struct flb_output_plugin *plugin;
struct flb_output_instance *instance = NULL;
if (!output) {
return NULL;
}
mk_list_foreach(head, &config->out_plugins) {
plugin = mk_list_entry(head, struct flb_output_plugin, _head);
if (!check_protocol(plugin->name, output)) {
plugin = NULL;
continue;
}
if (public_only && plugin->flags & FLB_OUTPUT_PRIVATE) {
return NULL;
}
break;
}
if (!plugin) {
return NULL;
}
/* Create and load instance */
instance = flb_calloc(1, sizeof(struct flb_output_instance));
if (!instance) {
flb_errno();
return NULL;
}
/* Initialize event type, if not set, default to FLB_OUTPUT_LOGS */
if (plugin->event_type == 0) {
instance->event_type = FLB_OUTPUT_LOGS;
}
else {
instance->event_type = plugin->event_type;
}
instance->config = config;
instance->log_level = -1;
instance->log_suppress_interval = -1;
instance->test_mode = FLB_FALSE;
instance->is_threaded = FLB_FALSE;
instance->tp_workers = plugin->workers;
/* Retrieve an instance id for the output instance */
instance->id = instance_id(config);
/* format name (with instance id) */
snprintf(instance->name, sizeof(instance->name) - 1,
"%s.%i", plugin->name, instance->id);
instance->p = plugin;
instance->callback = flb_callback_create(instance->name);
if (!instance->callback) {
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_task_queue_destroy(instance->singleplex_queue);
}
flb_free(instance);
return NULL;
}
if (plugin->type == FLB_OUTPUT_PLUGIN_CORE) {
instance->context = NULL;
}
else {
struct flb_plugin_proxy_context *ctx;
ctx = flb_calloc(1, sizeof(struct flb_plugin_proxy_context));
if (!ctx) {
flb_errno();
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_task_queue_destroy(instance->singleplex_queue);
}
flb_free(instance);
return NULL;
}
ctx->proxy = plugin->proxy;
instance->context = ctx;
}
instance->alias = NULL;
instance->flags = instance->p->flags;
instance->data = data;
instance->match = NULL;
#ifdef FLB_HAVE_REGEX
instance->match_regex = NULL;
#endif
instance->retry_limit = 1;
instance->host.name = NULL;
instance->host.address = NULL;
instance->net_config_map = NULL;
/* Storage */
instance->total_limit_size = -1;
/* Parent plugin flags */
flags = instance->flags;
if (flags & FLB_IO_TCP) {
instance->use_tls = FLB_FALSE;
}
else if (flags & FLB_IO_TLS) {
instance->use_tls = FLB_TRUE;
}
else if (flags & FLB_IO_OPT_TLS) {
/* TLS must be enabled manually in the config */
instance->use_tls = FLB_FALSE;
instance->flags |= FLB_IO_TLS;
}
#ifdef FLB_HAVE_TLS
instance->tls = NULL;
instance->tls_debug = -1;
instance->tls_verify = FLB_TRUE;
instance->tls_vhost = NULL;
instance->tls_ca_path = NULL;
instance->tls_ca_file = NULL;
instance->tls_crt_file = NULL;
instance->tls_key_file = NULL;
instance->tls_key_passwd = NULL;
#endif
if (plugin->flags & FLB_OUTPUT_NET) {
ret = flb_net_host_set(plugin->name, &instance->host, output);
if (ret != 0) {
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
flb_task_queue_destroy(instance->singleplex_queue);
}
flb_free(instance);
return NULL;
}
}
/* Create singleplex queue if SYNCHRONOUS mode is used */
instance->singleplex_queue = NULL;
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
instance->singleplex_queue = flb_task_queue_create();
if (!instance->singleplex_queue) {
flb_free(instance);
flb_errno();
return NULL;
}
}
flb_kv_init(&instance->properties);
flb_kv_init(&instance->net_properties);
mk_list_init(&instance->upstreams);
mk_list_init(&instance->flush_list);
mk_list_init(&instance->flush_list_destroy);
mk_list_add(&instance->_head, &config->outputs);
/* processor instance */
instance->processor = flb_processor_create(config, instance->name, instance, FLB_PLUGIN_OUTPUT);
/* Tests */
instance->test_formatter.callback = plugin->test_formatter.callback;
return instance;
}
static inline int prop_key_check(const char *key, const char *kv, int k_len)
{
int len;
len = strlen(key);
if (strncasecmp(key, kv, k_len) == 0 && len == k_len) {
return 0;
}
return -1;
}
/* Override a configuration property for the given input_instance plugin */
int flb_output_set_property(struct flb_output_instance *ins,
const char *k, const char *v)
{
int len;
int ret;
ssize_t limit;
flb_sds_t tmp;
struct flb_kv *kv;
struct flb_config *config = ins->config;
len = strlen(k);
tmp = flb_env_var_translate(config->env, v);
if (tmp) {
if (strlen(tmp) == 0) {
flb_sds_destroy(tmp);
tmp = NULL;
}
}
/* Check if the key is a known/shared property */
if (prop_key_check("match", k, len) == 0) {
ins->match = tmp;
}
#ifdef FLB_HAVE_REGEX
else if (prop_key_check("match_regex", k, len) == 0 && tmp) {
ins->match_regex = flb_regex_create(tmp);
flb_sds_destroy(tmp);
}
#endif
else if (prop_key_check("alias", k, len) == 0 && tmp) {
ins->alias = tmp;
}
else if (prop_key_check("log_level", k, len) == 0 && tmp) {
ret = flb_log_get_level_str(tmp);
flb_sds_destroy(tmp);
if (ret == -1) {
return -1;
}
ins->log_level = ret;
}
else if (prop_key_check("log_suppress_interval", k, len) == 0 && tmp) {
ret = flb_utils_time_to_seconds(tmp);
flb_sds_destroy(tmp);
if (ret == -1) {
return -1;
}
ins->log_suppress_interval = ret;
}
else if (prop_key_check("host", k, len) == 0) {
ins->host.name = tmp;
}
else if (prop_key_check("port", k, len) == 0) {
if (tmp) {
ins->host.port = atoi(tmp);
flb_sds_destroy(tmp);
}
else {
ins->host.port = 0;
}
}
else if (prop_key_check("ipv6", k, len) == 0 && tmp) {
ins->host.ipv6 = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
}
else if (prop_key_check("retry_limit", k, len) == 0) {
if (tmp) {
if (strcasecmp(tmp, "no_limits") == 0 ||
strcasecmp(tmp, "false") == 0 ||
strcasecmp(tmp, "off") == 0) {
/* No limits for retries */
ins->retry_limit = FLB_OUT_RETRY_UNLIMITED;
}
else if (strcasecmp(tmp, "no_retries") == 0) {
ins->retry_limit = FLB_OUT_RETRY_NONE;
}
else {
ins->retry_limit = atoi(tmp);
if (ins->retry_limit <= 0) {
flb_warn("[config] invalid retry_limit. set default.");
/* set default when input is invalid number */
ins->retry_limit = 1;
}
}
flb_sds_destroy(tmp);
}
else {
ins->retry_limit = 1;
}
}
else if (strncasecmp("net.", k, 4) == 0 && tmp) {
kv = flb_kv_item_create(&ins->net_properties, (char *) k, NULL);
if (!kv) {
if (tmp) {
flb_sds_destroy(tmp);
}
return -1;
}
kv->val = tmp;
}
#ifdef FLB_HAVE_HTTP_CLIENT_DEBUG
else if (strncasecmp("_debug.http.", k, 12) == 0 && tmp) {
ret = flb_http_client_debug_property_is_valid((char *) k, tmp);
if (ret == FLB_TRUE) {
kv = flb_kv_item_create(&ins->properties, (char *) k, NULL);
if (!kv) {
if (tmp) {
flb_sds_destroy(tmp);
}
return -1;
}
kv->val = tmp;
}
else {
flb_error("[config] invalid property '%s' on instance '%s'",
k, flb_output_name(ins));
flb_sds_destroy(tmp);
}
}
#endif
#ifdef FLB_HAVE_TLS
else if (prop_key_check("tls", k, len) == 0 && tmp) {
if (strcasecmp(tmp, "true") == 0 || strcasecmp(tmp, "on") == 0) {
if ((ins->flags & FLB_IO_TLS) == 0) {
flb_error("[config] %s don't support TLS", ins->name);
flb_sds_destroy(tmp);
return -1;
}
ins->use_tls = FLB_TRUE;
}
else {
ins->use_tls = FLB_FALSE;
}
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.verify", k, len) == 0 && tmp) {
if (strcasecmp(tmp, "true") == 0 || strcasecmp(tmp, "on") == 0) {
ins->tls_verify = FLB_TRUE;
}
else {
ins->tls_verify = FLB_FALSE;
}
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.debug", k, len) == 0 && tmp) {
ins->tls_debug = atoi(tmp);
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.vhost", k, len) == 0) {
ins->tls_vhost = tmp;
}
else if (prop_key_check("tls.ca_path", k, len) == 0) {
ins->tls_ca_path = tmp;
}
else if (prop_key_check("tls.ca_file", k, len) == 0) {
ins->tls_ca_file = tmp;
}
else if (prop_key_check("tls.crt_file", k, len) == 0) {
ins->tls_crt_file = tmp;
}
else if (prop_key_check("tls.key_file", k, len) == 0) {
ins->tls_key_file = tmp;
}
else if (prop_key_check("tls.key_passwd", k, len) == 0) {
ins->tls_key_passwd = tmp;
}
#endif
else if (prop_key_check("storage.total_limit_size", k, len) == 0 && tmp) {
if (strcasecmp(tmp, "off") == 0 ||
flb_utils_bool(tmp) == FLB_FALSE) {
/* no limit for filesystem storage */
limit = -1;
flb_info("[config] unlimited filesystem buffer for %s plugin",
ins->name);
}
else {
limit = flb_utils_size_to_bytes(tmp);
if (limit == -1) {
flb_sds_destroy(tmp);
return -1;
}
if (limit == 0) {
limit = -1;
}
}
flb_sds_destroy(tmp);
ins->total_limit_size = (size_t) limit;
}
else if (prop_key_check("workers", k, len) == 0 && tmp) {
/* Set the number of workers */
ins->tp_workers = atoi(tmp);
flb_sds_destroy(tmp);
}
else {
/*
* Create the property, we don't pass the value since we will
* map it directly to avoid an extra memory allocation.
*/
kv = flb_kv_item_create(&ins->properties, (char *) k, NULL);
if (!kv) {
if (tmp) {
flb_sds_destroy(tmp);
}
return -1;
}
kv->val = tmp;
}
return 0;
}
/* Configure a default hostname and TCP port if they are not set */
void flb_output_net_default(const char *host, const int port,
struct flb_output_instance *ins)
{
/* Set default network configuration */
if (!ins->host.name) {
ins->host.name = flb_sds_create(host);
}
if (ins->host.port == 0) {
ins->host.port = port;
}
}
/* Add thread pool for output plugin if configured with workers */
int flb_output_enable_multi_threading(struct flb_output_instance *ins, struct flb_config *config)
{
/* Multi-threading enabled ? (through 'workers' property) */
if (ins->tp_workers > 0) {
if(flb_output_thread_pool_create(config, ins) != 0) {
flb_output_instance_destroy(ins);
return -1;
}
flb_output_thread_pool_start(ins);
}
return 0;
}
/* Return an instance name or alias */
const char *flb_output_name(struct flb_output_instance *ins)
{
if (ins->alias) {
return ins->alias;
}
return ins->name;
}
const char *flb_output_get_property(const char *key, struct flb_output_instance *ins)
{
return flb_config_prop_get(key, &ins->properties);
}
#ifdef FLB_HAVE_METRICS
void *flb_output_get_cmt_instance(struct flb_output_instance *ins)
{