/src/fluent-bit/plugins/out_stdout/stdout.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2026 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_output_plugin.h> |
21 | | #include <fluent-bit/flb_utils.h> |
22 | | #include <fluent-bit/flb_slist.h> |
23 | | #include <fluent-bit/flb_time.h> |
24 | | #include <fluent-bit/flb_pack.h> |
25 | | #include <fluent-bit/flb_config_map.h> |
26 | | #include <fluent-bit/flb_metrics.h> |
27 | | #include <fluent-bit/flb_log_event_decoder.h> |
28 | | |
29 | | #include <ctraces/ctraces.h> |
30 | | #include <ctraces/ctr_decode_msgpack.h> |
31 | | |
32 | | #include <cprofiles/cprofiles.h> |
33 | | #include <cprofiles/cprof_encode_text.h> |
34 | | #include <cprofiles/cprof_decode_msgpack.h> |
35 | | |
36 | | #include <msgpack.h> |
37 | | #include "stdout.h" |
38 | | |
39 | | |
40 | | static int cb_stdout_init(struct flb_output_instance *ins, |
41 | | struct flb_config *config, void *data) |
42 | 97 | { |
43 | 97 | int ret; |
44 | 97 | const char *tmp; |
45 | 97 | struct flb_stdout *ctx = NULL; |
46 | 97 | (void) ins; |
47 | 97 | (void) config; |
48 | 97 | (void) data; |
49 | | |
50 | 97 | ctx = flb_calloc(1, sizeof(struct flb_stdout)); |
51 | 97 | if (!ctx) { |
52 | 0 | flb_errno(); |
53 | 0 | return -1; |
54 | 0 | } |
55 | 97 | ctx->ins = ins; |
56 | | |
57 | 97 | ret = flb_output_config_map_set(ins, (void *) ctx); |
58 | 97 | if (ret == -1) { |
59 | 0 | flb_free(ctx); |
60 | 0 | return -1; |
61 | 0 | } |
62 | | |
63 | 97 | ctx->out_format = FLB_PACK_JSON_FORMAT_NONE; |
64 | 97 | tmp = flb_output_get_property("format", ins); |
65 | 97 | if (tmp) { |
66 | 0 | ret = flb_pack_to_json_format_type(tmp); |
67 | 0 | if (ret == -1) { |
68 | 0 | flb_plg_error(ctx->ins, "unrecognized 'format' option. " |
69 | 0 | "Using 'msgpack'"); |
70 | 0 | } |
71 | 0 | else { |
72 | 0 | ctx->out_format = ret; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | /* Date key */ |
77 | 97 | ctx->date_key = ctx->json_date_key; |
78 | 97 | tmp = flb_output_get_property("json_date_key", ins); |
79 | 97 | if (tmp) { |
80 | | /* Just check if we have to disable it */ |
81 | 0 | if (flb_utils_bool(tmp) == FLB_FALSE) { |
82 | 0 | ctx->date_key = NULL; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | /* Date format for JSON output */ |
87 | 97 | ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE; |
88 | 97 | tmp = flb_output_get_property("json_date_format", ins); |
89 | 97 | if (tmp) { |
90 | 0 | ret = flb_pack_to_json_date_type(tmp); |
91 | 0 | if (ret == -1) { |
92 | 0 | flb_plg_error(ctx->ins, "invalid json_date_format '%s'. " |
93 | 0 | "Using 'double' type", tmp); |
94 | 0 | } |
95 | 0 | else { |
96 | 0 | ctx->json_date_format = ret; |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | /* Export context */ |
101 | 97 | flb_output_set_context(ins, ctx); |
102 | | |
103 | 97 | return 0; |
104 | 97 | } |
105 | | |
106 | | #ifdef FLB_HAVE_METRICS |
107 | | static void print_metrics_text(struct flb_output_instance *ins, |
108 | | const void *data, size_t bytes) |
109 | 0 | { |
110 | 0 | int ret; |
111 | 0 | size_t off = 0; |
112 | 0 | cfl_sds_t text; |
113 | 0 | struct cmt *cmt = NULL; |
114 | 0 | int ok = CMT_DECODE_MSGPACK_SUCCESS; |
115 | | |
116 | | /* get cmetrics context */ |
117 | 0 | while((ret = cmt_decode_msgpack_create(&cmt, |
118 | 0 | (char *) data, |
119 | 0 | bytes, &off)) == ok) { |
120 | 0 | if (ret != 0) { |
121 | 0 | flb_plg_error(ins, "could not process metrics payload"); |
122 | 0 | return; |
123 | 0 | } |
124 | | |
125 | | /* convert to text representation */ |
126 | 0 | text = cmt_encode_text_create(cmt); |
127 | | |
128 | | /* destroy cmt context */ |
129 | 0 | cmt_destroy(cmt); |
130 | |
|
131 | 0 | printf("%s", text); |
132 | 0 | fflush(stdout); |
133 | |
|
134 | 0 | cmt_encode_text_destroy(text); |
135 | 0 | } |
136 | | |
137 | 0 | if (ret != ok) { |
138 | 0 | flb_plg_debug(ins, "cmt decode msgpack returned : %d", ret); |
139 | 0 | } |
140 | 0 | } |
141 | | #endif |
142 | | |
143 | | static void print_traces_text(struct flb_output_instance *ins, |
144 | | const void *data, size_t bytes) |
145 | 0 | { |
146 | 0 | int ret; |
147 | 0 | size_t off = 0; |
148 | 0 | cfl_sds_t text; |
149 | 0 | struct ctrace *ctr = NULL; |
150 | 0 | int ok = CTR_DECODE_MSGPACK_SUCCESS; |
151 | | |
152 | | /* Decode each ctrace context */ |
153 | 0 | while ((ret = ctr_decode_msgpack_create(&ctr, |
154 | 0 | (char *) data, |
155 | 0 | bytes, &off)) == ok) { |
156 | | /* convert to text representation */ |
157 | 0 | text = ctr_encode_text_create(ctr); |
158 | | |
159 | | /* destroy ctr context */ |
160 | 0 | ctr_destroy(ctr); |
161 | |
|
162 | 0 | printf("%s", text); |
163 | 0 | fflush(stdout); |
164 | |
|
165 | 0 | ctr_encode_text_destroy(text); |
166 | 0 | } |
167 | 0 | if (ret != ok) { |
168 | 0 | flb_plg_debug(ins, "ctr decode msgpack returned : %d", ret); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | static void print_profiles_text(struct flb_output_instance *ins, |
173 | | const void *data, size_t bytes) |
174 | 0 | { |
175 | 0 | int ret; |
176 | 0 | size_t off; |
177 | 0 | cfl_sds_t text; |
178 | 0 | struct cprof *profiles_context; |
179 | |
|
180 | 0 | profiles_context = NULL; |
181 | 0 | off = 0; |
182 | | |
183 | | /* Decode each profiles context */ |
184 | 0 | while ((ret = cprof_decode_msgpack_create(&profiles_context, |
185 | 0 | (unsigned char *) data, |
186 | 0 | bytes, &off)) == |
187 | 0 | CPROF_DECODE_MSGPACK_SUCCESS) { |
188 | | /* convert to text representation */ |
189 | 0 | ret = cprof_encode_text_create(&text, |
190 | 0 | profiles_context, |
191 | 0 | CPROF_ENCODE_TEXT_RENDER_RESOLVED); |
192 | |
|
193 | 0 | if (ret != CPROF_ENCODE_TEXT_SUCCESS) { |
194 | 0 | flb_plg_debug(ins, "cprofiles text encoder returned : %d", ret); |
195 | |
|
196 | 0 | continue; |
197 | 0 | } |
198 | | |
199 | | /* destroy ctr context */ |
200 | 0 | cprof_decode_msgpack_destroy(profiles_context); |
201 | |
|
202 | 0 | printf("%s", text); |
203 | 0 | fflush(stdout); |
204 | |
|
205 | 0 | cprof_encode_text_destroy(text); |
206 | 0 | } |
207 | |
|
208 | 0 | if (ret != CPROF_DECODE_MSGPACK_SUCCESS) { |
209 | 0 | flb_plg_debug(ins, "cprofiles msgpack decoder returned : %d", ret); |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | static void cb_stdout_flush(struct flb_event_chunk *event_chunk, |
214 | | struct flb_output_flush *out_flush, |
215 | | struct flb_input_instance *i_ins, |
216 | | void *out_context, |
217 | | struct flb_config *config) |
218 | 1 | { |
219 | 1 | struct flb_log_event_decoder log_decoder; |
220 | 1 | struct flb_log_event log_event; |
221 | 1 | int result; |
222 | 1 | flb_sds_t json; |
223 | 1 | struct flb_stdout *ctx; |
224 | 1 | size_t cnt; |
225 | | |
226 | 1 | (void) config; |
227 | | |
228 | 1 | result = FLB_EVENT_DECODER_SUCCESS; |
229 | 1 | ctx = (struct flb_stdout *) out_context; |
230 | 1 | cnt = 0; |
231 | | |
232 | 1 | #ifdef FLB_HAVE_METRICS |
233 | | /* Check if the event type is metrics, handle the payload differently */ |
234 | 1 | if (event_chunk->type == FLB_EVENT_TYPE_METRICS) { |
235 | 0 | print_metrics_text(ctx->ins, (char *) |
236 | 0 | event_chunk->data, |
237 | 0 | event_chunk->size); |
238 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
239 | 0 | } |
240 | 1 | #endif |
241 | | |
242 | 1 | if (event_chunk->type == FLB_EVENT_TYPE_TRACES) { |
243 | 0 | print_traces_text(ctx->ins, (char *) |
244 | 0 | event_chunk->data, |
245 | 0 | event_chunk->size); |
246 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
247 | 0 | } |
248 | | |
249 | 1 | if (event_chunk->type == FLB_EVENT_TYPE_PROFILES) { |
250 | 0 | print_profiles_text(ctx->ins, (char *) |
251 | 0 | event_chunk->data, |
252 | 0 | event_chunk->size); |
253 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
254 | 0 | } |
255 | | |
256 | | /* Assuming data is a log entry...*/ |
257 | 1 | if (ctx->out_format != FLB_PACK_JSON_FORMAT_NONE) { |
258 | 0 | json = flb_pack_msgpack_to_json_format(event_chunk->data, |
259 | 0 | event_chunk->size, |
260 | 0 | ctx->out_format, |
261 | 0 | ctx->json_date_format, |
262 | 0 | ctx->date_key, |
263 | 0 | config->json_escape_unicode); |
264 | 0 | write(STDOUT_FILENO, json, flb_sds_len(json)); |
265 | 0 | flb_sds_destroy(json); |
266 | | |
267 | | /* |
268 | | * If we are 'not' in json_lines mode, we need to add an extra |
269 | | * breakline. |
270 | | */ |
271 | 0 | if (ctx->out_format != FLB_PACK_JSON_FORMAT_LINES) { |
272 | 0 | printf("\n"); |
273 | 0 | } |
274 | 0 | fflush(stdout); |
275 | 0 | } |
276 | 1 | else { |
277 | 1 | result = flb_log_event_decoder_init(&log_decoder, |
278 | 1 | (char *) event_chunk->data, |
279 | 1 | event_chunk->size); |
280 | | |
281 | 1 | if (result != FLB_EVENT_DECODER_SUCCESS) { |
282 | 0 | flb_plg_error(ctx->ins, |
283 | 0 | "Log event decoder initialization error : %d", result); |
284 | |
|
285 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
286 | 0 | } |
287 | | |
288 | 3 | while (flb_log_event_decoder_next(&log_decoder, |
289 | 3 | &log_event) == FLB_EVENT_DECODER_SUCCESS) { |
290 | | |
291 | 2 | if (log_event.group_attributes != NULL) { |
292 | 0 | printf("GROUP METADATA : \n\n"); |
293 | 0 | msgpack_object_print(stdout, *log_event.group_metadata); |
294 | 0 | printf("\n\n"); |
295 | |
|
296 | 0 | printf("GROUP ATTRIBUTES : \n\n"); |
297 | 0 | msgpack_object_print(stdout, *log_event.group_attributes); |
298 | 0 | printf("\n\n"); |
299 | 0 | } |
300 | | |
301 | 2 | printf("[%zd] %s: [[", cnt++, event_chunk->tag); |
302 | | |
303 | 2 | printf("%"PRId32".%09lu, ", (int32_t) log_event.timestamp.tm.tv_sec, |
304 | 2 | log_event.timestamp.tm.tv_nsec); |
305 | | |
306 | 2 | msgpack_object_print(stdout, *log_event.metadata); |
307 | | |
308 | 2 | printf("], "); |
309 | | |
310 | 2 | msgpack_object_print(stdout, *log_event.body); |
311 | | |
312 | 2 | printf("]\n"); |
313 | 2 | } |
314 | 1 | result = flb_log_event_decoder_get_last_result(&log_decoder); |
315 | | |
316 | 1 | flb_log_event_decoder_destroy(&log_decoder); |
317 | 1 | } |
318 | | |
319 | 1 | fflush(stdout); |
320 | | |
321 | 1 | if (result != FLB_EVENT_DECODER_SUCCESS) { |
322 | 0 | flb_plg_error(ctx->ins, "Log event decoder error : %d", result); |
323 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
324 | 0 | } |
325 | | |
326 | 1 | FLB_OUTPUT_RETURN(FLB_OK); |
327 | 1 | } |
328 | | |
329 | | static int cb_stdout_exit(void *data, struct flb_config *config) |
330 | 97 | { |
331 | 97 | struct flb_stdout *ctx = data; |
332 | | |
333 | 97 | if (!ctx) { |
334 | 0 | return 0; |
335 | 0 | } |
336 | | |
337 | 97 | flb_free(ctx); |
338 | 97 | return 0; |
339 | 97 | } |
340 | | |
341 | | /* Configuration properties map */ |
342 | | static struct flb_config_map config_map[] = { |
343 | | { |
344 | | FLB_CONFIG_MAP_STR, "format", NULL, |
345 | | 0, FLB_FALSE, 0, |
346 | | "Specifies the data format to be printed. Supported formats are msgpack json, json_lines and json_stream." |
347 | | }, |
348 | | { |
349 | | FLB_CONFIG_MAP_STR, "json_date_format", NULL, |
350 | | 0, FLB_FALSE, 0, |
351 | | FBL_PACK_JSON_DATE_FORMAT_DESCRIPTION |
352 | | }, |
353 | | { |
354 | | FLB_CONFIG_MAP_STR, "json_date_key", "date", |
355 | | 0, FLB_TRUE, offsetof(struct flb_stdout, json_date_key), |
356 | | "Specifies the name of the date field in output." |
357 | | }, |
358 | | |
359 | | /* EOF */ |
360 | | {0} |
361 | | }; |
362 | | |
363 | | /* Plugin registration */ |
364 | | struct flb_output_plugin out_stdout_plugin = { |
365 | | .name = "stdout", |
366 | | .description = "Prints events to STDOUT", |
367 | | .cb_init = cb_stdout_init, |
368 | | .cb_flush = cb_stdout_flush, |
369 | | .cb_exit = cb_stdout_exit, |
370 | | .flags = 0, |
371 | | .workers = 1, |
372 | | .event_type = FLB_OUTPUT_LOGS | FLB_OUTPUT_METRICS | FLB_OUTPUT_TRACES | |
373 | | FLB_OUTPUT_PROFILES | FLB_OUTPUT_BLOBS, |
374 | | .config_map = config_map |
375 | | }; |