Skip to content

Commit

Permalink
Merge pull request #4620 from sysown/v2.x-fix_connpool_metrics_init_val
Browse files Browse the repository at this point in the history
Fix value on first fetch for connpool prometheus metrics
  • Loading branch information
renecannao authored Aug 21, 2024
2 parents daf2b0d + 99afddb commit a52fd0b
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/MySQL_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3314,12 +3314,13 @@ void MySQL_HostGroups_Manager::p_update_connection_pool_update_counter(
counter_id->second->Increment(value - cur_val);
} else {
auto& new_counter = status.p_dyn_counter_array[idx];
m_map.insert(
const auto& new_counter_it = m_map.insert(
{
endpoint_id,
std::addressof(new_counter->Add(labels))
}
);
new_counter_it.first->second->Increment(value);
}
}

Expand All @@ -3332,12 +3333,13 @@ void MySQL_HostGroups_Manager::p_update_connection_pool_update_gauge(
counter_id->second->Set(value);
} else {
auto& new_counter = status.p_dyn_gauge_array[idx];
m_map.insert(
const auto& new_gauge_it = m_map.insert(
{
endpoint_id,
std::addressof(new_counter->Add(labels))
}
);
new_gauge_it.first->second->Set(value);
}
}

Expand Down
28 changes: 16 additions & 12 deletions test/tap/tap/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,23 +1607,27 @@ json fetch_internal_session(MYSQL* proxy, bool verbose) {
}
}

pair<string, string> split_line_by_last(const string& ln, char c) {
size_t pos = ln.find_last_of(c);

if (pos == string::npos) {
return { ln, "" };
} else {
const string f { ln.substr(0, pos) };
const string s { ln.substr(pos + 1) };

return { f, s };
}
}

map<string, double> parse_prometheus_metrics(const string& s) {
vector<string> lines { split(s, '\n') };
const vector<string> lines { split(s, '\n') };
map<string, double> metrics_map {};

for (const string ln : lines) {
const vector<string> line_values { split(ln, ' ') };

if (ln.empty() == false && ln[0] != '#') {
if (line_values.size() > 2) {
size_t delim_pos_st = ln.rfind("} ");
string metric_key = ln.substr(0, delim_pos_st);
string metric_val = ln.substr(delim_pos_st + 2);

metrics_map.insert({metric_key, stod(metric_val)});
} else {
metrics_map.insert({line_values.front(), stod(line_values.back())});
}
pair<string, string> p_line_val { split_line_by_last(ln, ' ') };
metrics_map.insert({p_line_val.first, stod(p_line_val.second)});
}
}

Expand Down
Loading

0 comments on commit a52fd0b

Please sign in to comment.