Skip to content

Commit 60c5c8b

Browse files
committed
WIP - prometheus tests
1 parent a4e0f2b commit 60c5c8b

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

docker-compose.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ services:
1212
- ./tests/provisioning:/etc/grafana/provisioning
1313
- ./tests/dashboards:/var/lib/grafana/dashboards
1414

15+
prometheus:
16+
image: prom/prometheus
17+
ports:
18+
- "9090:9090"
19+
command:
20+
- --config.file=/etc/prometheus/prometheus.yml
21+
1522
volumes:
1623
var-lib-grafana:

mcpgrafana.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
)
1515

1616
const (
17+
defaultGrafanaHost = "localhost:3000"
18+
defaultGrafanaURL = "http://" + defaultGrafanaHost
19+
1720
grafanaURLEnvVar = "GRAFANA_URL"
1821
grafanaAPIEnvVar = "GRAFANA_API_KEY"
1922

@@ -62,12 +65,18 @@ func WithGrafanaAPIKey(ctx context.Context, apiKey string) context.Context {
6265

6366
// GrafanaURLFromContext extracts the Grafana URL from the context.
6467
func GrafanaURLFromContext(ctx context.Context) string {
65-
return ctx.Value(grafanaURLKey{}).(string)
68+
if u, ok := ctx.Value(grafanaURLKey{}).(string); ok {
69+
return u
70+
}
71+
return defaultGrafanaURL
6672
}
6773

6874
// GrafanaAPIKeyFromContext extracts the Grafana API key from the context.
6975
func GrafanaAPIKeyFromContext(ctx context.Context) string {
70-
return ctx.Value(grafanaAPIKeyKey{}).(string)
76+
if k, ok := ctx.Value(grafanaAPIKeyKey{}).(string); ok {
77+
return k
78+
}
79+
return ""
7180
}
7281

7382
type grafanaClientKey struct{}

tests/prometheus.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
global:
2+
scrape_interval: 1s
3+
4+
scrape_configs:
5+
- job_name: 'prometheus'
6+
static_configs:
7+
- targets: ['localhost:9090']
8+

tools/prometheus.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type ListPrometheusMetricMetadataParams struct {
4242
Metric string `json:"metric" jsonschema:"description=The metric to query"`
4343
}
4444

45-
func listPrometheusMetricMetadata(ctx context.Context, args ListPrometheusMetricMetadataParams) (*mcp.CallToolResult, error) {
45+
func listPrometheusMetricMetadata(ctx context.Context, args ListPrometheusMetricMetadataParams) (map[string][]promv1.Metadata, error) {
4646
promClient, err := promClientFromContext(ctx, args.DatasourceUID)
4747
if err != nil {
4848
return nil, fmt.Errorf("getting Prometheus client: %w", err)
@@ -57,11 +57,7 @@ func listPrometheusMetricMetadata(ctx context.Context, args ListPrometheusMetric
5757
if err != nil {
5858
return nil, fmt.Errorf("listing Prometheus metric metadata: %w", err)
5959
}
60-
b, err := json.Marshal(metadata)
61-
if err != nil {
62-
return nil, fmt.Errorf("marshalling Prometheus metric metadata: %w", err)
63-
}
64-
return mcp.NewToolResultText(string(b)), nil
60+
return metadata, nil
6561
}
6662

6763
var ListPrometheusMetricMetadata = mcpgrafana.MustTool(

tools/prometheus_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tools
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestPrometheusTools(t *testing.T) {
11+
t.Run("list prometheus metric metadata", func(t *testing.T) {
12+
ctx := newTestContext()
13+
result, err := listPrometheusMetricMetadata(ctx, ListPrometheusMetricMetadataParams{
14+
DatasourceUID: "prometheus",
15+
})
16+
require.NoError(t, err)
17+
assert.Len(t, result, 10)
18+
})
19+
}

0 commit comments

Comments
 (0)