@@ -78,12 +78,27 @@ static int http_put(struct flb_out_doris *ctx,
7878 struct flb_upstream * u ;
7979 struct flb_connection * u_conn ;
8080 struct flb_http_client * c ;
81+ struct mk_list * head ;
82+ struct flb_config_map_val * mv ;
83+ struct flb_slist_entry * key = NULL ;
84+ struct flb_slist_entry * val = NULL ;
85+
86+ int i ;
87+ int root_type ;
88+ char * out_buf ;
89+ size_t off = 0 ;
90+ size_t out_size ;
91+ msgpack_unpacked result ;
92+ msgpack_object root ;
93+ msgpack_object msg_key ;
94+ msgpack_object msg_val ;
8195
8296 /* Get upstream context and connection */
8397 if (strcmp (host , ctx -> host ) == 0 && port == ctx -> port ) {
8498 u = ctx -> u ;
8599 }
86100 else {
101+ // TODO cache
87102 u = flb_upstream_create (ctx -> u -> base .config ,
88103 host ,
89104 port ,
@@ -117,22 +132,32 @@ static int http_put(struct flb_out_doris *ctx,
117132 flb_http_add_header (c , "format" , 6 , "json" , 4 );
118133 flb_http_add_header (c , "Expect" , 6 , "100-continue" , 12 );
119134 flb_http_add_header (c , "strip_outer_array" , 17 , "true" , 4 );
120- flb_http_add_header (c , "columns" , 7 , ctx -> columns , strlen (ctx -> columns ));
121135 flb_http_add_header (c , "User-Agent" , 10 , "Fluent-Bit" , 10 );
122- if (ctx -> timeout_second > 0 ) {
123- char timeout [256 ];
124- snprintf (timeout , sizeof (timeout ) - 1 , "%d" , ctx -> timeout_second );
125- flb_http_add_header (c , "timeout" , 7 , timeout , strlen (timeout ));
136+
137+ flb_config_map_foreach (head , mv , ctx -> headers ) {
138+ key = mk_list_entry_first (mv -> val .list , struct flb_slist_entry , _head );
139+ val = mk_list_entry_last (mv -> val .list , struct flb_slist_entry , _head );
140+
141+ flb_http_add_header (c ,
142+ key -> str , flb_sds_len (key -> str ),
143+ val -> str , flb_sds_len (val -> str ));
126144 }
127145
128146 /* Basic Auth headers */
129147 flb_http_basic_auth (c , ctx -> user , ctx -> password );
130148
131149 ret = flb_http_do (c , & b_sent );
132150 if (ret == 0 ) {
133- flb_plg_debug (ctx -> ins , "%s:%i, HTTP status=%i\n%s\n" ,
134- host , port ,
135- c -> resp .status , c -> resp .payload );
151+ if (ctx -> log_request ) {
152+ flb_plg_info (ctx -> ins , "%s:%i, HTTP status=%i\n%s\n" ,
153+ host , port ,
154+ c -> resp .status , c -> resp .payload );
155+ } else {
156+ flb_plg_debug (ctx -> ins , "%s:%i, HTTP status=%i\n%s\n" ,
157+ host , port ,
158+ c -> resp .status , c -> resp .payload );
159+ }
160+
136161 if (c -> resp .status == 307 ) { // redict
137162 // example: Location: http://admin:[email protected] :8040/api/d_fb/t_fb/_stream_load? 138163 char * location = strstr (c -> resp .data , "Location:" );
@@ -147,15 +172,53 @@ static int http_put(struct flb_out_doris *ctx,
147172 out_ret = http_put (ctx , redict_host , atoi (redict_port ),
148173 body , body_len , tag , tag_len );
149174 }
150- else if (c -> resp .status == 200 ) {
151- if (c -> resp .payload_size > 0 &&
152- (strstr (c -> resp .payload , "\"Status\": \"Success\"" ) != NULL ||
153- strstr (c -> resp .payload , "\"Status\": \"Publish Timeout\"" ) != NULL )) {
154- // continue
175+ else if (c -> resp .status == 200 && c -> resp .payload_size > 0 ) {
176+ ret = flb_pack_json (c -> resp .payload , c -> resp .payload_size ,
177+ & out_buf , & out_size , & root_type , NULL );
178+
179+ if (ret == -1 ) {
180+ out_ret = FLB_RETRY ;
181+ }
182+
183+ msgpack_unpacked_init (& result );
184+ ret = msgpack_unpack_next (& result , out_buf , out_size , & off );
185+ if (ret != MSGPACK_UNPACK_SUCCESS ) {
186+ out_ret = FLB_RETRY ;
155187 }
156- else {
188+
189+ root = result .data ;
190+ if (root .type != MSGPACK_OBJECT_MAP ) {
157191 out_ret = FLB_RETRY ;
158192 }
193+
194+ for (i = 0 ; i < root .via .map .size ; i ++ ) {
195+ msg_key = root .via .map .ptr [i ].key ;
196+ if (msg_key .type != MSGPACK_OBJECT_STR ) {
197+ out_ret = FLB_RETRY ;
198+ break ;
199+ }
200+
201+ if (msg_key .via .str .size == 6 && strncmp (msg_key .via .str .ptr , "Status" , 6 ) == 0 ) {
202+ msg_val = root .via .map .ptr [i ].val ;
203+ if (msg_val .type != MSGPACK_OBJECT_STR ) {
204+ out_ret = FLB_RETRY ;
205+ break ;
206+ }
207+
208+ if (msg_val .via .str .size == 7 && strncmp (msg_val .via .str .ptr , "Success" , 7 ) == 0 ) {
209+ out_ret = FLB_OK ;
210+ break ;
211+ }
212+
213+ if (msg_val .via .str .size == 15 && strncmp (msg_val .via .str .ptr , "Publish Timeout" , 15 ) == 0 ) {
214+ out_ret = FLB_OK ;
215+ break ;
216+ }
217+
218+ out_ret = FLB_RETRY ;
219+ break ;
220+ }
221+ }
159222 }
160223 else {
161224 out_ret = FLB_RETRY ;
@@ -204,15 +267,19 @@ static int compose_payload(struct flb_out_doris *ctx,
204267 in_size ,
205268 FLB_PACK_JSON_FORMAT_JSON ,
206269 FLB_PACK_JSON_DATE_EPOCH ,
207- ctx -> time_key );
270+ ctx -> date_key );
208271 if (encoded == NULL ) {
209272 flb_plg_error (ctx -> ins , "failed to convert json" );
210273 return FLB_ERROR ;
211274 }
212275 * out_body = (void * )encoded ;
213276 * out_size = flb_sds_len (encoded );
214277
215- flb_plg_debug (ctx -> ins , "http body: %s" , (char * ) * out_body );
278+ if (ctx -> log_request ) {
279+ flb_plg_info (ctx -> ins , "http body: %s" , (char * ) * out_body );
280+ } else {
281+ flb_plg_debug (ctx -> ins , "http body: %s" , (char * ) * out_body );
282+ }
216283
217284 return FLB_OK ;
218285}
@@ -233,13 +300,22 @@ static void cb_doris_flush(struct flb_event_chunk *event_chunk,
233300 & out_body , & out_size );
234301
235302 if (ret != FLB_OK ) {
303+ if (ret == FLB_ERROR ) {
304+ __sync_fetch_and_add (& ctx -> reporter -> failed_rows , event_chunk -> total_events );
305+ }
236306 FLB_OUTPUT_RETURN (ret );
237307 }
238308
239309 ret = http_put (ctx , ctx -> host , ctx -> port , out_body , out_size ,
240310 event_chunk -> tag , flb_sds_len (event_chunk -> tag ));
241311 flb_sds_destroy (out_body );
242312
313+ if (ret == FLB_OK ) {
314+ __sync_fetch_and_add (& ctx -> reporter -> total_bytes , out_size );
315+ __sync_fetch_and_add (& ctx -> reporter -> total_rows , event_chunk -> total_events );
316+ } else if (ret == FLB_ERROR ) {
317+ __sync_fetch_and_add (& ctx -> reporter -> failed_rows , event_chunk -> total_events );
318+ }
243319 FLB_OUTPUT_RETURN (ret );
244320}
245321
@@ -283,17 +359,23 @@ static struct flb_config_map config_map[] = {
283359 0 , FLB_TRUE , offsetof(struct flb_out_doris , time_key ),
284360 "Specify the name of the date field in output"
285361 },
286- // columns
362+ // header
363+ {
364+ FLB_CONFIG_MAP_SLIST_1 , "header" , NULL ,
365+ FLB_CONFIG_MAP_MULT , FLB_TRUE , offsetof(struct flb_out_doris , headers ),
366+ "Add a doris stream load header key/value pair. Multiple headers can be set"
367+ },
368+ // log_request
287369 {
288- FLB_CONFIG_MAP_STR , "columns " , "date,log " ,
289- 0 , FLB_TRUE , offsetof(struct flb_out_doris , columns ),
290- "Set columns "
370+ FLB_CONFIG_MAP_BOOL , "log_request " , "true " ,
371+ 0 , FLB_TRUE , offsetof(struct flb_out_doris , log_request ),
372+ "Specify if the doris stream load request and response should be logged or not "
291373 },
292- // timeout
374+ // log_progress_interval
293375 {
294- FLB_CONFIG_MAP_INT , "timeout_second " , "60 " ,
295- 0 , FLB_TRUE , offsetof(struct flb_out_doris , timeout_second ),
296- "Set timeout in second "
376+ FLB_CONFIG_MAP_INT , "log_progress_interval " , "10 " ,
377+ 0 , FLB_TRUE , offsetof(struct flb_out_doris , log_progress_interval ),
378+ "Specify the interval in seconds to log the progress of the doris stream load "
297379 },
298380
299381 /* EOF */
0 commit comments