3333#include "opentelemetry_conf.h"
3434#include "opentelemetry_utils.h"
3535
36+ #define RESOURCE_LOGS_INITIAL_CAPACITY 256
37+
3638static 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
10301076start_resource :
0 commit comments