Skip to content

Commit

Permalink
processor_calyptia: remove for loop initial declarations
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Padilha <[email protected]>
  • Loading branch information
tarruda committed Sep 2, 2024
1 parent de6a217 commit 716ad80
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion plugins/processor_calyptia/calyptia_logs_from_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
int calyptia_logs_from_lua(struct flb_processor_instance *ins, lua_State *L,
struct flb_mp_chunk_cobj *chunk_cobj)
{
int i;
int logs_length, metadata_length, timestamps_length;
struct flb_mp_chunk_record *record;

Expand Down Expand Up @@ -41,7 +42,7 @@ int calyptia_logs_from_lua(struct flb_processor_instance *ins, lua_State *L,
return -1;
}

for (int i = 1; i <= logs_length; i++) {
for (i = 1; i <= logs_length; i++) {
record = flb_mp_chunk_record_create(chunk_cobj);
if (!record) {
flb_plg_error(ins, "failed to create record");
Expand Down
30 changes: 20 additions & 10 deletions plugins/processor_calyptia/calyptia_metrics_from_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ struct metrics_header {

static void free_labels(char **labels, size_t label_count)
{
int i;
if (!labels) {
return;
}
for (int i = 0; i < label_count; i++) {
for (i = 0; i < label_count; i++) {
if (labels[i]) {
free(labels[i]);
}
Expand Down Expand Up @@ -59,7 +60,8 @@ static void split_fqname(const char *fqname, struct metrics_header *header)
static int assign_label(char **keys, size_t key_count, char **values,
const char *key, const char *value)
{
for (size_t i = 0; i < key_count; i++) {
size_t i;
for (i = 0; i < key_count; i++) {
if (!strcmp(keys[i], key)) {
values[i] = strdup(value);
if (!values[i]) {
Expand Down Expand Up @@ -124,13 +126,14 @@ static double *lua_to_quantile_values(struct flb_processor_instance *ins,
lua_State *L, double *quantile_keys,
int count)
{
int i;
double *quantile_values = calloc(count, sizeof(*quantile_values));
if (!quantile_values) {
flb_plg_error(ins, "could not allocate memory for quantile values");
return NULL;
}

for (int i = 0; i < count; i++) {
for (i = 0; i < count; i++) {
lua_pushnumber(L, quantile_keys[i]);
lua_gettable(L, -2);
quantile_values[i] = lua_to_double(L, -1);
Expand All @@ -144,13 +147,14 @@ static uint64_t *lua_to_bucket_values(struct flb_processor_instance *ins,
lua_State *L, double *bucket_keys,
int count)
{
int i;
uint64_t *values = calloc(count, sizeof(*values));
if (!values) {
flb_plg_error(ins, "could not allocate memory for bucket values");
return NULL;
}

for (int i = 0; i < count; i++) {
for (i = 0; i < count; i++) {
lua_pushnumber(L, bucket_keys[i]);
lua_gettable(L, -2);
values[i] = lua_to_uint(L);
Expand All @@ -163,6 +167,7 @@ static uint64_t *lua_to_bucket_values(struct flb_processor_instance *ins,
static double *lua_to_quantiles_buckets(struct flb_processor_instance *ins,
lua_State *L, int *count)
{
int i;
double *keys;
*count = 0;
if (lua_type(L, -1) != LUA_TTABLE) {
Expand All @@ -183,7 +188,7 @@ static double *lua_to_quantiles_buckets(struct flb_processor_instance *ins,
}

lua_pushnil(L); // first key
int i = 0;
i = 0;
while (lua_next(L, -2) != 0) {
keys[i] = lua_to_double(L, -2);
i++;
Expand All @@ -200,6 +205,7 @@ static double *lua_to_quantile_bucket_keys(struct flb_processor_instance *ins,
lua_State *L, const char *kind,
int *count)
{
int i;
int sample_count;
double *keys;

Expand All @@ -214,7 +220,7 @@ static double *lua_to_quantile_bucket_keys(struct flb_processor_instance *ins,
int found = 0;

/* find the first sample that has quantiles */
for (int i = 1; i <= sample_count; i++) {
for (i = 1; i <= sample_count; i++) {
lua_rawgeti(L, -1, i);
lua_getfield(L, -1, kind);
if (lua_type(L, -1) == LUA_TTABLE) {
Expand All @@ -240,7 +246,8 @@ static double *lua_to_quantile_bucket_keys(struct flb_processor_instance *ins,
static char **append_label(char **labels, size_t *labels_size,
size_t *label_index, const char *label)
{
for (size_t i = 0; i < *label_index; i++) {
size_t i;
for (i = 0; i < *label_index; i++) {
if (!strcmp(labels[i], label)) {
/* don't do anything if the label is already in the array */
return labels;
Expand Down Expand Up @@ -272,6 +279,7 @@ static char **append_label(char **labels, size_t *labels_size,
static char **lua_to_label_keys(struct flb_processor_instance *ins,
lua_State *L, int *label_count)
{
int i;
int sample_count;
char **label_keys;
size_t label_index;
Expand All @@ -295,7 +303,7 @@ static char **lua_to_label_keys(struct flb_processor_instance *ins,
labels_size = 0;
label_index = 0;

for (int i = 1; i <= sample_count; i++) {
for (i = 1; i <= sample_count; i++) {
lua_rawgeti(L, -1, i);
if (lua_type(L, -1) != LUA_TTABLE) {
free_labels(label_keys, label_index);
Expand Down Expand Up @@ -357,6 +365,8 @@ int calyptia_metrics_from_lua(struct flb_processor_instance *ins, lua_State *L,
char **label_vals;
int label_count;
uint64_t timestamp;
int i;
int j;

if (lua_type(L, -1) != LUA_TTABLE) {
flb_plg_error(ins, "expected metrics array");
Expand All @@ -365,7 +375,7 @@ int calyptia_metrics_from_lua(struct flb_processor_instance *ins, lua_State *L,

metric_count = lua_objlen(L, -1);

for (int i = 1; i <= metric_count; i++) {
for (i = 1; i <= metric_count; i++) {
lua_rawgeti(L, -1, i);

label_keys = lua_to_label_keys(ins, L, &label_count);
Expand Down Expand Up @@ -465,7 +475,7 @@ int calyptia_metrics_from_lua(struct flb_processor_instance *ins, lua_State *L,
return -1;
}

for (int j = 1; j <= sample_count; j++) {
for (j = 1; j <= sample_count; j++) {
label_vals = NULL;

/* get sample */
Expand Down
3 changes: 2 additions & 1 deletion plugins/processor_calyptia/calyptia_metrics_to_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static void push_histogram(lua_State *L, struct cmt_map *map,
static void push_summary(lua_State *L, struct cmt_map *map,
struct cmt_metric *metric)
{
int i;
struct cmt_summary *summary;
struct cmt_opts *opts;

Expand All @@ -89,7 +90,7 @@ static void push_summary(lua_State *L, struct cmt_map *map,

if (metric->sum_quantiles_set) {
lua_createtable(L, summary->quantiles_count, 0);
for (int i = 0; i < summary->quantiles_count; i++) {
for (i = 0; i < summary->quantiles_count; i++) {
lua_pushnumber(L, summary->quantiles[i]);
lua_pushnumber(L, cmt_summary_quantile_get_value(metric, i));
lua_settable(L, -3);
Expand Down
15 changes: 10 additions & 5 deletions plugins/processor_calyptia/calyptia_traces_from_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ static void lua_to_links(lua_State *L, struct ctrace_span *span)
{
struct ctrace_link *link;
size_t count;
size_t i;
struct ctrace_id *trace_id;

if (lua_type(L, -1) != LUA_TTABLE) {
return;
}

count = lua_objlen(L, -1);
for (size_t i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) {
lua_rawgeti(L, -1, i);

lua_getfield(L, -1, "traceId");
Expand Down Expand Up @@ -68,14 +69,15 @@ static void lua_to_events(lua_State *L, struct ctrace_span *span)
{
struct ctrace_span_event *event;
size_t count;
size_t i;
const char *name;

if (lua_type(L, -1) != LUA_TTABLE) {
return;
}

count = lua_objlen(L, -1);
for (size_t i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) {
lua_rawgeti(L, -1, i);

lua_getfield(L, -1, "name");
Expand Down Expand Up @@ -154,14 +156,15 @@ static void lua_to_spans(lua_State *L, struct ctrace *ctx,
size_t count;
struct ctrace_span *span;
cfl_sds_t name;
size_t i;
struct ctrace_id *parent_span_id;

if (lua_type(L, -1) != LUA_TTABLE) {
return;
}

count = lua_objlen(L, -1);
for (size_t i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) {
lua_rawgeti(L, -1, i);

lua_getfield(L, -1, "name");
Expand Down Expand Up @@ -224,6 +227,7 @@ static void lua_to_scope_spans(lua_State *L, struct ctrace *ctx,
struct ctrace_resource_span *resource_span)
{
size_t count;
size_t i;
struct ctrace_scope_span *scope_span;

if (lua_type(L, -1) != LUA_TTABLE) {
Expand All @@ -232,7 +236,7 @@ static void lua_to_scope_spans(lua_State *L, struct ctrace *ctx,

count = lua_objlen(L, -1);

for (size_t i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) {
scope_span = ctr_scope_span_create(resource_span);

lua_rawgeti(L, -1, i);
Expand All @@ -257,6 +261,7 @@ static void lua_to_scope_spans(lua_State *L, struct ctrace *ctx,
static void lua_to_resource_spans(lua_State *L, struct ctrace *ctx)
{
size_t count;
size_t i;
struct ctrace_resource_span *resource_span;

if (lua_type(L, -1) != LUA_TTABLE) {
Expand All @@ -265,7 +270,7 @@ static void lua_to_resource_spans(lua_State *L, struct ctrace *ctx)

count = lua_objlen(L, -1);

for (size_t i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) {
resource_span = ctr_resource_span_create(ctx);

lua_rawgeti(L, -1, i);
Expand Down
3 changes: 2 additions & 1 deletion plugins/processor_calyptia/lua_to_cfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ bool lua_isinteger(lua_State *L, int index)

struct cfl_array *lua_array_to_variant(lua_State *L, int array_len)
{
int i;
struct cfl_array *array = cfl_array_create(array_len);

for (int i = 1; i <= array_len; i++) {
for (i = 1; i <= array_len; i++) {
lua_rawgeti(L, -1, i);
struct cfl_variant *variant = lua_to_variant(L, -1);
cfl_array_append(array, variant);
Expand Down

0 comments on commit 716ad80

Please sign in to comment.