-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix test_prometheus_metrics-t to handle Prometheus labels #5380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1855,7 +1855,13 @@ map<string, double> parse_prometheus_metrics(const string& s) { | |
| for (const string ln : lines) { | ||
| if (ln.empty() == false && ln[0] != '#') { | ||
| pair<string, string> p_line_val { split_line_by_last(ln, ' ') }; | ||
| metrics_map.insert({p_line_val.first, stod(p_line_val.second)}); | ||
| // Strip labels from metric name (e.g., "metric{label=\"value\"}" -> "metric") | ||
| string metric_name = p_line_val.first; | ||
| size_t brace_pos = metric_name.find('{'); | ||
| if (brace_pos != string::npos) { | ||
| metric_name = metric_name.substr(0, brace_pos); | ||
| } | ||
| metrics_map.insert({metric_name, stod(p_line_val.second)}); | ||
|
Comment on lines
+1858
to
+1864
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find where parse_prometheus_metrics is called
rg -n 'parse_prometheus_metrics' --type=cpp -B2 -A2Repository: sysown/proxysql Length of output: 1737 🏁 Script executed: # Find test files that might consume this function
fd -e cpp -e hpp | head -20Repository: sysown/proxysql Length of output: 640 🏁 Script executed: # Look for Prometheus metrics output in tests
rg -n 'prometheus\|metrics_map' --type=cpp -B2 -A2 | head -100Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look at test_prometheus_metrics-t.cpp to see how metrics_vals is used
cat test/tap/tests/test_prometheus_metrics-t.cpp | head -100Repository: sysown/proxysql Length of output: 2862 🏁 Script executed: # Look at reg_test_4556-ssl_error_queue-t.cpp to see how m_id_val is used
rg -n 'const map.*m_id_val|m_id_val\[' test/tap/tests/reg_test_4556-ssl_error_queue-t.cpp -A3Repository: sysown/proxysql Length of output: 261 🏁 Script executed: # Search for any test data or fixtures that contain Prometheus metrics format
rg -n 'metric{|protocol=|label=' test/tap/tests/ --type=cpp -C2Repository: sysown/proxysql Length of output: 172 🏁 Script executed: # Search for Prometheus metrics with labels in test files
rg 'proxysql_.*{' test/tap/tests/ --type=cpp -C2Repository: sysown/proxysql Length of output: 146 🏁 Script executed: # Look at what the Prometheus output looks like - search for "# HELP\|# TYPE"
rg 'HELP|TYPE' test/tap/tests/ --type=cpp -C2 | head -50Repository: sysown/proxysql Length of output: 3686 🏁 Script executed: # Check the actual usage of metrics_vals after parsing
rg -n 'metrics_vals\.find\|metrics_vals\[' test/tap/tests/test_prometheus_metrics-t.cpp -A2 -B2Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look for any fixtures or test data files that might have prometheus output
find test/tap -name "*prometheus*" -o -name "*metrics*" | grep -v '\\.o$'Repository: sysown/proxysql Length of output: 273 🏁 Script executed: # Look at parse_prometheus_metrics implementation completely
sed -n '1851,1870p' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 711 🏁 Script executed: # Look for how metrics are looked up - they search for base metric names
rg 'proxysql_' test/tap/tests/test_prometheus_metrics-t.cpp | head -20Repository: sysown/proxysql Length of output: 1671 🏁 Script executed: # Look at the disabled test which might have test data
head -150 test/tap/tests/test_admin_prometheus_metrics_dump-t_disabled_gh3571.cppRepository: sysown/proxysql Length of output: 2674 🏁 Script executed: # Search for actual metric values in the test to understand structure
rg -n 'find\(' test/tap/tests/test_prometheus_metrics-t.cpp -A1 -B1 | head -40Repository: sysown/proxysql Length of output: 1787 🏁 Script executed: # Check if extract_metric_tags is called and used with the returned map from parse_prometheus_metrics
rg -n 'extract_metric_tags\(' test/tap/tests/test_prometheus_metrics-t.cpp -A2 -B2Repository: sysown/proxysql Length of output: 1165 🏁 Script executed: # Look at the full extract_metric_tags function
rg -n 'extract_metric_tags' test/tap/tap/utils.cpp -A15Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Check what the actual Prometheus output format is - look at the comment/example
rg -n 'HELP\|# TYPE' test/tap/tap/utils.cpp -B2 -A2Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look at the complete extract_metric_tags function to see what it returns when labels are missing
sed -n '277,310p' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 1012 🏁 Script executed: # Check what happens when extract_metric_tags finds no labels - does it return empty or what?
rg -n 'extract_metric_tags' test/tap/tap/utils.cpp -A20 | head -40Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look at the specific line where metric_tags.at("hostgroup") is called
sed -n '360,370p' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 489 🏁 Script executed: # Look at the complete extract_metric_tags implementation to confirm what it returns
sed -n '277,300p' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 791 🏁 Script executed: # Check if there's any initialization or default behavior after find returns npos
sed -n '275,310p' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 1096 🏁 Script executed: # Use ast-grep to find the extract_metric_tags function definition
ast-grep --pattern $'map<string,string> extract_metric_tags($_) {
$$$
}'Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look at utils.cpp around line 277 where extract_metric_tags should be
head -320 test/tap/tap/utils.cpp | tail -50Repository: sysown/proxysql Length of output: 1546 🏁 Script executed: # Search for the exact location of extract_metric_tags function
rg -n 'map<string,string> extract_metric_tags' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 41 🏁 Script executed: # Once we know the line number, read that section
rg -n 'map<string,string> extract_metric_tags' test/tap/tap/utils.cpp -A25Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Search for extract_metric_tags without the full signature
rg -n 'extract_metric_tags' test/tap/tap/utils.cpp | head -5Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Look at the line numbers from earlier results - they showed line 277 has it
sed -n '277,320p' test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 1362 🏁 Script executed: # Try searching in the header file
rg -n 'extract_metric_tags' test/tap/tap/utils.h -A10Repository: sysown/proxysql Length of output: 41 🏁 Script executed: # Search for it anywhere in the tap directory
find test/tap -name "*.cpp" -o -name "*.h" | xargs grep -l 'extract_metric_tags'Repository: sysown/proxysql Length of output: 103 🏁 Script executed: # Get file size and search more carefully
wc -l test/tap/tap/utils.cppRepository: sysown/proxysql Length of output: 86 🏁 Script executed: # Search for extract_metric_tags in the test file
rg -n 'extract_metric_tags' test/tap/tests/test_prometheus_metrics-t.cpp -B2 -A15Repository: sysown/proxysql Length of output: 2974 🏁 Script executed: # Also check if extract_metric_tags is defined in the test file or just used
grep -n 'extract_metric_tags' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 373 🏁 Script executed: # Look for the function definition in test_prometheus_metrics-t.cpp
sed -n '250,330p' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 2511 🏁 Script executed: # Confirm the call to metric_tags.at("hostgroup") on line 363
# If extract_metric_tags returns empty map (because labels are stripped),
# .at("hostgroup") will throw std::out_of_range exception
sed -n '360,365p' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 330 🏁 Script executed: # Check if proxysql_connpool_conns metric actually has hostgroup labels in Prometheus output
# Look for any documentation or comments about metric format
rg -n 'proxysql_connpool_conns' test/tap/ -C3 | head -40Repository: sysown/proxysql Length of output: 3590 🏁 Script executed: # Check the git log to see what the original code did before the labels were stripped
git log --oneline test/tap/tap/utils.cpp | head -5Repository: sysown/proxysql Length of output: 125 🏁 Script executed: # Look at the commit that mentions handling Prometheus labels
git show 195bbfb5 --statRepository: sysown/proxysql Length of output: 50371 🏁 Script executed: # Check what changed in that commit for parse_prometheus_metrics
git show 195bbfb5 test/tap/tap/utils.cpp | head -80Repository: sysown/proxysql Length of output: 2745 🏁 Script executed: # Verify that extract_metric_tags is actually called on the parsed metrics
# and will fail when labels are stripped
grep -n 'extract_metric_tags' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 373 🏁 Script executed: # Look at what extract_metric_tags returns when it doesn't find '{'
sed -n '277,290p' test/tap/tests/test_prometheus_metrics-t.cppRepository: sysown/proxysql Length of output: 560 This change breaks test assertions by stripping labels that
The same issue occurs at lines 466 and 640 where other tests call Additionally, if any Prometheus metric is emitted with multiple label variants (e.g., The label-stripping approach conflicts with the test expectations. Consider either preserving the full metric names with labels in the returned map or refactoring the tests to work without label information. 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to strip labels from the metric name can be made more concise. You can combine
std::string::substrwithstd::string::findin a single expression. Whenfinddoesn't locate the character, it returnsstd::string::npos, andsubstr(0, std::string::npos)correctly returns a copy of the entire string. This simplifies the code by removing the conditional logic and intermediate variables.