Skip to content

Commit 78e604e

Browse files
authored
Check if datasource exists before querying (#84)
1 parent a18a21c commit 78e604e

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

tools/datasources.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*mo
7777
c := mcpgrafana.GrafanaClientFromContext(ctx)
7878
datasource, err := c.Datasources.GetDataSourceByUID(args.UID)
7979
if err != nil {
80+
// Check if it's a 404 Not Found Error
81+
if strings.Contains(err.Error(), "404") {
82+
return nil, fmt.Errorf("datasource with UID '%s' not found. Please check if the datasource exists and is accessible", args.UID)
83+
}
8084
return nil, fmt.Errorf("get datasource by uid %s: %w", args.UID, err)
8185
}
8286
return datasource.Payload, nil

tools/datasources_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ func TestDatasourcesTools(t *testing.T) {
7474
assert.Equal(t, "Prometheus", result.Name)
7575
})
7676

77+
t.Run("get datasource by uid - not found", func(t *testing.T) {
78+
ctx := newTestContext()
79+
result, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{
80+
UID: "non-existent-datasource",
81+
})
82+
require.Error(t, err)
83+
require.Nil(t, result)
84+
assert.Contains(t, err.Error(), "not found")
85+
})
86+
7787
t.Run("get datasource by name", func(t *testing.T) {
7888
ctx := newTestContext()
7989
result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{

tools/loki.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ type Stats struct {
4444
}
4545

4646
func newLokiClient(ctx context.Context, uid string) (*Client, error) {
47+
// First check if the datasource exists
48+
_, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{UID: uid})
49+
if err != nil {
50+
return nil, err
51+
}
52+
4753
grafanaURL, apiKey := mcpgrafana.GrafanaURLFromContext(ctx), mcpgrafana.GrafanaAPIKeyFromContext(ctx)
4854
url := fmt.Sprintf("%s/api/datasources/proxy/uid/%s", strings.TrimRight(grafanaURL, "/"), uid)
4955

tools/prometheus.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ var (
2727
)
2828

2929
func promClientFromContext(ctx context.Context, uid string) (promv1.API, error) {
30+
// First check if the datasource exists
31+
_, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{UID: uid})
32+
if err != nil {
33+
return nil, err
34+
}
35+
3036
grafanaURL, apiKey := mcpgrafana.GrafanaURLFromContext(ctx), mcpgrafana.GrafanaAPIKeyFromContext(ctx)
3137
url := fmt.Sprintf("%s/api/datasources/proxy/uid/%s", strings.TrimRight(grafanaURL, "/"), uid)
3238
rt := api.DefaultRoundTripper
@@ -42,6 +48,7 @@ func promClientFromContext(ctx context.Context, uid string) (promv1.API, error)
4248
if err != nil {
4349
return nil, fmt.Errorf("creating Prometheus client: %w", err)
4450
}
51+
4552
return promv1.NewAPI(c), nil
4653
}
4754

0 commit comments

Comments
 (0)