Skip to content

Commit 19b849f

Browse files
committed
output splunk plugin: add possibility to set source with record key similarly to sourcetype
1 parent 09d9e8a commit 19b849f

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

plugins/out_splunk/splunk.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static int pack_map_meta(struct flb_splunk *ctx,
269269
char *tag, int tag_len)
270270
{
271271
int index_key_set = FLB_FALSE;
272+
int source_key_set = FLB_FALSE;
272273
int sourcetype_key_set = FLB_FALSE;
273274
flb_sds_t str;
274275
struct mk_list *head;
@@ -294,11 +295,13 @@ static int pack_map_meta(struct flb_splunk *ctx,
294295
}
295296
}
296297

297-
/* event source */
298-
if (ctx->event_source) {
299-
str = flb_ra_translate(ctx->ra_event_source, tag, tag_len,
298+
299+
/* event source (key lookup) */
300+
if (ctx->event_source_key) {
301+
str = flb_ra_translate(ctx->ra_event_source_key, tag, tag_len,
300302
map, NULL);
301303
if (str) {
304+
/* source_key was found */
302305
if (flb_sds_len(str) > 0) {
303306
flb_mp_map_header_append(mh);
304307
msgpack_pack_str(mp_pck, sizeof(FLB_SPLUNK_DEFAULT_EVENT_SOURCE) -1);
@@ -307,11 +310,25 @@ static int pack_map_meta(struct flb_splunk *ctx,
307310
sizeof(FLB_SPLUNK_DEFAULT_EVENT_SOURCE) - 1);
308311
msgpack_pack_str(mp_pck, flb_sds_len(str));
309312
msgpack_pack_str_body(mp_pck, str, flb_sds_len(str));
313+
source_key_set = FLB_TRUE;
310314
}
311315
flb_sds_destroy(str);
312316
}
317+
/* If not found, it will fallback to the value set in event_source */
318+
}
319+
320+
if (source_key_set == FLB_FALSE && ctx->event_source) {
321+
flb_mp_map_header_append(mh);
322+
msgpack_pack_str(mp_pck, sizeof(FLB_SPLUNK_DEFAULT_EVENT_SOURCE) -1);
323+
msgpack_pack_str_body(mp_pck,
324+
FLB_SPLUNK_DEFAULT_EVENT_SOURCE,
325+
sizeof(FLB_SPLUNK_DEFAULT_EVENT_SOURCE) - 1);
326+
msgpack_pack_str(mp_pck, flb_sds_len(ctx->event_source));
327+
msgpack_pack_str_body(mp_pck,
328+
ctx->event_source, flb_sds_len(ctx->event_source));
313329
}
314330

331+
315332
/* event sourcetype (key lookup) */
316333
if (ctx->event_sourcetype_key) {
317334
str = flb_ra_translate(ctx->ra_event_sourcetype_key, tag, tag_len,
@@ -1117,6 +1134,13 @@ static struct flb_config_map config_map[] = {
11171134
"Set the source value to assign to the event data."
11181135
},
11191136

1137+
{
1138+
FLB_CONFIG_MAP_STR, "event_source_key", NULL,
1139+
0, FLB_TRUE, offsetof(struct flb_splunk, event_source_key),
1140+
"Set a record key that will populate 'source'. If the key is found, it will "
1141+
"have precedence over the value set in 'event_source'."
1142+
},
1143+
11201144
{
11211145
FLB_CONFIG_MAP_STR, "event_sourcetype", NULL,
11221146
0, FLB_TRUE, offsetof(struct flb_splunk, event_sourcetype),

plugins/out_splunk/splunk.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ struct flb_splunk {
6262

6363
/* Event source */
6464
flb_sds_t event_source;
65-
struct flb_record_accessor *ra_event_source;
65+
66+
/* Event source record key */
67+
flb_sds_t event_source_key;
68+
struct flb_record_accessor *ra_event_source_key;
69+
6670

6771
/*
6872
* NOTE: EVENT SOURCE

plugins/out_splunk/splunk_conf.c

+13-12
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,27 @@ struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
195195
}
196196
}
197197

198-
/* Event source */
199-
if (ctx->event_source) {
200-
ctx->ra_event_source = flb_ra_create(ctx->event_source, FLB_TRUE);
201-
if (!ctx->ra_event_source) {
198+
/* Event source (key lookup) */
199+
if (ctx->event_source_key) {
200+
ctx->ra_event_source_key = flb_ra_create(ctx->event_source_key, FLB_TRUE);
201+
if (!ctx->ra_event_source_key) {
202202
flb_plg_error(ctx->ins,
203-
"cannot create record accessor for event_source pattern: '%s'",
204-
ctx->event_host);
203+
"cannot create record accessor for "
204+
"event_source_key pattern: '%s'",
205+
ctx->event_source_key);
205206
flb_splunk_conf_destroy(ctx);
206207
return NULL;
207208
}
208209
}
209210

210-
/* Event source (key lookup) */
211+
/* Event sourcetype (key lookup) */
211212
if (ctx->event_sourcetype_key) {
212213
ctx->ra_event_sourcetype_key = flb_ra_create(ctx->event_sourcetype_key, FLB_TRUE);
213214
if (!ctx->ra_event_sourcetype_key) {
214215
flb_plg_error(ctx->ins,
215216
"cannot create record accessor for "
216217
"event_sourcetype_key pattern: '%s'",
217-
ctx->event_host);
218+
ctx->event_sourcetype_key);
218219
flb_splunk_conf_destroy(ctx);
219220
return NULL;
220221
}
@@ -227,7 +228,7 @@ struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
227228
flb_plg_error(ctx->ins,
228229
"cannot create record accessor for "
229230
"event_index_key pattern: '%s'",
230-
ctx->event_host);
231+
ctx->event_index_key);
231232
flb_splunk_conf_destroy(ctx);
232233
return NULL;
233234
}
@@ -273,7 +274,7 @@ struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
273274
flb_plg_error(ctx->ins,
274275
"cannot create record accessor for "
275276
"metadata_auth_key pattern: '%s'",
276-
ctx->event_host);
277+
ctx->metadata_auth_key);
277278
flb_splunk_conf_destroy(ctx);
278279
return NULL;
279280
}
@@ -312,8 +313,8 @@ int flb_splunk_conf_destroy(struct flb_splunk *ctx)
312313
flb_ra_destroy(ctx->ra_event_host);
313314
}
314315

315-
if (ctx->ra_event_source) {
316-
flb_ra_destroy(ctx->ra_event_source);
316+
if (ctx->ra_event_source_key) {
317+
flb_ra_destroy(ctx->ra_event_source_key);
317318
}
318319

319320
if (ctx->ra_event_sourcetype_key) {

0 commit comments

Comments
 (0)