Skip to content

Commit

Permalink
[pgexporter#72] Some empty queries break output
Browse files Browse the repository at this point in the history
  • Loading branch information
resyfer committed Jul 9, 2023
1 parent f27209d commit 3b424d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
82 changes: 36 additions & 46 deletions src/libpgexporter/prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef struct query_list
query_alts_t* query_alt;
char tag[MISC_LENGTH];
int sort_type;
bool error;
} query_list_t;

/**
Expand Down Expand Up @@ -1151,7 +1152,6 @@ custom_metrics(int client_fd)
// Iterate through each metric to send its query to PostgreSQL server
for (int i = 0; i < config->number_of_metrics; i++)
{
int err = false;
struct prometheus* prom = &config->prometheus[i];

/* Expose only if default or specified */
Expand All @@ -1176,43 +1176,29 @@ custom_metrics(int client_fd)
continue;
}

query_alts_t* query_alt = pgexporter_get_query_alt(prom->root, server);

if (!query_alt)
{
/* Skip */
continue;
}

// Setting Temp's value
query_list_t* next = malloc(sizeof(query_list_t));
memset(next, 0, sizeof(query_list_t));
if (!err)

if (!q_list)
{
if (!q_list)
{
q_list = next;
temp = q_list;
}
else if (temp->query)
{
temp->next = next;
temp = next;
}
q_list = next;
temp = q_list;
}
else
else if (temp && temp->query)
{
// Free node with previous data if previous query had an error while executing
pgexporter_free_query(temp->query);
if (temp)
{
temp->query_alt = NULL;
free(temp);
}

temp->next = next;
temp = next;
}

query_alts_t* query_alt = pgexporter_get_query_alt(prom->root, server);

if (!query_alt)
{
/* Skip */
continue;
}

/* Names */
char** names = malloc(query_alt->n_columns * sizeof(char*));
for (int j = 0; j < query_alt->n_columns; j++)
Expand All @@ -1225,12 +1211,12 @@ custom_metrics(int client_fd)
// Gather all the queries in a linked list, with each query's result (linked list of tuples in it) as a node.
if (query_alt->is_histogram)
{
err = pgexporter_custom_query(server, query_alt->query, prom->tag, -1, NULL, &temp->query);
temp->error = pgexporter_custom_query(server, query_alt->query, prom->tag, -1, NULL, &temp->query);
temp->sort_type = prom->sort_type;
}
else
{
err = pgexporter_custom_query(server, query_alt->query, prom->tag, query_alt->n_columns, names, &temp->query);
temp->error = pgexporter_custom_query(server, query_alt->query, prom->tag, query_alt->n_columns, names, &temp->query);
temp->sort_type = prom->sort_type;
}

Expand All @@ -1246,15 +1232,16 @@ custom_metrics(int client_fd)

while (temp)
{
if (temp->query_alt->is_histogram)
{
handle_histogram(store, &n_store, temp);
}
else
{
handle_gauge_counter(store, &n_store, temp);
if (temp->error || (temp->query != NULL && temp->query->tuples != NULL)) {
if (temp->query_alt->is_histogram)
{
handle_histogram(store, &n_store, temp);
}
else
{
handle_gauge_counter(store, &n_store, temp);
}
}

temp = temp->next;
}

Expand Down Expand Up @@ -1288,7 +1275,6 @@ custom_metrics(int client_fd)
query_list_t* last = NULL;
while (temp)
{

pgexporter_free_query(temp->query);
// temp->query_alt // Not freed here, but when program ends
last = temp;
Expand Down Expand Up @@ -1421,6 +1407,10 @@ handle_histogram(column_store_t* store, int* n_store, query_list_t* temp)
}
}

if(!temp || !temp->query || !temp->query->tuples) {
return;
}

struct tuple* tp = temp->query->tuples;

if (!tp)
Expand Down Expand Up @@ -1651,6 +1641,12 @@ handle_gauge_counter(column_store_t* store, int* n_store, query_list_t* temp)
}

append:
if (!temp || !temp->query || !temp->query->tuples)
{
/* Skip */
continue;
}

if (idx < (*n_store))
{
/* Found Match */
Expand Down Expand Up @@ -1714,12 +1710,6 @@ handle_gauge_counter(column_store_t* store, int* n_store, query_list_t* temp)
else
{
/* New Column */
if (!temp->query->tuples)
{
/* Skip */
continue;
}

(*n_store)++;

memcpy(store[idx].name, temp->query_alt->columns[i].name, MISC_LENGTH);
Expand Down
6 changes: 3 additions & 3 deletions src/libpgexporter/yaml_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ parse_queries(yaml_parser_t* parser_ptr, yaml_event_t* event_ptr, parser_state_t
/* Keys */

case YAML_SCALAR_EVENT:
if (*state_ptr != PARSER_VALUE && *state_ptr != PARSER_MAP_START)
if (*state_ptr != PARSER_VALUE && *state_ptr != PARSER_MAP_START && *state_ptr != PARSER_SEQ_END)
{
goto error;
}
Expand Down Expand Up @@ -1111,13 +1111,13 @@ semantics_yaml(struct prometheus* prometheus, yaml_config_t* yaml_config)
query_alts_t* new_query = malloc(sizeof(query_alts_t));
memset(new_query, 0, sizeof(query_alts_t));

new_query->n_columns = yaml_config->metrics[i].queries[j].n_columns;
new_query->n_columns = MIN(yaml_config->metrics[i].queries[j].n_columns, MAX_NUMBER_OF_COLUMNS);

memcpy(new_query->query, yaml_config->metrics[i].queries[j].query, MIN(MAX_QUERY_LENGTH - 1, strlen(yaml_config->metrics[i].queries[j].query)));
new_query->version = yaml_config->metrics[i].queries[j].version;

// Columns
for (int k = 0; k < MIN(yaml_config->metrics[i].queries[j].n_columns, MAX_NUMBER_OF_COLUMNS); k++)
for (int k = 0; k < new_query->n_columns; k++)
{

// Name
Expand Down

0 comments on commit 3b424d1

Please sign in to comment.