2727#include "kafka_topic.h"
2828#include "kafka_callbacks.h"
2929
30- struct flb_out_kafka * flb_kafka_conf_create (struct flb_output_instance * ins ,
31- struct flb_config * config )
30+ struct flb_out_kafka * flb_out_kafka_create (struct flb_output_instance * ins ,
31+ struct flb_config * config )
3232{
33- int ret ;
3433 const char * tmp ;
3534 char errstr [512 ];
3635 struct mk_list * head ;
3736 struct mk_list * topics ;
3837 struct flb_split_entry * entry ;
3938 struct flb_out_kafka * ctx ;
40- struct flb_kv * kv ;
4139
4240 /* Configuration context */
4341 ctx = flb_calloc (1 , sizeof (struct flb_out_kafka ));
@@ -49,55 +47,13 @@ struct flb_out_kafka *flb_kafka_conf_create(struct flb_output_instance *ins,
4947 ctx -> blocked = FLB_FALSE ;
5048
5149 /* rdkafka config context */
52- ctx -> conf = rd_kafka_conf_new ( );
50+ ctx -> conf = flb_kafka_conf_create ( & ctx -> kafka , & ins -> properties , 0 );
5351 if (!ctx -> conf ) {
5452 flb_plg_error (ctx -> ins , "error creating context" );
5553 flb_free (ctx );
5654 return NULL ;
5755 }
5856
59- /* rdkafka configuration parameters */
60- ret = rd_kafka_conf_set (ctx -> conf , "client.id" , "fluent-bit" ,
61- errstr , sizeof (errstr ));
62- if (ret != RD_KAFKA_CONF_OK ) {
63- flb_plg_error (ctx -> ins , "cannot configure client.id" );
64- }
65-
66- /* Config: Brokers */
67- tmp = flb_output_get_property ("brokers" , ins );
68- if (tmp ) {
69- ret = rd_kafka_conf_set (ctx -> conf ,
70- "bootstrap.servers" ,
71- tmp ,
72- errstr , sizeof (errstr ));
73- if (ret != RD_KAFKA_CONF_OK ) {
74- flb_plg_error (ctx -> ins , "config: %s" , errstr );
75- flb_free (ctx );
76- return NULL ;
77- }
78- ctx -> brokers = flb_strdup (tmp );
79- }
80- else {
81- flb_plg_error (ctx -> ins , "config: no brokers defined" );
82- flb_free (ctx );
83- return NULL ;
84- }
85-
86- /* Iterate custom rdkafka properties */
87- mk_list_foreach (head , & ins -> properties ) {
88- kv = mk_list_entry (head , struct flb_kv , _head );
89- if (strncasecmp (kv -> key , "rdkafka." , 8 ) == 0 &&
90- flb_sds_len (kv -> key ) > 8 ) {
91-
92- ret = rd_kafka_conf_set (ctx -> conf , kv -> key + 8 , kv -> val ,
93- errstr , sizeof (errstr ));
94- if (ret != RD_KAFKA_CONF_OK ) {
95- flb_plg_error (ctx -> ins , "cannot configure '%s' property" ,
96- kv -> key + 8 );
97- }
98- }
99- }
100-
10157 /* Set our global opaque data (plugin context*/
10258 rd_kafka_conf_set_opaque (ctx -> conf , ctx );
10359
@@ -234,12 +190,12 @@ struct flb_out_kafka *flb_kafka_conf_create(struct flb_output_instance *ins,
234190 }
235191
236192 /* Kafka Producer */
237- ctx -> producer = rd_kafka_new (RD_KAFKA_PRODUCER , ctx -> conf ,
193+ ctx -> kafka . rk = rd_kafka_new (RD_KAFKA_PRODUCER , ctx -> conf ,
238194 errstr , sizeof (errstr ));
239- if (!ctx -> producer ) {
195+ if (!ctx -> kafka . rk ) {
240196 flb_plg_error (ctx -> ins , "failed to create producer: %s" ,
241197 errstr );
242- flb_kafka_conf_destroy (ctx );
198+ flb_out_kafka_destroy (ctx );
243199 return NULL ;
244200 }
245201
@@ -280,28 +236,28 @@ struct flb_out_kafka *flb_kafka_conf_create(struct flb_output_instance *ins,
280236 }
281237 }
282238
283- flb_plg_info (ctx - > ins , "brokers = '%s' topics = '%s' ", ctx - > brokers , tmp );
239+ flb_plg_info (ctx - > ins , "brokers = '%s' topics = '%s' ", ctx - > kafka . brokers , tmp );
284240#ifdef FLB_HAVE_AVRO_ENCODER
285241 flb_plg_info (ctx - > ins , "schemaID = '%s' schema = '%s' ", ctx - > avro_fields .schema_id , ctx - > avro_fields .schema_str );
286242#endif
287243
288244 return ctx ;
289245}
290246
291- int flb_kafka_conf_destroy (struct flb_out_kafka * ctx )
247+ int flb_out_kafka_destroy (struct flb_out_kafka * ctx )
292248{
293249 if (!ctx ) {
294250 return 0 ;
295251 }
296252
297- if (ctx -> brokers ) {
298- flb_free (ctx - > brokers );
253+ if (ctx -> kafka . brokers ) {
254+ flb_free (ctx - > kafka . brokers );
299255 }
300256
301257 flb_kafka_topic_destroy_all (ctx );
302258
303- if (ctx -> producer ) {
304- rd_kafka_destroy (ctx - > producer );
259+ if (ctx -> kafka . rk ) {
260+ rd_kafka_destroy (ctx - > kafka . rk );
305261 }
306262
307263 if (ctx -> topic_key ) {
0 commit comments