Skip to content

Commit 700e9e1

Browse files
committed
in_docker_events: implement config maps
Signed-off-by: Eduardo Silva <[email protected]>
1 parent c99cd63 commit 700e9e1

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

plugins/in_docker_events/docker_events.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ static int in_de_collect(struct flb_input_instance *ins,
110110
flb_pack_time_now(&mp_pck);
111111
msgpack_pack_map(&mp_pck, 1);
112112

113-
msgpack_pack_str(&mp_pck, ctx->key_len);
114-
msgpack_pack_str_body(&mp_pck, ctx->key,
115-
ctx->key_len);
113+
msgpack_pack_str(&mp_pck, flb_sds_len(ctx->key));
114+
msgpack_pack_str_body(&mp_pck, ctx->key, flb_sds_len(ctx->key));
116115
msgpack_pack_str(&mp_pck, str_len);
117116
msgpack_pack_str_body(&mp_pck, ctx->buf, str_len);
118117
flb_input_chunk_append_raw(ins, NULL, 0, mp_sbuf.data,
@@ -198,6 +197,7 @@ static int in_de_init(struct flb_input_instance *ins,
198197
return -1;
199198
}
200199

200+
flb_plg_info(ctx->ins, "listening for events on %s", ctx->unix_path);
201201
return 0;
202202
}
203203

@@ -214,11 +214,41 @@ static int in_de_exit(void *data, struct flb_config *config)
214214
(void) config;
215215
struct flb_in_de_config *ctx = data;
216216

217+
if (!ctx) {
218+
return 0;
219+
}
220+
217221
de_config_destroy(ctx);
218222

219223
return 0;
220224
}
221225

226+
/* Configuration properties map */
227+
static struct flb_config_map config_map[] = {
228+
{
229+
FLB_CONFIG_MAP_STR, "unix_path", DEFAULT_UNIX_SOCKET_PATH,
230+
0, FLB_TRUE, offsetof(struct flb_in_de_config, unix_path),
231+
"Define Docker unix socket path to read events"
232+
},
233+
{
234+
FLB_CONFIG_MAP_SIZE, "buffer_size", "8k",
235+
0, FLB_TRUE, offsetof(struct flb_in_de_config, buf_size),
236+
"Set buffer size to read events"
237+
},
238+
{
239+
FLB_CONFIG_MAP_STR, "parser", NULL,
240+
0, FLB_FALSE, 0,
241+
"Optional parser for records, if not set, records are packages under 'key'"
242+
},
243+
{
244+
FLB_CONFIG_MAP_STR, "key", DEFAULT_FIELD_NAME,
245+
0, FLB_TRUE, offsetof(struct flb_in_de_config, key),
246+
"Set the key name to store unparsed Docker events"
247+
},
248+
/* EOF */
249+
{0}
250+
};
251+
222252
/* Plugin reference */
223253
struct flb_input_plugin in_docker_events_plugin = {
224254
.name = "docker_events",
@@ -228,5 +258,6 @@ struct flb_input_plugin in_docker_events_plugin = {
228258
.cb_collect = in_de_collect,
229259
.cb_flush_buf = NULL,
230260
.cb_exit = in_de_exit,
261+
.config_map = config_map,
231262
.flags = FLB_INPUT_NET
232263
};

plugins/in_docker_events/docker_events.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
struct flb_in_de_config
3434
{
3535
int fd; /* File descriptor */
36-
char *unix_path; /* Unix path for socket */
37-
struct flb_parser *parser;
38-
struct flb_input_instance *ins; /* Input plugin instace */
36+
flb_sds_t unix_path; /* Unix path for socket */
3937
char *buf;
4038
size_t buf_size;
41-
char *key;
42-
int key_len;
39+
flb_sds_t key;
40+
struct flb_parser *parser;
41+
struct flb_input_instance *ins; /* Input plugin instace */
42+
4343
};
4444

4545
#endif

plugins/in_docker_events/docker_events_config.c

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
struct flb_in_de_config *de_config_init(struct flb_input_instance *ins,
3737
struct flb_config *config)
3838
{
39-
const char *p;
39+
int ret;
40+
const char *tmp;
4041
struct flb_in_de_config *ctx;
4142

4243
ctx = flb_calloc(1, sizeof(struct flb_in_de_config));
@@ -46,41 +47,30 @@ struct flb_in_de_config *de_config_init(struct flb_input_instance *ins,
4647
}
4748
ctx->ins = ins;
4849

49-
p = flb_input_get_property("unix_path", ins);
50-
if (p) {
51-
ctx->unix_path = flb_strdup(p);
52-
}
53-
else {
54-
ctx->unix_path = flb_strdup(DEFAULT_UNIX_SOCKET_PATH);
50+
/* Load the config map */
51+
ret = flb_input_config_map_set(ins, (void *) ctx);
52+
if (ret == -1) {
53+
flb_free(ctx);
54+
return NULL;
5555
}
5656

57-
p = flb_input_get_property("buffer_size", ins);
58-
if (!p) {
59-
ctx->buf_size = DEFAULT_BUF_SIZE;
60-
}
61-
else {
62-
ctx->buf_size = flb_utils_size_to_bytes(p);
63-
}
57+
/* Allocate buffer for events */
6458
ctx->buf = flb_malloc(ctx->buf_size);
59+
if (!ctx->buf) {
60+
flb_errno();
61+
flb_free(ctx);
62+
return NULL;
63+
}
6564

66-
p = flb_input_get_property("parser", ins);
67-
if (p) {
68-
ctx->parser = flb_parser_get(p, config);
65+
tmp = flb_input_get_property("parser", ins);
66+
if (tmp) {
67+
ctx->parser = flb_parser_get(tmp, config);
6968
if (ctx->parser == NULL) {
70-
flb_error("[in_docker_events] requested parser '%s' not found", p);
69+
flb_plg_error(ctx->ins, "requested parser '%s' not found", tmp);
7170
return NULL;
7271
}
7372
}
7473

75-
p = flb_input_get_property("key", ins);
76-
if (p) {
77-
ctx->key = flb_strdup(p);
78-
}
79-
else {
80-
ctx->key = flb_strdup(DEFAULT_FIELD_NAME);
81-
}
82-
ctx->key_len = strlen(ctx->key);
83-
8474
return ctx;
8575
}
8676

@@ -93,15 +83,9 @@ struct flb_in_de_config *de_config_init(struct flb_input_instance *ins,
9383
*/
9484
int de_config_destroy(struct flb_in_de_config *ctx)
9585
{
96-
if (ctx->unix_path) {
97-
flb_free(ctx->unix_path);
98-
}
9986
if (ctx->buf) {
10087
flb_free(ctx->buf);
10188
}
102-
if (ctx->key) {
103-
flb_free(ctx->key);
104-
}
10589

10690
flb_free(ctx);
10791
return 0;

0 commit comments

Comments
 (0)