A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/grafana/mcp-grafana/commit/78e604e723620270cec3c63121ab478d1070f5f6 below:

Check if datasource exists before querying (#84) · grafana/mcp-grafana@78e604e · GitHub

File tree Expand file treeCollapse file tree 4 files changed

+27

-0

lines changed

Filter options

Expand file treeCollapse file tree 4 files changed

+27

-0

lines changed Original file line number Diff line number Diff line change

@@ -77,6 +77,10 @@ func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*mo

77 77

c := mcpgrafana.GrafanaClientFromContext(ctx)

78 78

datasource, err := c.Datasources.GetDataSourceByUID(args.UID)

79 79

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 +

}

80 84

return nil, fmt.Errorf("get datasource by uid %s: %w", args.UID, err)

81 85

}

82 86

return datasource.Payload, nil

Original file line number Diff line number Diff line change

@@ -74,6 +74,16 @@ func TestDatasourcesTools(t *testing.T) {

74 74

assert.Equal(t, "Prometheus", result.Name)

75 75

})

76 76 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 + 77 87

t.Run("get datasource by name", func(t *testing.T) {

78 88

ctx := newTestContext()

79 89

result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{

Original file line number Diff line number Diff line change

@@ -44,6 +44,12 @@ type Stats struct {

44 44

}

45 45 46 46

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 + 47 53

grafanaURL, apiKey := mcpgrafana.GrafanaURLFromContext(ctx), mcpgrafana.GrafanaAPIKeyFromContext(ctx)

48 54

url := fmt.Sprintf("%s/api/datasources/proxy/uid/%s", strings.TrimRight(grafanaURL, "/"), uid)

49 55 Original file line number Diff line number Diff line change

@@ -27,6 +27,12 @@ var (

27 27

)

28 28 29 29

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 + 30 36

grafanaURL, apiKey := mcpgrafana.GrafanaURLFromContext(ctx), mcpgrafana.GrafanaAPIKeyFromContext(ctx)

31 37

url := fmt.Sprintf("%s/api/datasources/proxy/uid/%s", strings.TrimRight(grafanaURL, "/"), uid)

32 38

rt := api.DefaultRoundTripper

@@ -42,6 +48,7 @@ func promClientFromContext(ctx context.Context, uid string) (promv1.API, error)

42 48

if err != nil {

43 49

return nil, fmt.Errorf("creating Prometheus client: %w", err)

44 50

}

51 + 45 52

return promv1.NewAPI(c), nil

46 53

}

47 54

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4