Skip to content

Commit ffd27bc

Browse files
committed
in_splunk: use record accessor for tag key extraction and add listening info
Replace manual key lookup with record accessor pattern for better performance and support for nested/complex key patterns. Add log messages showing listening interface and port for both HTTP versions. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 5d99d5a commit ffd27bc

File tree

4 files changed

+45
-55
lines changed

4 files changed

+45
-55
lines changed

plugins/in_splunk/splunk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ static int in_splunk_init(struct flb_input_instance *ins,
135135
ctx->http_server.request_callback = splunk_prot_handle_ng;
136136

137137
flb_input_downstream_set(ctx->http_server.downstream, ctx->ins);
138+
139+
flb_plg_info(ctx->ins, "listening on %s:%u",
140+
ins->host.listen, ins->host.port);
138141
}
139142
else {
140143
ctx->downstream = flb_downstream_create(FLB_TRANSPORT_TCP,
@@ -157,6 +160,8 @@ static int in_splunk_init(struct flb_input_instance *ins,
157160

158161
flb_input_downstream_set(ctx->downstream, ctx->ins);
159162

163+
flb_plg_info(ctx->ins, "listening on %s:%s", ctx->listen, ctx->tcp_port);
164+
160165
/* Collect upon data available on the standard input */
161166
ret = flb_input_set_collector_socket(ins,
162167
in_splunk_collect,
@@ -172,6 +177,7 @@ static int in_splunk_init(struct flb_input_instance *ins,
172177
ctx->collector_id = ret;
173178
}
174179

180+
175181
return 0;
176182
}
177183

plugins/in_splunk/splunk.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <fluent-bit/flb_input.h>
2626
#include <fluent-bit/flb_utils.h>
2727
#include <fluent-bit/flb_log_event_encoder.h>
28+
#include <fluent-bit/flb_record_accessor.h>
2829

2930
#include <monkey/monkey.h>
3031
#include <fluent-bit/http_server/flb_http_server.h>
@@ -41,7 +42,8 @@ struct flb_splunk_tokens {
4142
struct flb_splunk {
4243
flb_sds_t listen;
4344
flb_sds_t tcp_port;
44-
const char *tag_key;
45+
flb_sds_t tag_key;
46+
struct flb_record_accessor *ra_tag_key;
4547

4648
/* Success HTTP headers */
4749
struct mk_list *success_headers;

plugins/in_splunk/splunk_config.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,25 @@ struct flb_splunk *splunk_config_create(struct flb_input_instance *ins)
229229
}
230230
}
231231

232+
/* Create record accessor for tag_key if specified */
233+
if (ctx->tag_key) {
234+
ctx->ra_tag_key = flb_ra_create(ctx->tag_key, FLB_TRUE);
235+
if (!ctx->ra_tag_key) {
236+
flb_plg_error(ctx->ins, "invalid record accessor pattern for tag_key: %s", ctx->tag_key);
237+
splunk_config_destroy(ctx);
238+
return NULL;
239+
}
240+
}
241+
232242
return ctx;
233243
}
234244

235245
int splunk_config_destroy(struct flb_splunk *ctx)
236246
{
247+
if (ctx->ra_tag_key) {
248+
flb_ra_destroy(ctx->ra_tag_key);
249+
}
250+
237251
/* release all connections */
238252
splunk_conn_release_all(ctx);
239253

plugins/in_splunk/splunk_prot.c

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <fluent-bit/flb_error.h>
2323
#include <fluent-bit/flb_pack.h>
2424
#include <fluent-bit/flb_gzip.h>
25+
#include <fluent-bit/flb_record_accessor.h>
26+
#include <fluent-bit/flb_ra_key.h>
2527

2628
#include <monkey/monkey.h>
2729
#include <monkey/mk_core.h>
@@ -156,67 +158,33 @@ static int send_json_message_response(struct splunk_conn *conn, int http_status,
156158
/* implements functionality to get tag from key in record */
157159
static flb_sds_t tag_key(struct flb_splunk *ctx, msgpack_object *map)
158160
{
159-
size_t map_size = map->via.map.size;
160-
msgpack_object_kv *kv;
161-
msgpack_object key;
162-
msgpack_object val;
163-
char *key_str = NULL;
164-
char *val_str = NULL;
165-
size_t key_str_size = 0;
166-
size_t val_str_size = 0;
167-
int j;
168-
int check = FLB_FALSE;
169-
int found = FLB_FALSE;
170-
flb_sds_t tag;
161+
flb_sds_t tag = NULL;
162+
struct flb_ra_value *ra_val;
171163

172-
kv = map->via.map.ptr;
173-
174-
for(j=0; j < map_size; j++) {
175-
check = FLB_FALSE;
176-
found = FLB_FALSE;
177-
key = (kv+j)->key;
178-
if (key.type == MSGPACK_OBJECT_BIN) {
179-
key_str = (char *) key.via.bin.ptr;
180-
key_str_size = key.via.bin.size;
181-
check = FLB_TRUE;
182-
}
183-
if (key.type == MSGPACK_OBJECT_STR) {
184-
key_str = (char *) key.via.str.ptr;
185-
key_str_size = key.via.str.size;
186-
check = FLB_TRUE;
187-
}
164+
/* If no record accessor is configured, return NULL */
165+
if (!ctx->ra_tag_key) {
166+
return NULL;
167+
}
188168

189-
if (check == FLB_TRUE) {
190-
if (strncmp(ctx->tag_key, key_str, key_str_size) == 0) {
191-
val = (kv+j)->val;
192-
if (val.type == MSGPACK_OBJECT_BIN) {
193-
val_str = (char *) val.via.bin.ptr;
194-
val_str_size = val.via.str.size;
195-
found = FLB_TRUE;
196-
break;
197-
}
198-
if (val.type == MSGPACK_OBJECT_STR) {
199-
val_str = (char *) val.via.str.ptr;
200-
val_str_size = val.via.str.size;
201-
found = FLB_TRUE;
202-
break;
203-
}
204-
}
205-
}
169+
/* Use record accessor to get the value */
170+
ra_val = flb_ra_get_value_object(ctx->ra_tag_key, *map);
171+
if (!ra_val) {
172+
flb_plg_debug(ctx->ins, "Could not find tag_key %s in record", ctx->tag_key);
173+
return NULL;
206174
}
207175

208-
if (found == FLB_TRUE) {
209-
tag = flb_sds_create_len(val_str, val_str_size);
210-
if (!tag) {
211-
flb_errno();
212-
return NULL;
213-
}
214-
return tag;
176+
/* Convert the value to string */
177+
if (ra_val->type == FLB_RA_STRING) {
178+
tag = flb_sds_create_len(ra_val->o.via.str.ptr, ra_val->o.via.str.size);
179+
}
180+
else {
181+
flb_plg_debug(ctx->ins, "tag_key %s value is not a string", ctx->tag_key);
215182
}
216183

184+
/* Clean up the record accessor value */
185+
flb_ra_key_value_destroy(ra_val);
217186

218-
flb_plg_error(ctx->ins, "Could not find tag_key %s in record", ctx->tag_key);
219-
return NULL;
187+
return tag;
220188
}
221189

222190
/*

0 commit comments

Comments
 (0)