Skip to content

Commit 2b2b90e

Browse files
committed
Handle empty matcher
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
1 parent f1222d1 commit 2b2b90e

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pkg/prometheus/loader.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ func (p *RealLoader) WithGuardrails(g *Guardrails) *RealLoader {
5555
}
5656

5757
func (p *RealLoader) ListMetrics(ctx context.Context, nameRegex string) ([]string, error) {
58-
// Create a matcher for the __name__ label with the provided regex
59-
matcher := fmt.Sprintf("{__name__=~\"%q\"}", nameRegex)
60-
labelValues, _, err := p.client.LabelValues(ctx, "__name__", []string{matcher}, time.Now().Add(-ListMetricsTimeRange), time.Now())
58+
var matches []string
59+
60+
// For blanket regex patterns like ".*", use empty matcher to get all metrics to not get 4xx.
61+
if nameRegex != ".*" && nameRegex != ".+" && nameRegex != "" {
62+
matcher := fmt.Sprintf("{__name__=~\"%q\"}", nameRegex)
63+
matches = []string{matcher}
64+
}
65+
66+
labelValues, _, err := p.client.LabelValues(ctx, "__name__", matches, time.Now().Add(-ListMetricsTimeRange), time.Now())
6167
if err != nil {
6268
return nil, fmt.Errorf("error fetching metric names: %w", err)
6369
}

tests/e2e/e2e_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,33 @@ func TestHealthEndpoint(t *testing.T) {
6666
}
6767
}
6868

69-
func TestListMetrics(t *testing.T) {
70-
resp, err := mcpClient.CallTool(t, 1, "list_metrics", map[string]any{
69+
func TestListMetricsReturnsKnownMetrics(t *testing.T) {
70+
resp, err := mcpClient.CallTool(t, 2, "list_metrics", map[string]any{
7171
"name_regex": ".*",
7272
})
7373
if err != nil {
7474
t.Fatalf("Failed to call list_metrics: %v", err)
7575
}
7676

7777
if resp.Error != nil {
78-
t.Errorf("MCP error: %s", resp.Error.Message)
78+
t.Fatalf("MCP error: %s", resp.Error.Message)
7979
}
8080

81-
if resp.Result == nil {
82-
t.Error("Expected result, got nil")
83-
}
81+
// Verify known metrics from kube-prometheus are present
82+
resultJSON, _ := json.Marshal(resp.Result)
83+
resultStr := string(resultJSON)
8484

85-
t.Logf("list_metrics returned successfully")
85+
expectedMetrics := []string{"up", "prometheus_build_info"}
86+
for _, metric := range expectedMetrics {
87+
if !strings.Contains(resultStr, metric) {
88+
t.Errorf("Expected metric %q not found in results", metric)
89+
}
90+
}
8691
}
8792

88-
func TestListMetricsReturnsKnownMetrics(t *testing.T) {
93+
func TestListMetricsReturnsKnownMetricsWithMatcher(t *testing.T) {
8994
resp, err := mcpClient.CallTool(t, 2, "list_metrics", map[string]any{
90-
"name_regex": ".*",
95+
"name_regex": "prometheus.*",
9196
})
9297
if err != nil {
9398
t.Fatalf("Failed to call list_metrics: %v", err)
@@ -101,7 +106,7 @@ func TestListMetricsReturnsKnownMetrics(t *testing.T) {
101106
resultJSON, _ := json.Marshal(resp.Result)
102107
resultStr := string(resultJSON)
103108

104-
expectedMetrics := []string{"up", "prometheus_build_info"}
109+
expectedMetrics := []string{"prometheus_build_info"}
105110
for _, metric := range expectedMetrics {
106111
if !strings.Contains(resultStr, metric) {
107112
t.Errorf("Expected metric %q not found in results", metric)

0 commit comments

Comments
 (0)