Skip to content

Commit 4d7818a

Browse files
committed
processor_sampling: do a hard copy of attributes when reconciling
Signed-off-by: Eduardo Silva <[email protected]>
1 parent b9f41c5 commit 4d7818a

File tree

1 file changed

+128
-2
lines changed

1 file changed

+128
-2
lines changed

plugins/processor_sampling/sampling_tail.c

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static struct flb_config_map settings_config_map[] = {
5757
{0}
5858
};
5959

60+
static struct cfl_array *copy_array(struct cfl_array *array);
61+
static struct cfl_variant *copy_variant(struct cfl_variant *val);
62+
static struct cfl_kvlist *copy_kvlist(struct cfl_kvlist *kv);
63+
6064
/* delete a list ctrace entry */
6165
static void list_ctrace_delete_entry(struct sampling *ctx, struct sampling_ctrace_entry *ctrace_entry)
6266
{
@@ -92,11 +96,114 @@ static void list_ctrace_delete_all(struct sampling *ctx, struct sampling_setting
9296
}
9397
}
9498

99+
static struct cfl_kvlist *copy_kvlist(struct cfl_kvlist *kv)
100+
{
101+
struct cfl_kvlist *kvlist = NULL;
102+
struct cfl_kvpair *pair;
103+
struct cfl_variant *v;
104+
struct cfl_list *head;
105+
106+
kvlist = cfl_kvlist_create();
107+
if (!kvlist) {
108+
return NULL;
109+
}
110+
111+
cfl_list_foreach(head, &kv->list) {
112+
pair = cfl_list_entry(head, struct cfl_kvpair, _head);
113+
v = copy_variant(pair->val);
114+
if (!v) {
115+
cfl_kvlist_destroy(kvlist);
116+
return NULL;
117+
}
118+
cfl_kvlist_insert(kvlist, pair->key, v);
119+
}
120+
121+
return kvlist;
122+
}
123+
124+
static struct cfl_variant *copy_variant(struct cfl_variant *val)
125+
{
126+
struct cfl_kvlist *kvlist;
127+
struct cfl_array *array;
128+
struct cfl_variant *var = NULL;
129+
130+
switch (val->type) {
131+
case CFL_VARIANT_STRING:
132+
var = cfl_variant_create_from_string_s(val->data.as_string,
133+
cfl_variant_size_get(val),
134+
CFL_FALSE);
135+
break;
136+
case CFL_VARIANT_BYTES:
137+
var = cfl_variant_create_from_bytes(val->data.as_bytes,
138+
cfl_variant_size_get(val),
139+
CFL_FALSE);
140+
break;
141+
case CFL_VARIANT_BOOL:
142+
var = cfl_variant_create_from_bool(val->data.as_bool);
143+
break;
144+
case CFL_VARIANT_INT:
145+
var = cfl_variant_create_from_int64(val->data.as_int64);
146+
break;
147+
case CFL_VARIANT_UINT:
148+
var = cfl_variant_create_from_uint64(val->data.as_uint64);
149+
break;
150+
case CFL_VARIANT_DOUBLE:
151+
var = cfl_variant_create_from_double(val->data.as_double);
152+
break;
153+
case CFL_VARIANT_NULL:
154+
var = cfl_variant_create_from_null();
155+
break;
156+
case CFL_VARIANT_ARRAY:
157+
array = copy_array(val->data.as_array);
158+
if (!array) {
159+
return NULL;
160+
}
161+
var = cfl_variant_create_from_array(array);
162+
break;
163+
case CFL_VARIANT_KVLIST:
164+
kvlist = copy_kvlist(val->data.as_kvlist);
165+
if (!kvlist) {
166+
return NULL;
167+
}
168+
var = cfl_variant_create_from_kvlist(kvlist);
169+
break;
170+
default:
171+
var = NULL;
172+
}
173+
174+
return var;
175+
}
176+
177+
static struct cfl_array *copy_array(struct cfl_array *array)
178+
{
179+
int i;
180+
struct cfl_array *copy;
181+
struct cfl_variant *v ;
182+
183+
copy = cfl_array_create(array->entry_count);
184+
if (!copy) {
185+
return NULL;
186+
}
187+
188+
for (i = 0; i < array->entry_count; i++) {
189+
v = copy_variant(array->entries[i]);
190+
if (!v) {
191+
cfl_array_destroy(copy);
192+
return NULL;
193+
}
194+
cfl_array_append(copy, v);
195+
}
196+
197+
return copy;
198+
}
199+
95200
struct ctrace_attributes *copy_attributes(struct sampling *ctx, struct ctrace_attributes *attr)
96201
{
97202
int ret = -1;
98203
struct cfl_list *head;
99204
struct cfl_kvpair *pair;
205+
struct cfl_array *array;
206+
struct cfl_kvlist *kvlist;
100207
struct ctrace_attributes *attr_copy;
101208

102209
attr_copy = ctr_attributes_create();
@@ -120,10 +227,29 @@ struct ctrace_attributes *copy_attributes(struct sampling *ctx, struct ctrace_at
120227
ret = ctr_attributes_set_double(attr_copy, pair->key, pair->val->data.as_double);
121228
}
122229
else if (pair->val->type == CFL_VARIANT_ARRAY) {
123-
ret = ctr_attributes_set_array(attr_copy, pair->key, pair->val->data.as_array);
230+
array = copy_array(pair->val->data.as_array);
231+
if (!array) {
232+
flb_plg_error(ctx->ins, "could not copy array attribute");
233+
ctr_attributes_destroy(attr_copy);
234+
return NULL;
235+
}
236+
237+
ret = ctr_attributes_set_array(attr_copy, pair->key, array);
238+
if (ret != 0) {
239+
cfl_array_destroy(array);
240+
}
124241
}
125242
else if (pair->val->type == CFL_VARIANT_KVLIST) {
126-
ret = ctr_attributes_set_kvlist(attr_copy, pair->key, pair->val->data.as_kvlist);
243+
kvlist = copy_kvlist(pair->val->data.as_kvlist);
244+
if (!kvlist) {
245+
flb_plg_error(ctx->ins, "could not copy kvlist attribute");
246+
ctr_attributes_destroy(attr_copy);
247+
return NULL;
248+
}
249+
ret = ctr_attributes_set_kvlist(attr_copy, pair->key, kvlist);
250+
if (ret != 0) {
251+
cfl_kvlist_destroy(kvlist);
252+
}
127253
}
128254
else {
129255
flb_plg_error(ctx->ins, "unsupported attribute type %i", pair->val->type);

0 commit comments

Comments
 (0)