/src/fluent-bit/plugins/out_stdout/stdout.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2022 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 | | |
28 | | #include <ctraces/ctraces.h> |
29 | | #include <ctraces/ctr_decode_msgpack.h> |
30 | | |
31 | | #include <msgpack.h> |
32 | | #include "stdout.h" |
33 | | |
34 | | |
35 | | static int cb_stdout_init(struct flb_output_instance *ins, |
36 | | struct flb_config *config, void *data) |
37 | 1.68k | { |
38 | 1.68k | int ret; |
39 | 1.68k | const char *tmp; |
40 | 1.68k | struct flb_stdout *ctx = NULL; |
41 | 1.68k | (void) ins; |
42 | 1.68k | (void) config; |
43 | 1.68k | (void) data; |
44 | | |
45 | 1.68k | ctx = flb_calloc(1, sizeof(struct flb_stdout)); |
46 | 1.68k | if (!ctx) { |
47 | 0 | flb_errno(); |
48 | 0 | return -1; |
49 | 0 | } |
50 | 1.68k | ctx->ins = ins; |
51 | | |
52 | 1.68k | ret = flb_output_config_map_set(ins, (void *) ctx); |
53 | 1.68k | if (ret == -1) { |
54 | 0 | flb_free(ctx); |
55 | 0 | return -1; |
56 | 0 | } |
57 | | |
58 | 1.68k | ctx->out_format = FLB_PACK_JSON_FORMAT_NONE; |
59 | 1.68k | tmp = flb_output_get_property("format", ins); |
60 | 1.68k | if (tmp) { |
61 | 0 | ret = flb_pack_to_json_format_type(tmp); |
62 | 0 | if (ret == -1) { |
63 | 0 | flb_plg_error(ctx->ins, "unrecognized 'format' option. " |
64 | 0 | "Using 'msgpack'"); |
65 | 0 | } |
66 | 0 | else { |
67 | 0 | ctx->out_format = ret; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | /* Date key */ |
72 | 1.68k | ctx->date_key = ctx->json_date_key; |
73 | 1.68k | tmp = flb_output_get_property("json_date_key", ins); |
74 | 1.68k | if (tmp) { |
75 | | /* Just check if we have to disable it */ |
76 | 0 | if (flb_utils_bool(tmp) == FLB_FALSE) { |
77 | 0 | ctx->date_key = NULL; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | /* Date format for JSON output */ |
82 | 1.68k | ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE; |
83 | 1.68k | tmp = flb_output_get_property("json_date_format", ins); |
84 | 1.68k | if (tmp) { |
85 | 0 | ret = flb_pack_to_json_date_type(tmp); |
86 | 0 | if (ret == -1) { |
87 | 0 | flb_plg_error(ctx->ins, "invalid json_date_format '%s'. " |
88 | 0 | "Using 'double' type", tmp); |
89 | 0 | } |
90 | 0 | else { |
91 | 0 | ctx->json_date_format = ret; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | /* Export context */ |
96 | 1.68k | flb_output_set_context(ins, ctx); |
97 | | |
98 | 1.68k | return 0; |
99 | 1.68k | } |
100 | | |
101 | | #ifdef FLB_HAVE_METRICS |
102 | | static void print_metrics_text(struct flb_output_instance *ins, |
103 | | const void *data, size_t bytes) |
104 | 0 | { |
105 | 0 | int ret; |
106 | 0 | size_t off = 0; |
107 | 0 | cfl_sds_t text; |
108 | 0 | struct cmt *cmt = NULL; |
109 | | |
110 | | /* get cmetrics context */ |
111 | 0 | ret = cmt_decode_msgpack_create(&cmt, (char *) data, bytes, &off); |
112 | 0 | if (ret != 0) { |
113 | 0 | flb_plg_error(ins, "could not process metrics payload"); |
114 | 0 | return; |
115 | 0 | } |
116 | | |
117 | | /* convert to text representation */ |
118 | 0 | text = cmt_encode_text_create(cmt); |
119 | | |
120 | | /* destroy cmt context */ |
121 | 0 | cmt_destroy(cmt); |
122 | |
|
123 | 0 | printf("%s", text); |
124 | 0 | fflush(stdout); |
125 | |
|
126 | 0 | cmt_encode_text_destroy(text); |
127 | 0 | } |
128 | | #endif |
129 | | |
130 | | static void print_traces_text(struct flb_output_instance *ins, |
131 | | const void *data, size_t bytes) |
132 | 0 | { |
133 | 0 | int ret; |
134 | 0 | size_t off = 0; |
135 | 0 | cfl_sds_t text; |
136 | 0 | struct ctrace *ctr = NULL; |
137 | | |
138 | | /* get cmetrics context */ |
139 | 0 | ret = ctr_decode_msgpack_create(&ctr, (char *) data, bytes, &off); |
140 | 0 | if (ret != 0) { |
141 | 0 | flb_plg_error(ins, "could not process traces payload (ret=%i)", ret); |
142 | 0 | return; |
143 | 0 | } |
144 | | |
145 | | /* convert to text representation */ |
146 | 0 | text = ctr_encode_text_create(ctr); |
147 | | |
148 | | /* destroy cmt context */ |
149 | 0 | ctr_destroy(ctr); |
150 | |
|
151 | 0 | printf("%s", text); |
152 | 0 | fflush(stdout); |
153 | |
|
154 | 0 | ctr_encode_text_destroy(text); |
155 | 0 | } |
156 | | |
157 | | static void cb_stdout_flush(struct flb_event_chunk *event_chunk, |
158 | | struct flb_output_flush *out_flush, |
159 | | struct flb_input_instance *i_ins, |
160 | | void *out_context, |
161 | | struct flb_config *config) |
162 | 1.28k | { |
163 | 1.28k | msgpack_unpacked result; |
164 | 1.28k | size_t off = 0, cnt = 0; |
165 | 1.28k | struct flb_stdout *ctx = out_context; |
166 | 1.28k | flb_sds_t json; |
167 | 1.28k | char *buf = NULL; |
168 | 1.28k | (void) config; |
169 | 1.28k | struct flb_time tmp; |
170 | 1.28k | msgpack_object *p; |
171 | | |
172 | 1.28k | #ifdef FLB_HAVE_METRICS |
173 | | /* Check if the event type is metrics, handle the payload differently */ |
174 | 1.28k | if (event_chunk->type == FLB_EVENT_TYPE_METRICS) { |
175 | 0 | print_metrics_text(ctx->ins, (char *) |
176 | 0 | event_chunk->data, |
177 | 0 | event_chunk->size); |
178 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
179 | 0 | } |
180 | 1.28k | #endif |
181 | | |
182 | 1.28k | if (event_chunk->type == FLB_EVENT_TYPE_TRACES) { |
183 | 0 | print_traces_text(ctx->ins, (char *) |
184 | 0 | event_chunk->data, |
185 | 0 | event_chunk->size); |
186 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
187 | 0 | } |
188 | | |
189 | | /* Assuming data is a log entry...*/ |
190 | 1.28k | if (ctx->out_format != FLB_PACK_JSON_FORMAT_NONE) { |
191 | 0 | json = flb_pack_msgpack_to_json_format(event_chunk->data, |
192 | 0 | event_chunk->size, |
193 | 0 | ctx->out_format, |
194 | 0 | ctx->json_date_format, |
195 | 0 | ctx->date_key); |
196 | 0 | write(STDOUT_FILENO, json, flb_sds_len(json)); |
197 | 0 | flb_sds_destroy(json); |
198 | | |
199 | | /* |
200 | | * If we are 'not' in json_lines mode, we need to add an extra |
201 | | * breakline. |
202 | | */ |
203 | 0 | if (ctx->out_format != FLB_PACK_JSON_FORMAT_LINES) { |
204 | 0 | printf("\n"); |
205 | 0 | } |
206 | 0 | fflush(stdout); |
207 | 0 | } |
208 | 1.28k | else { |
209 | 1.28k | msgpack_unpacked_init(&result); |
210 | 467k | while (msgpack_unpack_next(&result, |
211 | 467k | event_chunk->data, |
212 | 467k | event_chunk->size, &off) == MSGPACK_UNPACK_SUCCESS) { |
213 | 466k | if (flb_time_pop_from_msgpack(&tmp, &result, &p) != -1 ) { |
214 | 406 | printf("[%zd] %s: [", cnt++, event_chunk->tag); |
215 | 406 | printf("%"PRIu32".%09lu, ", (uint32_t)tmp.tm.tv_sec, tmp.tm.tv_nsec); |
216 | 406 | msgpack_object_print(stdout, *p); |
217 | 406 | printf("]\n"); |
218 | 406 | } |
219 | 466k | } |
220 | 1.28k | msgpack_unpacked_destroy(&result); |
221 | 1.28k | flb_free(buf); |
222 | 1.28k | } |
223 | 1.28k | fflush(stdout); |
224 | | |
225 | 1.28k | FLB_OUTPUT_RETURN(FLB_OK); |
226 | 1.28k | } |
227 | | |
228 | | static int cb_stdout_exit(void *data, struct flb_config *config) |
229 | 1.68k | { |
230 | 1.68k | struct flb_stdout *ctx = data; |
231 | | |
232 | 1.68k | if (!ctx) { |
233 | 0 | return 0; |
234 | 0 | } |
235 | | |
236 | 1.68k | flb_free(ctx); |
237 | 1.68k | return 0; |
238 | 1.68k | } |
239 | | |
240 | | /* Configuration properties map */ |
241 | | static struct flb_config_map config_map[] = { |
242 | | { |
243 | | FLB_CONFIG_MAP_STR, "format", NULL, |
244 | | 0, FLB_FALSE, 0, |
245 | | "Specifies the data format to be printed. Supported formats are msgpack json, json_lines and json_stream." |
246 | | }, |
247 | | { |
248 | | FLB_CONFIG_MAP_STR, "json_date_format", NULL, |
249 | | 0, FLB_FALSE, 0, |
250 | | FBL_PACK_JSON_DATE_FORMAT_DESCRIPTION |
251 | | }, |
252 | | { |
253 | | FLB_CONFIG_MAP_STR, "json_date_key", "date", |
254 | | 0, FLB_TRUE, offsetof(struct flb_stdout, json_date_key), |
255 | | "Specifies the name of the date field in output." |
256 | | }, |
257 | | |
258 | | /* EOF */ |
259 | | {0} |
260 | | }; |
261 | | |
262 | | /* Plugin registration */ |
263 | | struct flb_output_plugin out_stdout_plugin = { |
264 | | .name = "stdout", |
265 | | .description = "Prints events to STDOUT", |
266 | | .cb_init = cb_stdout_init, |
267 | | .cb_flush = cb_stdout_flush, |
268 | | .cb_exit = cb_stdout_exit, |
269 | | .flags = 0, |
270 | | .workers = 1, |
271 | | .event_type = FLB_OUTPUT_LOGS | FLB_OUTPUT_METRICS | FLB_OUTPUT_TRACES, |
272 | | .config_map = config_map |
273 | | }; |