Skip to content

Commit 7fcec9d

Browse files
committed
out_opentelemetry: add configurable log resource limits
Before this patch we had a hard-coded maximum of 100 log resources per chunk. The change in question introduces a new configurable option called 'logs_max_resources' to control the maximum limit, the default value is: 0 (unlimited). Signed-off-by: Eduardo Silva <[email protected]>
1 parent 7dff9c0 commit 7fcec9d

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

plugins/out_opentelemetry/opentelemetry.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ static struct flb_config_map config_map[] = {
955955
},
956956

957957
{
958-
FLB_CONFIG_MAP_INT, "batch_size", DEFAULT_LOG_RECORD_BATCH_SIZE,
958+
FLB_CONFIG_MAP_INT, "batch_size", DEFAULT_LOG_RECORD_BATCH_SIZE,
959959
0, FLB_TRUE, offsetof(struct opentelemetry_context, batch_size),
960960
"Set the maximum number of log records to be flushed at a time"
961961
},
@@ -964,10 +964,17 @@ static struct flb_config_map config_map[] = {
964964
0, FLB_FALSE, 0,
965965
"Set payload compression mechanism. Options available are 'gzip' and 'zstd'."
966966
},
967+
967968
/*
968969
* Logs Properties
969970
* ---------------
970971
*/
972+
{
973+
FLB_CONFIG_MAP_INT, "logs_max_resources", DEFAULT_MAX_RESOURCE_EXPORT,
974+
0, FLB_TRUE, offsetof(struct opentelemetry_context, max_resources),
975+
"Set the maximum number of OTLP log resources per export request (0 disables the limit; default: 0)"
976+
},
977+
971978
{
972979
FLB_CONFIG_MAP_STR, "logs_uri", "/v1/logs",
973980
0, FLB_TRUE, offsetof(struct opentelemetry_context, logs_uri),

plugins/out_opentelemetry/opentelemetry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* including the ones that succeeded. This is not ideal.
4242
*/
4343
#define DEFAULT_LOG_RECORD_BATCH_SIZE "1000"
44+
#define DEFAULT_MAX_RESOURCE_EXPORT "0" /* no resource limits */
4445

4546
struct opentelemetry_body_key {
4647
flb_sds_t key;
@@ -139,6 +140,9 @@ struct opentelemetry_context {
139140
/* Number of logs to flush at a time */
140141
int batch_size;
141142

143+
/* Maximum number of resources per OTLP export */
144+
int max_resources;
145+
142146
/* Log the response payload */
143147
int log_response_payload;
144148

plugins/out_opentelemetry/opentelemetry_conf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ struct opentelemetry_context *flb_opentelemetry_context_create(struct flb_output
279279
return NULL;
280280
}
281281

282+
if (ctx->max_resources < 0) {
283+
flb_plg_error(ins, "max_resources must be greater than or equal to zero");
284+
flb_opentelemetry_context_destroy(ctx);
285+
return NULL;
286+
}
287+
282288
/* Parse 'add_label' */
283289
ret = config_add_labels(ins, ctx);
284290
if (ret == -1) {

plugins/out_opentelemetry/opentelemetry_logs.c

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "opentelemetry_conf.h"
3434
#include "opentelemetry_utils.h"
3535

36+
#define RESOURCE_LOGS_INITIAL_CAPACITY 256
37+
3638
static int hex_to_int(char ch)
3739
{
3840
if (ch >= '0' && ch <= '9') {
@@ -948,6 +950,9 @@ int otel_process_logs(struct flb_event_chunk *event_chunk,
948950
int max_scopes;
949951
int max_resources;
950952
int native_otel = FLB_FALSE;
953+
size_t resource_logs_capacity;
954+
size_t i;
955+
size_t new_capacity;
951956
int64_t resource_id = -1;
952957
int64_t scope_id = -1;
953958
int64_t tmp_resource_id = -1;
@@ -958,6 +963,7 @@ int otel_process_logs(struct flb_event_chunk *event_chunk,
958963
struct flb_record_accessor *ra_match;
959964
Opentelemetry__Proto__Collector__Logs__V1__ExportLogsServiceRequest export_logs;
960965
Opentelemetry__Proto__Logs__V1__ResourceLogs **resource_logs = NULL;
966+
Opentelemetry__Proto__Logs__V1__ResourceLogs **tmp_resource_logs = NULL;
961967
Opentelemetry__Proto__Logs__V1__ResourceLogs *resource_log = NULL;
962968
Opentelemetry__Proto__Logs__V1__ScopeLogs **scope_logs = NULL;
963969
Opentelemetry__Proto__Logs__V1__ScopeLogs *scope_log = NULL;
@@ -978,11 +984,19 @@ int otel_process_logs(struct flb_event_chunk *event_chunk,
978984
opentelemetry__proto__collector__logs__v1__export_logs_service_request__init(&export_logs);
979985

980986
/* local limits */
981-
max_resources = 100; /* maximim number of resources */
987+
max_resources = ctx->max_resources; /* maximum number of resources */
982988
max_scopes = 100; /* maximum number of scopes per resource */
983989

984-
/* allocate for 100 resource logs */
985-
resource_logs = flb_calloc(max_resources, sizeof(Opentelemetry__Proto__Logs__V1__ResourceLogs *));
990+
if (max_resources > 0) {
991+
resource_logs_capacity = max_resources;
992+
}
993+
else {
994+
resource_logs_capacity = RESOURCE_LOGS_INITIAL_CAPACITY; /* grow dynamically when unlimited */
995+
}
996+
997+
/* allocate storage for the configured number of resource logs */
998+
resource_logs = flb_calloc(resource_logs_capacity,
999+
sizeof(Opentelemetry__Proto__Logs__V1__ResourceLogs *));
9861000
if (!resource_logs) {
9871001
flb_errno();
9881002
flb_log_event_decoder_destroy(decoder);
@@ -1021,10 +1035,42 @@ int otel_process_logs(struct flb_event_chunk *event_chunk,
10211035

10221036
/* if we have a new resource_id, start a new resource context */
10231037
if (resource_id != tmp_resource_id) {
1024-
if (export_logs.n_resource_logs >= max_resources) {
1025-
flb_plg_error(ctx->ins, "max resources limit reached");
1026-
ret = FLB_ERROR;
1027-
break;
1038+
if (max_resources > 0) {
1039+
if (export_logs.n_resource_logs >= max_resources) {
1040+
/* respect the configured resource batching limit */
1041+
flb_plg_error(ctx->ins, "max resources limit reached");
1042+
ret = FLB_ERROR;
1043+
break;
1044+
}
1045+
}
1046+
else if (export_logs.n_resource_logs >= resource_logs_capacity) {
1047+
new_capacity = resource_logs_capacity * 2;
1048+
if (new_capacity <= resource_logs_capacity) {
1049+
flb_plg_error(ctx->ins, "resource logs capacity overflow");
1050+
ret = FLB_ERROR;
1051+
break;
1052+
}
1053+
1054+
if (new_capacity < RESOURCE_LOGS_INITIAL_CAPACITY) {
1055+
new_capacity = RESOURCE_LOGS_INITIAL_CAPACITY;
1056+
}
1057+
1058+
tmp_resource_logs = flb_realloc(resource_logs,
1059+
new_capacity * sizeof(Opentelemetry__Proto__Logs__V1__ResourceLogs *));
1060+
if (!tmp_resource_logs) {
1061+
flb_errno();
1062+
ret = FLB_RETRY;
1063+
break;
1064+
}
1065+
1066+
resource_logs = tmp_resource_logs;
1067+
1068+
for (i = resource_logs_capacity; i < new_capacity; i++) {
1069+
resource_logs[i] = NULL;
1070+
}
1071+
1072+
resource_logs_capacity = new_capacity;
1073+
export_logs.resource_logs = resource_logs;
10281074
}
10291075

10301076
start_resource:

0 commit comments

Comments
 (0)