Skip to content

Commit 5d99d5a

Browse files
committed
in_http: use record accessor for tag key extraction
Replace manual key lookup with record accessor pattern for better performance and support for nested/complex key patterns. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 932a967 commit 5d99d5a

File tree

3 files changed

+59
-66
lines changed

3 files changed

+59
-66
lines changed

plugins/in_http/http.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>
@@ -36,7 +37,8 @@ struct flb_http {
3637
int successful_response_code;
3738
flb_sds_t listen;
3839
flb_sds_t tcp_port;
39-
const char *tag_key;
40+
flb_sds_t tag_key;
41+
struct flb_record_accessor *ra_tag_key;
4042

4143
/* Success HTTP headers */
4244
struct mk_list *success_headers;

plugins/in_http/http_config.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,27 @@ struct flb_http *http_config_create(struct flb_input_instance *ins)
7575

7676
if (ret != FLB_EVENT_ENCODER_SUCCESS) {
7777
flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret);
78-
7978
http_config_destroy(ctx);
80-
8179
return NULL;
8280
}
8381

8482
ctx->success_headers_str = flb_sds_create_size(1);
8583

8684
if (ctx->success_headers_str == NULL) {
8785
http_config_destroy(ctx);
88-
8986
return NULL;
9087
}
9188

89+
/* Create record accessor for tag_key if specified */
90+
if (ctx->tag_key) {
91+
ctx->ra_tag_key = flb_ra_create(ctx->tag_key, FLB_TRUE);
92+
if (!ctx->ra_tag_key) {
93+
flb_plg_error(ctx->ins, "invalid record accessor pattern for tag_key: %s", ctx->tag_key);
94+
http_config_destroy(ctx);
95+
return NULL;
96+
}
97+
}
98+
9299
flb_config_map_foreach(header_iterator, header_pair, ctx->success_headers) {
93100
header_name = mk_list_entry_first(header_pair->val.list,
94101
struct flb_slist_entry,
@@ -132,6 +139,10 @@ struct flb_http *http_config_create(struct flb_input_instance *ins)
132139

133140
int http_config_destroy(struct flb_http *ctx)
134141
{
142+
if (ctx->ra_tag_key) {
143+
flb_ra_destroy(ctx->ra_tag_key);
144+
}
145+
135146
/* release all connections */
136147
http_conn_release_all(ctx);
137148

plugins/in_http/http_prot.c

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <fluent-bit/flb_gzip.h>
2828
#include <fluent-bit/flb_zstd.h>
2929
#include <fluent-bit/flb_snappy.h>
30+
#include <fluent-bit/flb_record_accessor.h>
31+
#include <fluent-bit/flb_ra_key.h>
3032

3133
#include <monkey/monkey.h>
3234
#include <monkey/mk_core.h>
@@ -42,8 +44,8 @@ static inline char hex2nibble(char c)
4244
if ((c >= 0x30) && (c <= '9')) {
4345
return c - 0x30;
4446
}
45-
// 0x30-0x39 are digits, 0x41-0x46 A-F,
46-
// so there is a gap at 0x40
47+
48+
/* 0x30-0x39 are digits, 0x41-0x46 A-F, so there is a gap at 0x40 */
4749
if ((c >= 'A') && (c <= 'F')) {
4850
return (c - 'A') + 10;
4951
}
@@ -163,70 +165,54 @@ static int send_response(struct http_conn *conn, int http_status, char *message)
163165
return 0;
164166
}
165167

166-
/* implements functionality to get tag from key in record */
167-
static flb_sds_t tag_key(struct flb_http *ctx, msgpack_object *map)
168+
static void sanitize_tag(flb_sds_t tag)
168169
{
169-
size_t map_size = map->via.map.size;
170-
msgpack_object_kv *kv;
171-
msgpack_object key;
172-
msgpack_object val;
173-
char *key_str = NULL;
174-
char *val_str = NULL;
175-
size_t key_str_size = 0;
176-
size_t val_str_size = 0;
177-
int j;
178-
int check = FLB_FALSE;
179-
int found = FLB_FALSE;
180-
flb_sds_t tag;
170+
size_t i;
181171

182-
kv = map->via.map.ptr;
172+
if (!tag) {
173+
return;
174+
}
183175

184-
for(j=0; j < map_size; j++) {
185-
check = FLB_FALSE;
186-
found = FLB_FALSE;
187-
key = (kv+j)->key;
188-
if (key.type == MSGPACK_OBJECT_BIN) {
189-
key_str = (char *) key.via.bin.ptr;
190-
key_str_size = key.via.bin.size;
191-
check = FLB_TRUE;
192-
}
193-
if (key.type == MSGPACK_OBJECT_STR) {
194-
key_str = (char *) key.via.str.ptr;
195-
key_str_size = key.via.str.size;
196-
check = FLB_TRUE;
176+
for (i = 0; i < flb_sds_len(tag); i++) {
177+
if (!isalnum(tag[i]) && tag[i] != '_' && tag[i] != '.') {
178+
tag[i] = '_';
197179
}
180+
}
181+
}
198182

199-
if (check == FLB_TRUE) {
200-
if (strncmp(ctx->tag_key, key_str, key_str_size) == 0) {
201-
val = (kv+j)->val;
202-
if (val.type == MSGPACK_OBJECT_BIN) {
203-
val_str = (char *) val.via.bin.ptr;
204-
val_str_size = val.via.bin.size;
205-
found = FLB_TRUE;
206-
break;
207-
}
208-
if (val.type == MSGPACK_OBJECT_STR) {
209-
val_str = (char *) val.via.str.ptr;
210-
val_str_size = val.via.str.size;
211-
found = FLB_TRUE;
212-
break;
213-
}
214-
}
215-
}
183+
/* implements functionality to get tag from key in record */
184+
static flb_sds_t tag_key(struct flb_http *ctx, msgpack_object *map)
185+
{
186+
struct flb_ra_value *ra_val;
187+
flb_sds_t tag = NULL;
188+
189+
/* If no record accessor is configured, return NULL */
190+
if (!ctx->ra_tag_key) {
191+
return NULL;
216192
}
217193

218-
if (found == FLB_TRUE) {
219-
tag = flb_sds_create_len(val_str, val_str_size);
220-
if (!tag) {
221-
flb_errno();
222-
return NULL;
194+
/* Use record accessor to get the value */
195+
ra_val = flb_ra_get_value_object(ctx->ra_tag_key, *map);
196+
if (!ra_val) {
197+
flb_plg_debug(ctx->ins, "Could not find tag_key %s in record", ctx->tag_key);
198+
return NULL;
199+
}
200+
201+
/* Convert the value to string */
202+
if (ra_val->type == FLB_RA_STRING) {
203+
tag = flb_sds_create_len(ra_val->o.via.str.ptr, ra_val->o.via.str.size);
204+
if (tag) {
205+
sanitize_tag(tag);
223206
}
224-
return tag;
207+
}
208+
else {
209+
flb_plg_debug(ctx->ins, "tag_key %s value is not a string", ctx->tag_key);
225210
}
226211

212+
/* Clean up the record accessor value */
213+
flb_ra_key_value_destroy(ra_val);
227214

228-
flb_plg_warn(ctx->ins, "Could not find tag_key %s in record", ctx->tag_key);
229-
return NULL;
215+
return tag;
230216
}
231217

232218
static int process_pack_record(struct flb_http *ctx, struct flb_time *tm,
@@ -878,7 +864,6 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
878864
struct mk_http_session *session,
879865
struct mk_http_request *request)
880866
{
881-
int i;
882867
int ret;
883868
int len;
884869
char *uri;
@@ -927,12 +912,7 @@ int http_prot_handle(struct flb_http *ctx, struct http_conn *conn,
927912
/* New tag skipping the URI '/' */
928913
flb_sds_cat_safe(&tag, uri + 1, len - 1);
929914

930-
/* Sanitize, only allow alphanum chars */
931-
for (i = 0; i < flb_sds_len(tag); i++) {
932-
if (!isalnum(tag[i]) && tag[i] != '_' && tag[i] != '.') {
933-
tag[i] = '_';
934-
}
935-
}
915+
sanitize_tag(tag);
936916
}
937917

938918
mk_mem_free(uri);

0 commit comments

Comments
 (0)