Skip to content

Commit 4f6a352

Browse files
committed
Check metric names
1 parent 6a098e2 commit 4f6a352

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

model/model.go

+4
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,7 @@ type GetAttributeKeysResponse struct {
514514
// The attribute values
515515
Attributes []string `json:"attributes"`
516516
}
517+
518+
type GetMetricNamesResponse struct {
519+
MetricNames []string `json:"metrics"`
520+
}

tools/get_attribute_keys.go

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
mcpgolang "github.com/metoro-io/mcp-golang"
99
"github.com/metoro-io/metoro-mcp-server/model"
1010
"github.com/metoro-io/metoro-mcp-server/utils"
11+
"slices"
12+
"strings"
13+
"time"
1114
)
1215

1316
type GetAttributeKeysHandlerArgs struct {
@@ -26,6 +29,12 @@ func GetAttributeKeysHandler(ctx context.Context, arguments GetAttributeKeysHand
2629
if arguments.MetricName == "" {
2730
return nil, fmt.Errorf("metricName is required when type is 'metric'")
2831
}
32+
33+
// Check whether the metric is valid. If not, return an error.
34+
err = CheckMetric(ctx, arguments.MetricName)
35+
if err != nil {
36+
return nil, err
37+
}
2938
}
3039

3140
metricAttr := model.GetMetricAttributesRequest{
@@ -52,3 +61,27 @@ func GetAttributeKeysHandler(ctx context.Context, arguments GetAttributeKeysHand
5261

5362
return mcpgolang.NewToolResponse(mcpgolang.NewTextContent(fmt.Sprintf("%s", string(resp)))), nil
5463
}
64+
65+
func CheckMetric(ctx context.Context, metricName string) error {
66+
now := time.Now()
67+
hourAgo := now.Add(-30 * time.Minute)
68+
request := model.FuzzyMetricsRequest{
69+
StartTime: hourAgo.Unix(),
70+
EndTime: now.Unix(),
71+
MetricFuzzyMatch: "", // This will return all the metric names.
72+
}
73+
metricNamesResp, err := getMetricNamesMetoroCall(ctx, request)
74+
75+
metricNames := model.GetMetricNamesResponse{}
76+
err = json.Unmarshal(metricNamesResp, &metricNames)
77+
if err != nil {
78+
return fmt.Errorf("error unmarshaling response: %v", err)
79+
}
80+
81+
metricNamesStr := strings.Join(metricNames.MetricNames, ", ")
82+
if !slices.Contains(metricNames.MetricNames, metricName) {
83+
return fmt.Errorf("metricName '%s' is not valid. Valid metric names are: %s", metricName, metricNamesStr)
84+
}
85+
86+
return nil
87+
}

tools/get_multi_metric.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func GetMultiMetricHandler(ctx context.Context, arguments GetMultiMetricHandlerA
4242
return nil, fmt.Errorf("error calculating time range: %v", err)
4343
}
4444

45-
err = checkTimeseriesAttributes(ctx, arguments.Timeseries, startTime, endTime)
45+
err = checkTimeseries(ctx, arguments.Timeseries, startTime, endTime)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -188,9 +188,13 @@ func CheckAttributes(ctx context.Context, requestType model.MetricType, filters
188188
return nil
189189
}
190190

191-
func checkTimeseriesAttributes(ctx context.Context, timeseries []SingleMetricRequest, startTime, endTime int64) error {
191+
func checkTimeseries(ctx context.Context, timeseries []SingleMetricRequest, startTime, endTime int64) error {
192192
for _, ts := range timeseries {
193-
err := CheckAttributes(ctx, ts.Type, ts.Filters, ts.ExcludeFilters, ts.Splits, &model.GetMetricAttributesRequest{
193+
err := CheckMetric(ctx, ts.MetricName)
194+
if err != nil {
195+
return err
196+
}
197+
err = CheckAttributes(ctx, ts.Type, ts.Filters, ts.ExcludeFilters, ts.Splits, &model.GetMetricAttributesRequest{
194198
StartTime: startTime,
195199
EndTime: endTime,
196200
MetricName: ts.MetricName,

0 commit comments

Comments
 (0)