2424#include <fluent-bit/flb_pack.h>
2525#include <fluent-bit/flb_config_map.h>
2626#include <fluent-bit/flb_gzip.h>
27+ #include <fluent-bit/flb_compression.h>
28+ #include <fluent-bit/flb_zstd.h>
2729#include <fluent-bit/flb_base64.h>
2830#include <fluent-bit/flb_sqldb.h>
2931#include <fluent-bit/flb_input_blob.h>
@@ -136,7 +138,7 @@ static int construct_request_buffer(struct flb_azure_blob *ctx, flb_sds_t new_da
136138 }
137139 body = buffered_data = tmp ;
138140 memcpy (body + buffer_size , new_data , flb_sds_len (new_data ));
139- if (ctx -> compress_gzip == FLB_FALSE ) {
141+ if (ctx -> compression == FLB_COMPRESSION_ALGORITHM_NONE ) {
140142 body [body_size ] = '\0' ;
141143 }
142144 }
@@ -149,6 +151,38 @@ static int construct_request_buffer(struct flb_azure_blob *ctx, flb_sds_t new_da
149151 return 0 ;
150152}
151153
154+ /*
155+ * Compress a payload using the configured algorithm. Returns 0 on success and
156+ * negative on failure so callers can gracefully fall back to sending the raw
157+ * payload.
158+ */
159+ static int azure_blob_compress_payload (int algorithm ,
160+ void * in_data , size_t in_len ,
161+ void * * out_data , size_t * out_len )
162+ {
163+ if (algorithm == FLB_COMPRESSION_ALGORITHM_GZIP ) {
164+ return flb_gzip_compress (in_data , in_len , out_data , out_len );
165+ }
166+ else if (algorithm == FLB_COMPRESSION_ALGORITHM_ZSTD ) {
167+ return flb_zstd_compress (in_data , in_len , out_data , out_len );
168+ }
169+
170+ return -1 ;
171+ }
172+
173+ /* Map a compression algorithm to its human-friendly label for logs. */
174+ static const char * azure_blob_compression_name (int algorithm )
175+ {
176+ if (algorithm == FLB_COMPRESSION_ALGORITHM_GZIP ) {
177+ return "gzip" ;
178+ }
179+ else if (algorithm == FLB_COMPRESSION_ALGORITHM_ZSTD ) {
180+ return "zstd" ;
181+ }
182+
183+ return "unknown" ;
184+ }
185+
152186void generate_random_string_blob (char * str , size_t length )
153187{
154188 const char charset [] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
@@ -332,8 +366,12 @@ static int http_send_blob(struct flb_config *config, struct flb_azure_blob *ctx,
332366{
333367 int ret ;
334368 int compressed = FLB_FALSE ;
335- int content_encoding = FLB_FALSE ;
336- int content_type = FLB_FALSE ;
369+ int content_encoding = AZURE_BLOB_CE_NONE ;
370+ int content_type = AZURE_BLOB_CT_NONE ;
371+ int compression_algorithm = FLB_COMPRESSION_ALGORITHM_NONE ;
372+ int network_compression_algorithm = ctx -> compression ;
373+ int network_compression_applied = FLB_FALSE ;
374+ int blob_compression_applied = FLB_FALSE ;
337375 size_t b_sent ;
338376 void * payload_buf ;
339377 size_t payload_size ;
@@ -358,27 +396,66 @@ static int http_send_blob(struct flb_config *config, struct flb_azure_blob *ctx,
358396 payload_buf = data ;
359397 payload_size = bytes ;
360398
399+ /* Determine compression algorithm */
400+ if (network_compression_algorithm != FLB_COMPRESSION_ALGORITHM_NONE ) {
401+ compression_algorithm = network_compression_algorithm ;
402+ }
403+
404+ if (ctx -> compress_blob == FLB_TRUE ) {
405+ if (compression_algorithm == FLB_COMPRESSION_ALGORITHM_NONE ) {
406+ compression_algorithm = FLB_COMPRESSION_ALGORITHM_GZIP ;
407+ }
408+ }
409+
361410 /* Handle compression requests */
362- if (ctx -> compress_gzip == FLB_TRUE || ctx -> compress_blob == FLB_TRUE ) {
363- ret = flb_gzip_compress ((void * ) data , bytes , & payload_buf , & payload_size );
411+ if (compression_algorithm != FLB_COMPRESSION_ALGORITHM_NONE ) {
412+ ret = azure_blob_compress_payload (compression_algorithm ,
413+ (void * ) data , bytes ,
414+ & payload_buf , & payload_size );
364415 if (ret == 0 ) {
365416 compressed = FLB_TRUE ;
417+ if (network_compression_algorithm != FLB_COMPRESSION_ALGORITHM_NONE &&
418+ compression_algorithm == network_compression_algorithm ) {
419+ network_compression_applied = FLB_TRUE ;
420+ }
421+ else if (ctx -> compress_blob == FLB_TRUE ) {
422+ blob_compression_applied = FLB_TRUE ;
423+ }
366424 }
367425 else {
426+ const char * alg_name ;
427+
428+ alg_name = azure_blob_compression_name (compression_algorithm );
368429 flb_plg_warn (ctx -> ins ,
369- "cannot gzip payload, disabling compression" );
430+ "cannot %s payload, disabling compression" ,
431+ alg_name );
370432 payload_buf = data ;
371433 payload_size = bytes ;
434+ compression_algorithm = FLB_COMPRESSION_ALGORITHM_NONE ;
372435 }
373436 }
374437
375438 /* set http header flags */
376- if (ctx -> compress_blob == FLB_TRUE ) {
439+ /* Blob compression takes precedence over network compression since we
440+ * cannot advertise both a compressed blob content type and a transport
441+ * content-encoding at the same time. */
442+ if (blob_compression_applied == FLB_TRUE ) {
377443 content_encoding = AZURE_BLOB_CE_NONE ;
378- content_type = AZURE_BLOB_CT_GZIP ;
444+
445+ if (compression_algorithm == FLB_COMPRESSION_ALGORITHM_ZSTD ) {
446+ content_type = AZURE_BLOB_CT_ZSTD ;
447+ }
448+ else {
449+ content_type = AZURE_BLOB_CT_GZIP ;
450+ }
379451 }
380- else if (compressed == FLB_TRUE ) {
381- content_encoding = AZURE_BLOB_CE_GZIP ;
452+ else if (network_compression_applied == FLB_TRUE ) {
453+ if (network_compression_algorithm == FLB_COMPRESSION_ALGORITHM_GZIP ) {
454+ content_encoding = AZURE_BLOB_CE_GZIP ;
455+ }
456+ else if (network_compression_algorithm == FLB_COMPRESSION_ALGORITHM_ZSTD ) {
457+ content_encoding = AZURE_BLOB_CE_ZSTD ;
458+ }
382459 content_type = AZURE_BLOB_CT_JSON ;
383460 }
384461
@@ -1783,14 +1860,15 @@ static struct flb_config_map config_map[] = {
17831860 {
17841861 FLB_CONFIG_MAP_STR , "compress" , NULL ,
17851862 0 , FLB_FALSE , 0 ,
1786- "Set payload compression in network transfer. Option available is 'gzip'"
1863+ "Set payload compression in network transfer. Options: 'gzip', 'zstd '"
17871864 },
17881865
17891866 {
17901867 FLB_CONFIG_MAP_BOOL , "compress_blob" , "false" ,
17911868 0 , FLB_TRUE , offsetof(struct flb_azure_blob , compress_blob ),
1792- "Enable block blob GZIP compression in the final blob file. This option is "
1793- "not compatible with 'appendblob' block type"
1869+ "Enable block blob compression in the final blob file (defaults to gzip, "
1870+ "uses the 'compress' codec when set). This option is not compatible with "
1871+ "'appendblob' block type"
17941872 },
17951873
17961874 {
0 commit comments