Skip to content

Commit 2fb74aa

Browse files
committed
out_gcs: Ensure uniqueness per gcs plugin instance
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent f3e0257 commit 2fb74aa

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

plugins/out_gcs/gcs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct flb_gcs {
8989
int compression_type;
9090
struct flb_fstore *fs;
9191
struct flb_fstore_stream *fs_stream;
92+
flb_sds_t fs_stream_name;
9293
struct mk_list upload_queue;
9394
time_t upload_timeout;
9495
int retry_time;

plugins/out_gcs/gcs_store.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@
2525
#include "gcs.h"
2626
#include "gcs_store.h"
2727

28+
static void normalize_stream_suffix(char *out, size_t out_size, const char *in)
29+
{
30+
size_t i;
31+
char ch;
32+
33+
if (!out || out_size == 0) {
34+
return;
35+
}
36+
37+
if (!in) {
38+
out[0] = '\0';
39+
return;
40+
}
41+
42+
for (i = 0; i < out_size - 1 && in[i] != '\0'; i++) {
43+
ch = in[i];
44+
if ((ch >= 'a' && ch <= 'z') ||
45+
(ch >= 'A' && ch <= 'Z') ||
46+
(ch >= '0' && ch <= '9') ||
47+
ch == '_' || ch == '-' || ch == '.') {
48+
out[i] = ch;
49+
}
50+
else {
51+
out[i] = '_';
52+
}
53+
}
54+
out[i] = '\0';
55+
}
56+
2857
static flb_sds_t gen_store_filename(void)
2958
{
3059
unsigned long hash;
@@ -54,22 +83,55 @@ static flb_sds_t gen_store_filename(void)
5483

5584
int gcs_store_init(struct flb_gcs *ctx)
5685
{
86+
const char *instance_name;
87+
char stream_suffix[96];
88+
flb_sds_t stream_name;
89+
90+
stream_name = flb_sds_create_size(64);
91+
if (!stream_name) {
92+
flb_errno();
93+
return -1;
94+
}
95+
5796
ctx->fs = flb_fstore_create(ctx->store_dir, FLB_FSTORE_FS);
5897
if (!ctx->fs) {
5998
return -1;
6099
}
61100

62-
ctx->fs_stream = flb_fstore_stream_create(ctx->fs, "gcs_upload_buffer");
101+
instance_name = ctx->ins->alias ? ctx->ins->alias : ctx->ins->name;
102+
normalize_stream_suffix(stream_suffix, sizeof(stream_suffix), instance_name);
103+
104+
flb_sds_printf(&stream_name, "gcs_upload_buffer_%s", stream_suffix);
105+
if (!stream_name) {
106+
flb_fstore_destroy(ctx->fs);
107+
ctx->fs = NULL;
108+
109+
return -1;
110+
}
111+
112+
ctx->fs_stream_name = stream_name;
113+
ctx->fs_stream = flb_fstore_stream_create(ctx->fs, ctx->fs_stream_name);
63114
if (!ctx->fs_stream) {
115+
flb_sds_destroy(ctx->fs_stream_name);
116+
ctx->fs_stream_name = NULL;
117+
flb_sds_destroy(stream_name);
64118
flb_fstore_destroy(ctx->fs);
65119
ctx->fs = NULL;
120+
66121
return -1;
67122
}
123+
flb_sds_destroy(stream_name);
124+
68125
return 0;
69126
}
70127

71128
int gcs_store_exit(struct flb_gcs *ctx)
72129
{
130+
if (ctx->fs_stream_name) {
131+
flb_sds_destroy(ctx->fs_stream_name);
132+
ctx->fs_stream_name = NULL;
133+
}
134+
73135
if (ctx->fs) {
74136
flb_fstore_destroy(ctx->fs);
75137
}

0 commit comments

Comments
 (0)