Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_es: support write_operation option #4079

Merged
merged 8 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion plugins/out_es/es.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ static int elasticsearch_format(struct flb_config *config,
char index_formatted[256];
char es_uuid[37];
flb_sds_t out_buf;
size_t out_buf_len = 0;
flb_sds_t tmp_buf;
flb_sds_t id_key_str = NULL;
msgpack_unpacked result;
msgpack_object root;
Expand Down Expand Up @@ -311,6 +313,10 @@ static int elasticsearch_format(struct flb_config *config,
msgpack_unpacked_destroy(&result);
msgpack_unpacked_init(&result);

flb_sds_t write_op = strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_UPSERT) == 0
nokute78 marked this conversation as resolved.
Show resolved Hide resolved
? FLB_ES_WRITE_OP_UPDATE
: ctx->write_operation;

/* Copy logstash prefix if logstash format is enabled */
if (ctx->logstash_format == FLB_TRUE) {
memcpy(logstash_index, ctx->logstash_prefix, flb_sds_len(ctx->logstash_prefix));
Expand All @@ -334,12 +340,14 @@ static int elasticsearch_format(struct flb_config *config,
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_WITHOUT_TYPE,
write_op,
es_index);
}
else {
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT,
write_op,
es_index, ctx->type);
}
}
Expand Down Expand Up @@ -446,12 +454,14 @@ static int elasticsearch_format(struct flb_config *config,
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_WITHOUT_TYPE,
write_op,
es_index);
}
else {
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT,
write_op,
es_index, ctx->type);
}
}
Expand Down Expand Up @@ -496,12 +506,14 @@ static int elasticsearch_format(struct flb_config *config,
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_ID_WITHOUT_TYPE,
write_op,
es_index, es_uuid);
}
else {
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_ID,
write_op,
es_index, ctx->type, es_uuid);
}
}
Expand All @@ -512,12 +524,14 @@ static int elasticsearch_format(struct flb_config *config,
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_ID_WITHOUT_TYPE,
write_op,
es_index, id_key_str);
}
else {
index_len = snprintf(j_index,
ES_BULK_HEADER,
ES_BULK_INDEX_FMT_ID,
write_op,
es_index, ctx->type, id_key_str);
}
flb_sds_destroy(id_key_str);
Expand All @@ -534,10 +548,25 @@ static int elasticsearch_format(struct flb_config *config,
return -1;
}

out_buf_len = flb_sds_len(out_buf);
if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_UPDATE) == 0) {
tmp_buf = out_buf;
out_buf = flb_sds_create_len(NULL, out_buf_len + sizeof(ES_BULK_UPDATE_OP_BODY) - 2);
out_buf_len = sprintf(out_buf, ES_BULK_UPDATE_OP_BODY, tmp_buf);
nokute78 marked this conversation as resolved.
Show resolved Hide resolved
flb_sds_destroy(tmp_buf);
}
else if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_UPSERT) == 0) {
tmp_buf = out_buf;
out_buf = flb_sds_create_len(NULL, out_buf_len + sizeof(ES_BULK_UPSERT_OP_BODY) - 2);
out_buf_len = sprintf(out_buf, ES_BULK_UPSERT_OP_BODY, tmp_buf);
nokute78 marked this conversation as resolved.
Show resolved Hide resolved
flb_sds_destroy(tmp_buf);
}

ret = es_bulk_append(bulk, j_index, index_len,
out_buf, flb_sds_len(out_buf),
out_buf, out_buf_len,
bytes, off_prev);
flb_sds_destroy(out_buf);

off_prev = off;
if (ret == -1) {
/* We likely ran out of memory, abort here */
Expand Down Expand Up @@ -1052,6 +1081,11 @@ static struct flb_config_map config_map[] = {
"When enabled, generate _id for outgoing records. This prevents duplicate "
"records when retrying ES"
},
{
FLB_CONFIG_MAP_STR, "write_operation", "index",
nokute78 marked this conversation as resolved.
Show resolved Hide resolved
0, FLB_TRUE, offsetof(struct flb_elasticsearch, write_operation),
"Operation to use to write in bulk requests"
},
{
FLB_CONFIG_MAP_STR, "id_key", NULL,
0, FLB_TRUE, offsetof(struct flb_elasticsearch, id_key),
Expand Down
10 changes: 9 additions & 1 deletion plugins/out_es/es.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#define FLB_ES_DEFAULT_TIME_KEYF "%Y-%m-%dT%H:%M:%S"
#define FLB_ES_DEFAULT_TAG_KEY "flb-key"
#define FLB_ES_DEFAULT_HTTP_MAX "512k"
#define FLB_ES_WRITE_OP_INDEX "index"
#define FLB_ES_WRITE_OP_CREATE "create"
#define FLB_ES_WRITE_OP_UPDATE "update"
#define FLB_ES_WRITE_OP_UPSERT "upsert"
#define FLB_ES_DEFAULT_WRITE_OP FLB_ES_WRITE_OP_INDEX
nokute78 marked this conversation as resolved.
Show resolved Hide resolved

struct flb_elasticsearch {
/* Elasticsearch index (database) and type (table) */
Expand Down Expand Up @@ -101,10 +106,13 @@ struct flb_elasticsearch {
int time_key_nanos;


/* write operation */
flb_sds_t write_operation;

/* id_key */
flb_sds_t id_key;
struct flb_record_accessor *ra_id_key;

/* include_tag_key */
int include_tag_key;
flb_sds_t tag_key;
Expand Down
10 changes: 6 additions & 4 deletions plugins/out_es/es_bulk.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

#define ES_BULK_CHUNK 4096 /* Size of buffer chunks */
#define ES_BULK_HEADER 165 /* ES Bulk API prefix line */
#define ES_BULK_INDEX_FMT "{\"create\":{\"_index\":\"%s\",\"_type\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID "{\"create\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_WITHOUT_TYPE "{\"create\":{\"_index\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID_WITHOUT_TYPE "{\"create\":{\"_index\":\"%s\",\"_id\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT "{\"%s\":{\"_index\":\"%s\",\"_type\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID "{\"%s\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_WITHOUT_TYPE "{\"%s\":{\"_index\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID_WITHOUT_TYPE "{\"%s\":{\"_index\":\"%s\",\"_id\":\"%s\"}}\n"
#define ES_BULK_UPDATE_OP_BODY "{\"doc\":%s}"
#define ES_BULK_UPSERT_OP_BODY "{\"doc_as_upsert\":true,\"doc\":%s}"

struct es_bulk {
char *ptr;
Expand Down
21 changes: 21 additions & 0 deletions plugins/out_es/es_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ struct flb_elasticsearch *flb_es_conf_create(struct flb_output_instance *ins,
snprintf(ctx->uri, sizeof(ctx->uri) - 1, "%s/_bulk", path);
}

if (ctx->write_operation) {
if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_INDEX) == 0) {
memcpy(ctx->write_operation, FLB_ES_WRITE_OP_INDEX,
sizeof(FLB_ES_WRITE_OP_INDEX));
}
else if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_CREATE) == 0) {
memcpy(ctx->write_operation, FLB_ES_WRITE_OP_CREATE,
sizeof(FLB_ES_WRITE_OP_CREATE));
}
else if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_UPDATE) == 0) {
memcpy(ctx->write_operation, FLB_ES_WRITE_OP_UPDATE,
sizeof(FLB_ES_WRITE_OP_UPDATE));
}
else if (strcasecmp(ctx->write_operation, FLB_ES_WRITE_OP_UPSERT) == 0) {
memcpy(ctx->write_operation, FLB_ES_WRITE_OP_UPSERT,
sizeof(FLB_ES_WRITE_OP_UPSERT));
}
nokute78 marked this conversation as resolved.
Show resolved Hide resolved
else {
flb_plg_error(ins, "wrong Write_Operation (should be one of index, create, update, upsert)");
}
}

if (ctx->id_key) {
ctx->ra_id_key = flb_ra_create(ctx->id_key, FLB_FALSE);
Expand Down
Loading