-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathkafka.c
More file actions
1561 lines (1361 loc) · 52 KB
/
Copy pathkafka.c
File metadata and controls
1561 lines (1361 loc) · 52 KB
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-2026 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 <inttypes.h>
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include <fluent-bit/flb_opentelemetry.h>
#include <fluent-bit/aws/flb_aws_msk_iam.h>
#include <fluent-bit/flb_kafka.h>
#include <cmetrics/cmt_encode_opentelemetry.h>
#include <cfl/cfl_hash.h>
#include <ctraces/ctr_decode_msgpack.h>
#include <ctraces/ctr_encode_opentelemetry.h>
#include "kafka_config.h"
#include "kafka_topic.h"
#define FLB_OTEL_LOGS_SCHEMA_KEY "schema"
#define FLB_OTEL_LOGS_SCHEMA_OTLP "otlp"
#define FLB_KAFKA_PARTIAL_QUEUE_FULL_RETRIES 10
struct otlp_logs_resource_partition {
int64_t resource_id;
uint64_t resource_hash;
int has_key;
char key[17];
msgpack_sbuffer buffer;
};
static const char *default_logs_body_keys[] = {"log", "message"};
static void init_otlp_logs_options(struct flb_opentelemetry_otlp_logs_options *options)
{
memset(options, 0, sizeof(*options));
options->logs_require_otel_metadata = FLB_FALSE;
options->logs_body_keys = default_logs_body_keys;
options->logs_body_key_count = sizeof(default_logs_body_keys) /
sizeof(default_logs_body_keys[0]);
options->logs_body_key_attributes = FLB_FALSE;
}
void cb_kafka_msg(rd_kafka_t *rk, const rd_kafka_message_t *rkmessage,
void *opaque)
{
struct flb_kafka_opaque *op = (struct flb_kafka_opaque *) opaque;
struct flb_out_kafka *ctx = op ? (struct flb_out_kafka *) op->ptr : NULL;
if (rkmessage->err) {
flb_plg_warn(ctx->ins, "message delivery failed: %s",
rd_kafka_err2str(rkmessage->err));
}
else {
flb_plg_debug(ctx->ins, "message delivered (%zd bytes, "
"partition %"PRId32")",
rkmessage->len, rkmessage->partition);
}
}
void cb_kafka_logger(const rd_kafka_t *rk, int level,
const char *fac, const char *buf)
{
struct flb_kafka_opaque *op;
struct flb_out_kafka *ctx;
op = (struct flb_kafka_opaque *) rd_kafka_opaque(rk);
ctx = op ? (struct flb_out_kafka *) op->ptr : NULL;
if (level <= FLB_KAFKA_LOG_ERR) {
flb_plg_error(ctx->ins, "%s: %s",
rk ? rd_kafka_name(rk) : NULL, buf);
}
else if (level == FLB_KAFKA_LOG_WARNING) {
flb_plg_warn(ctx->ins, "%s: %s",
rk ? rd_kafka_name(rk) : NULL, buf);
}
else if (level == FLB_KAFKA_LOG_NOTICE || level == FLB_KAFKA_LOG_INFO) {
flb_plg_info(ctx->ins, "%s: %s",
rk ? rd_kafka_name(rk) : NULL, buf);
}
else if (level == FLB_KAFKA_LOG_DEBUG) {
flb_plg_debug(ctx->ins, "%s: %s",
rk ? rd_kafka_name(rk) : NULL, buf);
}
}
static int cb_kafka_init(struct flb_output_instance *ins,
struct flb_config *config,
void *data)
{
struct flb_out_kafka *ctx;
/* Configuration */
ctx = flb_out_kafka_create(ins, config);
if (!ctx) {
flb_plg_error(ins, "failed to initialize");
return -1;
}
/* Set global context */
flb_output_set_context(ins, ctx);
return 0;
}
int produce_message(struct flb_time *tm, msgpack_object *map,
struct flb_out_kafka *ctx, struct flb_config *config)
{
int i;
int ret;
int size;
int queue_full_retries = 0;
char *out_buf;
size_t out_size;
struct mk_list *head;
struct mk_list *topics;
struct flb_split_entry *entry;
char *dynamic_topic;
char *message_key = NULL;
size_t message_key_len = 0;
flb_sds_t raw_key = NULL;
struct flb_kafka_topic *topic = NULL;
msgpack_sbuffer mp_sbuf;
msgpack_packer mp_pck;
msgpack_object key;
msgpack_object val;
flb_sds_t s = NULL;
#ifdef FLB_HAVE_AVRO_ENCODER
// used to flag when a buffer needs to be freed for avro
bool avro_fast_buffer = true;
// avro encoding uses a buffer
// the majority of lines are fairly small
// so using static buffer for these is much more efficient
// larger sizes will allocate
#ifndef AVRO_DEFAULT_BUFFER_SIZE
#define AVRO_DEFAULT_BUFFER_SIZE 2048
#endif
static char avro_buff[AVRO_DEFAULT_BUFFER_SIZE];
// don't take lines that are too large
// these lines will log a warning
// this roughly a log line of 250000 chars
#ifndef AVRO_LINE_MAX_LEN
#define AVRO_LINE_MAX_LEN 1000000
// this is a convenience
#define AVRO_FREE(X, Y) if (!X) { flb_free(Y); }
#endif
// this is just to keep the code cleaner
// the avro encoding includes
// an embedded schemaid which is used
// the embedding is a null byte
// followed by a 16 byte schemaid
#define AVRO_SCHEMA_OVERHEAD 4 + 1
#endif
flb_debug("in produce_message\n");
if (flb_log_check(FLB_LOG_DEBUG))
msgpack_object_print(stderr, *map);
/* Init temporal buffers */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
if (ctx->format == FLB_KAFKA_FMT_JSON || ctx->format == FLB_KAFKA_FMT_MSGP) {
/* Make room for the timestamp */
size = map->via.map.size + 1;
msgpack_pack_map(&mp_pck, size);
/* Pack timestamp */
msgpack_pack_str(&mp_pck, ctx->timestamp_key_len);
msgpack_pack_str_body(&mp_pck,
ctx->timestamp_key, ctx->timestamp_key_len);
switch (ctx->timestamp_format) {
case FLB_JSON_DATE_DOUBLE:
msgpack_pack_double(&mp_pck, flb_time_to_double(tm));
break;
case FLB_JSON_DATE_ISO8601:
case FLB_JSON_DATE_ISO8601_NS:
{
size_t date_len;
int len;
struct tm _tm;
char time_formatted[36];
/* Format the time; use microsecond precision (not nanoseconds). */
gmtime_r(&tm->tm.tv_sec, &_tm);
date_len = strftime(time_formatted, sizeof(time_formatted) - 1,
FLB_JSON_DATE_ISO8601_FMT, &_tm);
if (ctx->timestamp_format == FLB_JSON_DATE_ISO8601) {
len = snprintf(time_formatted + date_len, sizeof(time_formatted) - 1 - date_len,
".%06" PRIu64 "Z", (uint64_t) tm->tm.tv_nsec / 1000);
}
else {
/* FLB_JSON_DATE_ISO8601_NS */
len = snprintf(time_formatted + date_len, sizeof(time_formatted) - 1 - date_len,
".%09" PRIu64 "Z", (uint64_t) tm->tm.tv_nsec);
}
date_len += len;
msgpack_pack_str(&mp_pck, date_len);
msgpack_pack_str_body(&mp_pck, time_formatted, date_len);
}
break;
}
}
else {
size = map->via.map.size;
msgpack_pack_map(&mp_pck, size);
}
for (i = 0; i < map->via.map.size; i++) {
key = map->via.map.ptr[i].key;
val = map->via.map.ptr[i].val;
msgpack_pack_object(&mp_pck, key);
msgpack_pack_object(&mp_pck, val);
/* Lookup message key */
if (ctx->message_key_field && !message_key && val.type == MSGPACK_OBJECT_STR) {
if (key.via.str.size == ctx->message_key_field_len &&
strncmp(key.via.str.ptr, ctx->message_key_field, ctx->message_key_field_len) == 0) {
message_key = (char *) val.via.str.ptr;
message_key_len = val.via.str.size;
}
}
/* Lookup raw_log_key */
if (ctx->raw_log_key && ctx->format == FLB_KAFKA_FMT_RAW && !raw_key && val.type == MSGPACK_OBJECT_STR) {
if (key.via.str.size == ctx->raw_log_key_len &&
strncmp(key.via.str.ptr, ctx->raw_log_key, ctx->raw_log_key_len) == 0) {
raw_key = flb_sds_create_len(val.via.str.ptr, val.via.str.size);
}
}
/* Lookup key/topic */
if (ctx->topic_key && !topic && val.type == MSGPACK_OBJECT_STR) {
if (key.via.str.size == ctx->topic_key_len &&
strncmp(key.via.str.ptr, ctx->topic_key, ctx->topic_key_len) == 0) {
topic = flb_kafka_topic_lookup((char *) val.via.str.ptr,
val.via.str.size, ctx);
/* Add extracted topic on the fly to topiclist */
if (ctx->dynamic_topic) {
/* Only if default topic is set and this topicname is not set for this message */
if (strncmp(topic->name, flb_kafka_topic_default(ctx)->name, val.via.str.size) == 0 &&
(strncmp(topic->name, val.via.str.ptr, val.via.str.size) != 0) ) {
if (memchr(val.via.str.ptr, ',', val.via.str.size)) {
/* Don't allow commas in kafkatopic name */
flb_warn("',' not allowed in dynamic_kafka topic names");
continue;
}
if (val.via.str.size > 249) {
/* Don't allow length of dynamic kafka topics > 249 */
flb_warn(" dynamic kafka topic length > 249 not allowed");
continue;
}
dynamic_topic = flb_malloc(val.via.str.size + 1);
if (!dynamic_topic) {
/* Use default topic */
flb_errno();
continue;
}
strncpy(dynamic_topic, val.via.str.ptr, val.via.str.size);
dynamic_topic[val.via.str.size] = '\0';
topics = flb_utils_split(dynamic_topic, ',', 0);
if (!topics) {
/* Use the default topic */
flb_errno();
flb_free(dynamic_topic);
continue;
}
mk_list_foreach(head, topics) {
/* Add the (one) found topicname to the topic configuration */
entry = mk_list_entry(head, struct flb_split_entry, _head);
topic = flb_kafka_topic_create(entry->value, ctx);
if (!topic) {
/* Use default topic */
flb_error("[out_kafka] cannot register topic '%s'",
entry->value);
topic = flb_kafka_topic_lookup((char *) val.via.str.ptr,
val.via.str.size, ctx);
}
else {
flb_info("[out_kafka] new topic added: %s", dynamic_topic);
}
}
flb_utils_split_free(topics);
flb_free(dynamic_topic);
}
}
}
}
}
if (ctx->format == FLB_KAFKA_FMT_JSON) {
s = flb_msgpack_raw_to_json_sds(mp_sbuf.data, mp_sbuf.size,
config->json_escape_unicode);
if (!s) {
flb_plg_error(ctx->ins, "error encoding to JSON");
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_ERROR;
}
out_buf = s;
out_size = flb_sds_len(out_buf);
}
else if (ctx->format == FLB_KAFKA_FMT_MSGP) {
out_buf = mp_sbuf.data;
out_size = mp_sbuf.size;
}
else if (ctx->format == FLB_KAFKA_FMT_GELF) {
s = flb_msgpack_raw_to_gelf(mp_sbuf.data, mp_sbuf.size,
tm, &(ctx->gelf_fields));
if (s == NULL) {
flb_plg_error(ctx->ins, "error encoding to GELF");
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_ERROR;
}
out_buf = s;
out_size = flb_sds_len(s);
}
#ifdef FLB_HAVE_AVRO_ENCODER
else if (ctx->format == FLB_KAFKA_FMT_AVRO) {
flb_plg_debug(ctx->ins, "avro schema ID:%d:\n", ctx->avro_fields.schema_id);
flb_plg_debug(ctx->ins, "avro schema string:%s:\n", ctx->avro_fields.schema_str);
// if there's no data then log it and return
if (mp_sbuf.size == 0) {
flb_plg_error(ctx->ins, "got zero bytes decoding to avro AVRO:schemaID:%d:\n", ctx->avro_fields.schema_id);
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_OK;
}
// is the line is too long log it and return
if (mp_sbuf.size > AVRO_LINE_MAX_LEN) {
flb_plg_warn(ctx->ins, "skipping long line AVRO:len:%zu:limit:%zu:schemaID:%d:\n", (size_t)mp_sbuf.size, (size_t)AVRO_LINE_MAX_LEN, ctx->avro_fields.schema_id);
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_OK;
}
flb_plg_debug(ctx->ins, "using default buffer AVRO:len:%zu:limit:%zu:schemaID:%d:\n", (size_t)mp_sbuf.size, (size_t)AVRO_DEFAULT_BUFFER_SIZE, ctx->avro_fields.schema_id);
out_buf = avro_buff;
out_size = AVRO_DEFAULT_BUFFER_SIZE;
if (mp_sbuf.size + AVRO_SCHEMA_OVERHEAD >= AVRO_DEFAULT_BUFFER_SIZE) {
flb_plg_info(ctx->ins, "upsizing to dynamic buffer AVRO:len:%zu:schemaID:%d:\n", (size_t)mp_sbuf.size, ctx->avro_fields.schema_id);
avro_fast_buffer = false;
// avro will always be smaller than msgpack
// it contains no meta-info aside from the schemaid
// all the metadata is in the schema which is not part of the msg
// add schemaid + magic byte for safety buffer and allocate
// that's 16 byte schemaid and one byte magic byte
out_size = mp_sbuf.size + AVRO_SCHEMA_OVERHEAD;
out_buf = flb_malloc(out_size);
if (!out_buf) {
flb_plg_error(ctx->ins, "error allocating memory for decoding to AVRO:schema:%s:schemaID:%d:\n", ctx->avro_fields.schema_str, ctx->avro_fields.schema_id);
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_ERROR;
}
}
if(!flb_msgpack_raw_to_avro_sds(mp_sbuf.data, mp_sbuf.size, &ctx->avro_fields, out_buf, &out_size)) {
flb_plg_error(ctx->ins, "error encoding to AVRO:schema:%s:schemaID:%d:\n", ctx->avro_fields.schema_str, ctx->avro_fields.schema_id);
msgpack_sbuffer_destroy(&mp_sbuf);
if (!avro_fast_buffer) {
flb_free(out_buf);
}
return FLB_ERROR;
}
}
#endif
else if (ctx->format == FLB_KAFKA_FMT_RAW) {
if (raw_key == NULL) {
flb_plg_error(ctx->ins, "missing raw_log_key");
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_ERROR;
}
out_buf = raw_key;
out_size = flb_sds_len(raw_key);
}
if (!message_key) {
message_key = ctx->message_key;
message_key_len = ctx->message_key_len;
}
if (!topic) {
topic = flb_kafka_topic_default(ctx);
}
if (!topic) {
flb_plg_error(ctx->ins, "no default topic found");
msgpack_sbuffer_destroy(&mp_sbuf);
#ifdef FLB_HAVE_AVRO_ENCODER
if (ctx->format == FLB_KAFKA_FMT_AVRO) {
AVRO_FREE(avro_fast_buffer, out_buf)
}
#endif
flb_sds_destroy(raw_key);
return FLB_ERROR;
}
retry:
/*
* If the local rdkafka queue is full, we retry up to 'queue_full_retries'
* times set by the configuration (default: 10). If the configuration
* property was set to '0' or 'false', we don't impose a limit. Use that
* value under your own risk.
*/
if (ctx->queue_full_retries > 0 &&
queue_full_retries >= ctx->queue_full_retries) {
if (ctx->format != FLB_KAFKA_FMT_MSGP) {
flb_sds_destroy(s);
}
msgpack_sbuffer_destroy(&mp_sbuf);
#ifdef FLB_HAVE_AVRO_ENCODER
if (ctx->format == FLB_KAFKA_FMT_AVRO) {
AVRO_FREE(avro_fast_buffer, out_buf)
}
#endif
flb_sds_destroy(raw_key);
/*
* Unblock the flush requests so that the
* engine could try sending data again.
*/
ctx->blocked = FLB_FALSE;
return FLB_RETRY;
}
ret = rd_kafka_produce(topic->tp,
RD_KAFKA_PARTITION_UA,
RD_KAFKA_MSG_F_COPY,
out_buf, out_size,
message_key, message_key_len,
ctx);
if (ret == -1) {
flb_error(
"%% Failed to produce to topic %s: %s\n",
rd_kafka_topic_name(topic->tp),
rd_kafka_err2str(rd_kafka_last_error()));
/*
* rdkafka queue is full, keep trying 'locally' for a few seconds,
* otherwise let the caller to issue a main retry againt the engine.
*/
if (rd_kafka_last_error() == RD_KAFKA_RESP_ERR__QUEUE_FULL) {
flb_plg_warn(ctx->ins,
"internal queue is full, retrying in one second");
/*
* If the queue is full, first make sure to discard any further
* flush request from the engine. This means 'the caller will
* issue a retry at a later time'.
*/
ctx->blocked = FLB_TRUE;
/*
* Next step is to give it some time to the background rdkafka
* library to do it own work. By default rdkafka wait 1 second
* or up to 10000 messages to be enqueued before delivery.
*
* If the kafka broker is down we should try a couple of times
* to enqueue this message, if we exceed 10 times, we just
* issue a full retry of the data chunk.
*/
flb_time_sleep(1000);
rd_kafka_poll(ctx->kafka.rk, 0);
/* Issue a re-try */
queue_full_retries++;
goto retry;
}
ctx->blocked = FLB_FALSE;
if (ctx->format == FLB_KAFKA_FMT_JSON ||
ctx->format == FLB_KAFKA_FMT_GELF) {
flb_sds_destroy(s);
}
msgpack_sbuffer_destroy(&mp_sbuf);
#ifdef FLB_HAVE_AVRO_ENCODER
if (ctx->format == FLB_KAFKA_FMT_AVRO) {
AVRO_FREE(avro_fast_buffer, out_buf)
}
#endif
flb_sds_destroy(raw_key);
return FLB_ERROR;
}
else {
flb_plg_debug(ctx->ins, "enqueued message (%zd bytes) for topic '%s'",
out_size, rd_kafka_topic_name(topic->tp));
}
ctx->blocked = FLB_FALSE;
rd_kafka_poll(ctx->kafka.rk, 0);
if (ctx->format == FLB_KAFKA_FMT_JSON) {
flb_sds_destroy(s);
}
if (ctx->format == FLB_KAFKA_FMT_GELF) {
flb_sds_destroy(s);
}
#ifdef FLB_HAVE_AVRO_ENCODER
if (ctx->format == FLB_KAFKA_FMT_AVRO) {
AVRO_FREE(avro_fast_buffer, out_buf)
}
#endif
flb_sds_destroy(raw_key);
msgpack_sbuffer_destroy(&mp_sbuf);
return FLB_OK;
}
static int produce_raw_payload_with_key_retry_control(const void *payload,
size_t payload_size,
char *key,
size_t key_len,
int use_default_key,
int allow_engine_retry,
struct flb_out_kafka *ctx)
{
int ret;
int queue_full_retries;
int queue_full_retry_limit;
char *message_key;
size_t message_key_len;
struct flb_kafka_topic *topic;
if (payload == NULL || payload_size == 0) {
return FLB_OK;
}
queue_full_retries = 0;
queue_full_retry_limit = ctx->queue_full_retries;
if (key != NULL) {
message_key = key;
message_key_len = key_len;
}
else if (use_default_key == FLB_TRUE) {
message_key = ctx->message_key;
message_key_len = ctx->message_key_len;
}
else {
message_key = NULL;
message_key_len = 0;
}
if (queue_full_retry_limit <= 0 && allow_engine_retry == FLB_FALSE) {
queue_full_retry_limit = FLB_KAFKA_PARTIAL_QUEUE_FULL_RETRIES;
}
topic = flb_kafka_topic_default(ctx);
if (topic == NULL) {
flb_plg_error(ctx->ins, "no default topic found");
return FLB_ERROR;
}
retry:
if (queue_full_retry_limit > 0 &&
queue_full_retries >= queue_full_retry_limit) {
ctx->blocked = FLB_FALSE;
if (allow_engine_retry == FLB_TRUE) {
return FLB_RETRY;
}
flb_plg_error(ctx->ins,
"failed to produce partitioned OTLP payload to topic %s: "
"internal queue is full after %d retries",
rd_kafka_topic_name(topic->tp),
queue_full_retries);
return FLB_ERROR;
}
ret = rd_kafka_produce(topic->tp,
RD_KAFKA_PARTITION_UA,
RD_KAFKA_MSG_F_COPY,
(void *) payload,
payload_size,
message_key,
message_key_len,
ctx);
if (ret == -1) {
if (rd_kafka_last_error() == RD_KAFKA_RESP_ERR__QUEUE_FULL) {
ctx->blocked = FLB_TRUE;
flb_time_sleep(1000);
rd_kafka_poll(ctx->kafka.rk, 0);
queue_full_retries++;
goto retry;
}
ctx->blocked = FLB_FALSE;
flb_plg_error(ctx->ins,
"failed to produce OTLP payload to topic %s: %s",
rd_kafka_topic_name(topic->tp),
rd_kafka_err2str(rd_kafka_last_error()));
return FLB_ERROR;
}
ctx->blocked = FLB_FALSE;
rd_kafka_poll(ctx->kafka.rk, 0);
return FLB_OK;
}
static int produce_raw_payload_with_key(const void *payload, size_t payload_size,
char *key, size_t key_len,
struct flb_out_kafka *ctx)
{
return produce_raw_payload_with_key_retry_control(payload,
payload_size,
key,
key_len,
FLB_TRUE,
FLB_TRUE,
ctx);
}
static int produce_raw_payload(const void *payload, size_t payload_size,
struct flb_out_kafka *ctx)
{
return produce_raw_payload_with_key(payload, payload_size, NULL, 0, ctx);
}
static msgpack_object *msgpack_map_get_object(msgpack_object_map *map,
const char *key)
{
size_t index;
size_t key_length;
msgpack_object_kv *entry;
if (map == NULL || key == NULL) {
return NULL;
}
key_length = strlen(key);
for (index = 0; index < map->size; index++) {
entry = &map->ptr[index];
if (entry->key.type != MSGPACK_OBJECT_STR) {
continue;
}
if (entry->key.via.str.size != key_length) {
continue;
}
if (strncmp(entry->key.via.str.ptr, key, key_length) == 0) {
return &entry->val;
}
}
return NULL;
}
static int msgpack_map_entry_is_string(msgpack_object_map *map,
const char *key,
const char *expected)
{
msgpack_object *value;
value = msgpack_map_get_object(map, key);
if (value == NULL || value->type != MSGPACK_OBJECT_STR) {
return FLB_FALSE;
}
if (value->via.str.size != strlen(expected)) {
return FLB_FALSE;
}
if (strncmp(value->via.str.ptr, expected, value->via.str.size) != 0) {
return FLB_FALSE;
}
return FLB_TRUE;
}
static int msgpack_map_get_int64(msgpack_object_map *map,
const char *key,
int64_t *output)
{
msgpack_object *value;
value = msgpack_map_get_object(map, key);
if (value == NULL) {
return -1;
}
if (value->type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
*output = (int64_t) value->via.u64;
return 0;
}
else if (value->type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
*output = value->via.i64;
return 0;
}
return -1;
}
static uint64_t msgpack_object_hash(msgpack_object *object)
{
uint64_t hash;
msgpack_sbuffer buffer;
msgpack_packer packer;
if (object == NULL) {
return cfl_hash_64bits("null", 4);
}
msgpack_sbuffer_init(&buffer);
msgpack_packer_init(&packer, &buffer, msgpack_sbuffer_write);
if (msgpack_pack_object(&packer, *object) != 0) {
msgpack_sbuffer_destroy(&buffer);
return 0;
}
hash = cfl_hash_64bits(buffer.data, buffer.size);
msgpack_sbuffer_destroy(&buffer);
return hash;
}
static uint64_t msgpack_object_pair_hash(msgpack_object *left,
msgpack_object *right)
{
uint64_t hash;
msgpack_sbuffer buffer;
msgpack_packer packer;
msgpack_sbuffer_init(&buffer);
msgpack_packer_init(&packer, &buffer, msgpack_sbuffer_write);
if (msgpack_pack_array(&packer, 2) != 0) {
msgpack_sbuffer_destroy(&buffer);
return 0;
}
if (left == NULL) {
msgpack_pack_nil(&packer);
}
else if (msgpack_pack_object(&packer, *left) != 0) {
msgpack_sbuffer_destroy(&buffer);
return 0;
}
if (right == NULL) {
msgpack_pack_nil(&packer);
}
else if (msgpack_pack_object(&packer, *right) != 0) {
msgpack_sbuffer_destroy(&buffer);
return 0;
}
hash = cfl_hash_64bits(buffer.data, buffer.size);
msgpack_sbuffer_destroy(&buffer);
return hash;
}
static msgpack_object *resource_schema_url_object(msgpack_object *resource_object,
msgpack_object *resource_body)
{
msgpack_object *schema_url;
if (resource_body != NULL && resource_body->type == MSGPACK_OBJECT_MAP) {
schema_url = msgpack_map_get_object(&resource_body->via.map, "schema_url");
if (schema_url != NULL) {
return schema_url;
}
}
if (resource_object != NULL && resource_object->type == MSGPACK_OBJECT_MAP) {
schema_url = msgpack_map_get_object(&resource_object->via.map, "schema_url");
if (schema_url != NULL) {
return schema_url;
}
}
return NULL;
}
static uint64_t resource_identity_hash(msgpack_object *resource_object,
msgpack_object *resource_body)
{
msgpack_object *schema_url;
schema_url = resource_schema_url_object(resource_object, resource_body);
return msgpack_object_pair_hash(resource_object, schema_url);
}
static uint64_t resource_attributes_hash(msgpack_object *resource_object)
{
msgpack_object *attributes;
if (resource_object == NULL || resource_object->type != MSGPACK_OBJECT_MAP) {
return msgpack_object_hash(NULL);
}
attributes = msgpack_map_get_object(&resource_object->via.map, "attributes");
return msgpack_object_hash(attributes);
}
static void destroy_otlp_logs_partitions(
struct otlp_logs_resource_partition *partitions,
size_t count)
{
size_t index;
if (partitions == NULL) {
return;
}
for (index = 0; index < count; index++) {
msgpack_sbuffer_destroy(&partitions[index].buffer);
}
flb_free(partitions);
}
static struct otlp_logs_resource_partition *find_otlp_logs_partition(
struct otlp_logs_resource_partition *partitions,
size_t count,
int64_t resource_id,
uint64_t resource_hash,
int has_key)
{
size_t index;
for (index = 0; index < count; index++) {
if (partitions[index].resource_id == resource_id &&
partitions[index].resource_hash == resource_hash &&
partitions[index].has_key == has_key) {
return &partitions[index];
}
}
return NULL;
}
static struct otlp_logs_resource_partition *get_otlp_logs_partition(
struct otlp_logs_resource_partition **partitions,
size_t *count,
int64_t resource_id,
uint64_t resource_hash,
uint64_t key_hash,
int has_key)
{
struct otlp_logs_resource_partition *partition;
struct otlp_logs_resource_partition *tmp;
partition = find_otlp_logs_partition(*partitions,
*count,
resource_id,
resource_hash,
has_key);
if (partition != NULL) {
return partition;
}
tmp = flb_realloc(*partitions,
sizeof(struct otlp_logs_resource_partition) * (*count + 1));
if (tmp == NULL) {
flb_errno();
return NULL;
}
*partitions = tmp;
partition = &(*partitions)[*count];
memset(partition, 0, sizeof(struct otlp_logs_resource_partition));
partition->resource_id = resource_id;
partition->resource_hash = resource_hash;
partition->has_key = has_key;
if (has_key == FLB_TRUE) {
snprintf(partition->key, sizeof(partition->key), "%016" PRIx64, key_hash);
}
msgpack_sbuffer_init(&partition->buffer);
(*count)++;
return partition;
}
static int append_partition_record(
struct otlp_logs_resource_partition *partition,
struct flb_log_event_decoder *decoder)
{
if (partition == NULL || decoder->record_base == NULL ||
decoder->record_length == 0) {
return 0;
}
return msgpack_sbuffer_write(&partition->buffer,
decoder->record_base,
decoder->record_length);
}
static int get_otlp_group_resource(msgpack_object *group_metadata,
msgpack_object *group_body,
int64_t *resource_id,
msgpack_object **resource_object)
{
int ret;
int64_t scope_id;
if (group_metadata == NULL ||
group_metadata->type != MSGPACK_OBJECT_MAP ||
msgpack_map_entry_is_string(&group_metadata->via.map,
FLB_OTEL_LOGS_SCHEMA_KEY,
FLB_OTEL_LOGS_SCHEMA_OTLP) != FLB_TRUE ||
msgpack_map_get_int64(&group_metadata->via.map,
"resource_id",
resource_id) != 0) {
return -1;
}
if (group_body != NULL && group_body->type == MSGPACK_OBJECT_MAP) {
*resource_object = msgpack_map_get_object(&group_body->via.map,
"resource");
}
else {
*resource_object = NULL;
}
ret = msgpack_map_get_int64(&group_metadata->via.map, "scope_id", &scope_id);
if (ret != 0) {
return -1;
}
(void) scope_id;
return 0;
}
static int produce_partitioned_otlp_logs(struct flb_out_kafka *ctx,
struct flb_event_chunk *event_chunk,
int format)
{
int ret;
int result;
int32_t record_type;
int64_t resource_id;
uint64_t resource_hash;
uint64_t key_hash;
flb_sds_t payload;
char *key;
size_t key_len;
size_t index;
size_t partition_count;
msgpack_object *group_body;
msgpack_object *group_metadata;
msgpack_object *resource_object;
struct flb_log_event event;
struct flb_log_event_decoder decoder;
struct otlp_logs_resource_partition *partition;
struct otlp_logs_resource_partition *current_partition;
struct otlp_logs_resource_partition *partitions;
struct flb_opentelemetry_otlp_logs_options options;
size_t produced_count;
partitions = NULL;
partition_count = 0;
current_partition = NULL;
produced_count = 0;
ret = flb_log_event_decoder_init(&decoder,
(char *) event_chunk->data,
event_chunk->size);
if (ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"could not decode OTLP log chunk for partitioning: %d",
ret);
return FLB_ERROR;