forked from grafana/mcp-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpgrafana_test.go
More file actions
933 lines (778 loc) · 30.3 KB
/
mcpgrafana_test.go
File metadata and controls
933 lines (778 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//go:build unit
// +build unit
package mcpgrafana
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-openapi/runtime/client"
grafana_client "github.com/grafana/grafana-openapi-client-go/client"
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)
func TestExtractIncidentClientFromEnv(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/")
ctx := ExtractIncidentClientFromEnv(context.Background())
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/", client.RemoteHost)
}
func TestExtractIncidentClientFromHeaders(t *testing.T) {
t.Run("no headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractIncidentClientFromHeaders(context.Background(), req)
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://localhost:3000/api/plugins/grafana-irm-app/resources/api/v1/", client.RemoteHost)
})
t.Run("no headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractIncidentClientFromHeaders(context.Background(), req)
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/", client.RemoteHost)
})
t.Run("with headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
require.NoError(t, err)
ctx := ExtractIncidentClientFromHeaders(context.Background(), req)
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/", client.RemoteHost)
})
t.Run("with headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "will-not-be-used")
req, err := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
require.NoError(t, err)
ctx := ExtractIncidentClientFromHeaders(context.Background(), req)
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://my-test-url.grafana.com/api/plugins/grafana-irm-app/resources/api/v1/", client.RemoteHost)
})
}
func TestExtractGrafanaInfoFromHeaders(t *testing.T) {
t.Run("no headers, no env", func(t *testing.T) {
// Explicitly clear environment variables to ensure test isolation
t.Setenv("GRAFANA_URL", "")
t.Setenv("GRAFANA_API_KEY", "")
t.Setenv("GRAFANA_SERVICE_ACCOUNT_TOKEN", "")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, defaultGrafanaURL, config.URL)
assert.Equal(t, "", config.APIKey)
assert.Nil(t, config.BasicAuth)
})
t.Run("no headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com")
t.Setenv("GRAFANA_API_KEY", "my-test-api-key")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", config.URL)
assert.Equal(t, "my-test-api-key", config.APIKey)
})
t.Run("no headers, with service account token", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com")
t.Setenv("GRAFANA_SERVICE_ACCOUNT_TOKEN", "my-service-account-token")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", config.URL)
assert.Equal(t, "my-service-account-token", config.APIKey)
})
t.Run("no headers, service account token takes precedence over api key", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com")
t.Setenv("GRAFANA_API_KEY", "my-deprecated-api-key")
t.Setenv("GRAFANA_SERVICE_ACCOUNT_TOKEN", "my-service-account-token")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", config.URL)
assert.Equal(t, "my-service-account-token", config.APIKey)
})
t.Run("with headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
req.Header.Set(grafanaAPIKeyHeader, "my-test-api-key")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", config.URL)
assert.Equal(t, "my-test-api-key", config.APIKey)
})
t.Run("with headers, with env", func(t *testing.T) {
// Env vars should be ignored if headers are present.
t.Setenv("GRAFANA_URL", "will-not-be-used")
t.Setenv("GRAFANA_API_KEY", "will-not-be-used")
t.Setenv("GRAFANA_SERVICE_ACCOUNT_TOKEN", "will-not-be-used")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
req.Header.Set(grafanaAPIKeyHeader, "my-test-api-key")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", config.URL)
assert.Equal(t, "my-test-api-key", config.APIKey)
})
t.Run("no headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_USERNAME", "foo")
t.Setenv("GRAFANA_PASSWORD", "bar")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "foo", config.BasicAuth.Username())
password, _ := config.BasicAuth.Password()
assert.Equal(t, "bar", password)
})
t.Run("user auth with headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
req.SetBasicAuth("foo", "bar")
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "foo", config.BasicAuth.Username())
password, _ := config.BasicAuth.Password()
assert.Equal(t, "bar", password)
})
t.Run("user auth with headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_USERNAME", "will-not-be-used")
t.Setenv("GRAFANA_PASSWORD", "will-not-be-used")
req, err := http.NewRequest("GET", "http://example.com", nil)
req.SetBasicAuth("foo", "bar")
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, "foo", config.BasicAuth.Username())
password, _ := config.BasicAuth.Password()
assert.Equal(t, "bar", password)
})
t.Run("orgID from env", func(t *testing.T) {
t.Setenv("GRAFANA_ORG_ID", "123")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, int64(123), config.OrgID)
})
t.Run("orgID from header", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set("X-Grafana-Org-Id", "456")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, int64(456), config.OrgID)
})
t.Run("orgID header takes precedence over env", func(t *testing.T) {
t.Setenv("GRAFANA_ORG_ID", "123")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set("X-Grafana-Org-Id", "456")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, int64(456), config.OrgID)
})
t.Run("invalid orgID from env ignored", func(t *testing.T) {
t.Setenv("GRAFANA_ORG_ID", "not-a-number")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, int64(0), config.OrgID)
})
t.Run("invalid orgID from header ignored", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set("X-Grafana-Org-Id", "invalid")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, int64(0), config.OrgID)
})
}
func TestExtractGrafanaClientPath(t *testing.T) {
t.Run("no custom path", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/")
ctx := ExtractGrafanaClientFromEnv(context.Background())
c := GrafanaClientFromContext(ctx)
require.NotNil(t, c)
rt := c.Transport.(*client.Runtime)
assert.Equal(t, "/api", rt.BasePath)
})
t.Run("custom path", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/grafana")
ctx := ExtractGrafanaClientFromEnv(context.Background())
c := GrafanaClientFromContext(ctx)
require.NotNil(t, c)
rt := c.Transport.(*client.Runtime)
assert.Equal(t, "/grafana/api", rt.BasePath)
})
t.Run("custom path, trailing slash", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/grafana/")
ctx := ExtractGrafanaClientFromEnv(context.Background())
c := GrafanaClientFromContext(ctx)
require.NotNil(t, c)
rt := c.Transport.(*client.Runtime)
assert.Equal(t, "/grafana/api", rt.BasePath)
})
}
// minURL is a helper struct representing what we can extract from a constructed
// Grafana client.
type minURL struct {
host, basePath string
}
// minURLFromClient extracts some minimal amount of URL info from a Grafana client.
func minURLFromClient(c *grafana_client.GrafanaHTTPAPI) minURL {
rt := c.Transport.(*client.Runtime)
return minURL{rt.Host, rt.BasePath}
}
func TestExtractGrafanaClientFromHeaders(t *testing.T) {
t.Run("no headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaClientFromHeaders(context.Background(), req)
c := GrafanaClientFromContext(ctx)
url := minURLFromClient(c)
assert.Equal(t, "localhost:3000", url.host)
assert.Equal(t, "/api", url.basePath)
})
t.Run("no headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaClientFromHeaders(context.Background(), req)
c := GrafanaClientFromContext(ctx)
url := minURLFromClient(c)
assert.Equal(t, "my-test-url.grafana.com", url.host)
assert.Equal(t, "/api", url.basePath)
})
t.Run("with headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
ctx := ExtractGrafanaClientFromHeaders(context.Background(), req)
c := GrafanaClientFromContext(ctx)
url := minURLFromClient(c)
assert.Equal(t, "my-test-url.grafana.com", url.host)
assert.Equal(t, "/api", url.basePath)
})
t.Run("with headers, with env", func(t *testing.T) {
// Env vars should be ignored if headers are present.
t.Setenv("GRAFANA_URL", "will-not-be-used")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
ctx := ExtractGrafanaClientFromHeaders(context.Background(), req)
c := GrafanaClientFromContext(ctx)
url := minURLFromClient(c)
assert.Equal(t, "my-test-url.grafana.com", url.host)
assert.Equal(t, "/api", url.basePath)
})
}
func TestToolTracingInstrumentation(t *testing.T) {
// Set up in-memory span recorder
spanRecorder := tracetest.NewSpanRecorder()
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(spanRecorder),
)
originalProvider := otel.GetTracerProvider()
otel.SetTracerProvider(tracerProvider)
defer otel.SetTracerProvider(originalProvider) // Restore original provider
t.Run("successful tool execution creates span with correct attributes", func(t *testing.T) {
// Clear any previous spans
spanRecorder.Reset()
// Define a simple test tool
type TestParams struct {
Message string `json:"message" jsonschema:"description=Test message"`
}
testHandler := func(ctx context.Context, args TestParams) (string, error) {
return "Hello " + args.Message, nil
}
// Create tool using MustTool (this applies our instrumentation)
tool := MustTool("test_tool", "A test tool for tracing", testHandler)
// Create context with argument logging enabled
config := GrafanaConfig{
IncludeArgumentsInSpans: true,
}
ctx := WithGrafanaConfig(context.Background(), config)
// Create a mock MCP request
request := mcp.CallToolRequest{
Params: struct {
Name string `json:"name"`
Arguments any `json:"arguments,omitempty"`
Meta *mcp.Meta `json:"_meta,omitempty"`
}{
Name: "test_tool",
Arguments: map[string]interface{}{
"message": "world",
},
},
}
// Execute the tool
result, err := tool.Handler(ctx, request)
require.NoError(t, err)
require.NotNil(t, result)
// Verify span was created
spans := spanRecorder.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "tools/call test_tool", span.Name())
assert.Equal(t, codes.Ok, span.Status().Code)
// Check semconv attributes
attributes := span.Attributes()
assertHasAttribute(t, attributes, "gen_ai.tool.name", "test_tool")
assertHasAttribute(t, attributes, "mcp.method.name", "tools/call")
assertHasAttribute(t, attributes, "gen_ai.tool.call.arguments", `{"message":"world"}`)
})
t.Run("tool execution error records error on span", func(t *testing.T) {
// Clear any previous spans
spanRecorder.Reset()
// Define a test tool that returns an error
type TestParams struct {
ShouldFail bool `json:"shouldFail" jsonschema:"description=Whether to fail"`
}
testHandler := func(ctx context.Context, args TestParams) (string, error) {
if args.ShouldFail {
return "", assert.AnError
}
return "success", nil
}
// Create tool
tool := MustTool("failing_tool", "A tool that can fail", testHandler)
// Create context (spans always created)
config := GrafanaConfig{}
ctx := WithGrafanaConfig(context.Background(), config)
// Create a mock MCP request that will cause failure
request := mcp.CallToolRequest{
Params: struct {
Name string `json:"name"`
Arguments any `json:"arguments,omitempty"`
Meta *mcp.Meta `json:"_meta,omitempty"`
}{
Name: "failing_tool",
Arguments: map[string]interface{}{
"shouldFail": true,
},
},
}
// Execute the tool (should fail)
result, err := tool.Handler(ctx, request)
assert.NoError(t, err)
require.NotNil(t, result)
assert.True(t, result.IsError)
// Verify span was created and marked as error
spans := spanRecorder.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "tools/call failing_tool", span.Name())
assert.Equal(t, codes.Error, span.Status().Code)
assert.Equal(t, assert.AnError.Error(), span.Status().Description)
// Verify error was recorded (check events for error record)
events := span.Events()
hasErrorEvent := false
for _, event := range events {
if event.Name == "exception" {
hasErrorEvent = true
break
}
}
assert.True(t, hasErrorEvent, "Expected error event to be recorded on span")
})
t.Run("spans always created for context propagation", func(t *testing.T) {
// Clear any previous spans
spanRecorder.Reset()
// Define a simple test tool
type TestParams struct {
Message string `json:"message" jsonschema:"description=Test message"`
}
testHandler := func(ctx context.Context, args TestParams) (string, error) {
return "processed", nil
}
// Create tool
tool := MustTool("context_prop_tool", "A tool for context propagation", testHandler)
// Create context with default config (no special flags)
config := GrafanaConfig{}
ctx := WithGrafanaConfig(context.Background(), config)
// Create a mock MCP request
request := mcp.CallToolRequest{
Params: struct {
Name string `json:"name"`
Arguments any `json:"arguments,omitempty"`
Meta *mcp.Meta `json:"_meta,omitempty"`
}{
Name: "context_prop_tool",
Arguments: map[string]interface{}{
"message": "test",
},
},
}
// Execute the tool (should always create spans for context propagation)
result, err := tool.Handler(ctx, request)
require.NoError(t, err)
require.NotNil(t, result)
// Verify spans ARE always created
spans := spanRecorder.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "tools/call context_prop_tool", span.Name())
assert.Equal(t, codes.Ok, span.Status().Code)
})
t.Run("arguments not logged by default (PII safety)", func(t *testing.T) {
// Clear any previous spans
spanRecorder.Reset()
// Define a simple test tool
type TestParams struct {
SensitiveData string `json:"sensitiveData" jsonschema:"description=Potentially sensitive data"`
}
testHandler := func(ctx context.Context, args TestParams) (string, error) {
return "processed", nil
}
// Create tool
tool := MustTool("sensitive_tool", "A tool with sensitive data", testHandler)
// Create context with argument logging disabled (default)
config := GrafanaConfig{
IncludeArgumentsInSpans: false, // Default: safe
}
ctx := WithGrafanaConfig(context.Background(), config)
// Create a mock MCP request with potentially sensitive data
request := mcp.CallToolRequest{
Params: struct {
Name string `json:"name"`
Arguments any `json:"arguments,omitempty"`
Meta *mcp.Meta `json:"_meta,omitempty"`
}{
Name: "sensitive_tool",
Arguments: map[string]interface{}{
"sensitiveData": "user@example.com",
},
},
}
// Execute the tool (arguments should NOT be logged by default)
result, err := tool.Handler(ctx, request)
require.NoError(t, err)
require.NotNil(t, result)
// Verify span was created
spans := spanRecorder.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "tools/call sensitive_tool", span.Name())
assert.Equal(t, codes.Ok, span.Status().Code)
// Check that arguments are NOT logged (PII safety)
attributes := span.Attributes()
assertHasAttribute(t, attributes, "gen_ai.tool.name", "sensitive_tool")
assertHasAttribute(t, attributes, "mcp.method.name", "tools/call")
// Verify arguments are NOT present
for _, attr := range attributes {
assert.NotEqual(t, "gen_ai.tool.call.arguments", string(attr.Key), "Arguments should not be logged by default for PII safety")
}
})
t.Run("arguments logged when argument logging enabled", func(t *testing.T) {
// Clear any previous spans
spanRecorder.Reset()
// Define a simple test tool
type TestParams struct {
SafeData string `json:"safeData" jsonschema:"description=Non-sensitive data"`
}
testHandler := func(ctx context.Context, args TestParams) (string, error) {
return "processed", nil
}
// Create tool
tool := MustTool("debug_tool", "A tool for debugging", testHandler)
// Create context with argument logging enabled
config := GrafanaConfig{
IncludeArgumentsInSpans: true,
}
ctx := WithGrafanaConfig(context.Background(), config)
// Create a mock MCP request
request := mcp.CallToolRequest{
Params: struct {
Name string `json:"name"`
Arguments any `json:"arguments,omitempty"`
Meta *mcp.Meta `json:"_meta,omitempty"`
}{
Name: "debug_tool",
Arguments: map[string]interface{}{
"safeData": "debug-value",
},
},
}
// Execute the tool (arguments SHOULD be logged when flag enabled)
result, err := tool.Handler(ctx, request)
require.NoError(t, err)
require.NotNil(t, result)
// Verify span was created
spans := spanRecorder.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "tools/call debug_tool", span.Name())
assert.Equal(t, codes.Ok, span.Status().Code)
// Check that arguments ARE logged when flag enabled
attributes := span.Attributes()
assertHasAttribute(t, attributes, "gen_ai.tool.name", "debug_tool")
assertHasAttribute(t, attributes, "mcp.method.name", "tools/call")
assertHasAttribute(t, attributes, "gen_ai.tool.call.arguments", `{"safeData":"debug-value"}`)
})
}
func TestHTTPTracingConfiguration(t *testing.T) {
t.Run("HTTP tracing always enabled for context propagation", func(t *testing.T) {
// Create context (HTTP tracing always enabled)
config := GrafanaConfig{}
ctx := WithGrafanaConfig(context.Background(), config)
// Create Grafana client
client := NewGrafanaClient(ctx, "http://localhost:3000", "test-api-key", nil, 0)
require.NotNil(t, client)
// Verify the client was created successfully (should not panic)
assert.NotNil(t, client.Transport)
})
t.Run("tracing works gracefully without OpenTelemetry configured", func(t *testing.T) {
// No OpenTelemetry tracer provider configured
// Create context (tracing always enabled for context propagation)
config := GrafanaConfig{}
ctx := WithGrafanaConfig(context.Background(), config)
// Create Grafana client (should not panic even without OTEL configured)
client := NewGrafanaClient(ctx, "http://localhost:3000", "test-api-key", nil, 0)
require.NotNil(t, client)
// Verify the client was created successfully
assert.NotNil(t, client.Transport)
})
}
func TestExtractTraceContext(t *testing.T) {
t.Run("no meta returns original context", func(t *testing.T) {
ctx := context.Background()
request := mcp.CallToolRequest{}
result := extractTraceContext(ctx, request)
assert.Equal(t, ctx, result)
})
t.Run("empty meta returns original context", func(t *testing.T) {
ctx := context.Background()
request := mcp.CallToolRequest{}
request.Params.Meta = &mcp.Meta{}
result := extractTraceContext(ctx, request)
assert.Equal(t, ctx, result)
})
t.Run("valid traceparent extracts span context", func(t *testing.T) {
ctx := context.Background()
request := mcp.CallToolRequest{}
request.Params.Meta = &mcp.Meta{
AdditionalFields: map[string]any{
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
},
}
result := extractTraceContext(ctx, request)
// Should have extracted a span context
sc := trace.SpanContextFromContext(result)
assert.True(t, sc.IsValid())
assert.Equal(t, "4bf92f3577b34da6a3ce929d0e0e4736", sc.TraceID().String())
assert.Equal(t, "00f067aa0ba902b7", sc.SpanID().String())
})
t.Run("invalid traceparent returns context unchanged", func(t *testing.T) {
ctx := context.Background()
request := mcp.CallToolRequest{}
request.Params.Meta = &mcp.Meta{
AdditionalFields: map[string]any{
"traceparent": "not-a-valid-traceparent",
},
}
result := extractTraceContext(ctx, request)
sc := trace.SpanContextFromContext(result)
assert.False(t, sc.IsValid())
})
}
// Helper function to check if an attribute exists with expected value
func assertHasAttribute(t *testing.T, attributes []attribute.KeyValue, key string, expectedValue string) {
for _, attr := range attributes {
if string(attr.Key) == key {
assert.Equal(t, expectedValue, attr.Value.AsString())
return
}
}
t.Errorf("Expected attribute %s with value %s not found", key, expectedValue)
}
func TestExtraHeadersFromEnv(t *testing.T) {
t.Run("empty env returns nil", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", "")
headers := extraHeadersFromEnv()
assert.Nil(t, headers)
})
t.Run("valid JSON", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", `{"X-Custom-Header": "custom-value", "X-Another": "another-value"}`)
headers := extraHeadersFromEnv()
assert.Equal(t, map[string]string{
"X-Custom-Header": "custom-value",
"X-Another": "another-value",
}, headers)
})
t.Run("invalid JSON returns nil", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", "not-json")
headers := extraHeadersFromEnv()
assert.Nil(t, headers)
})
t.Run("empty object", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", "{}")
headers := extraHeadersFromEnv()
assert.Equal(t, map[string]string{}, headers)
})
}
func TestExtraHeadersRoundTripper(t *testing.T) {
t.Run("adds headers to request", func(t *testing.T) {
var capturedReq *http.Request
mockRT := &extraHeadersMockRT{
fn: func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: 200}, nil
},
}
rt := NewExtraHeadersRoundTripper(mockRT, map[string]string{
"X-Custom": "value1",
"X-Another": "value2",
})
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, "value1", capturedReq.Header.Get("X-Custom"))
assert.Equal(t, "value2", capturedReq.Header.Get("X-Another"))
})
t.Run("does not modify original request", func(t *testing.T) {
mockRT := &extraHeadersMockRT{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200}, nil
},
}
rt := NewExtraHeadersRoundTripper(mockRT, map[string]string{
"X-Custom": "value",
})
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, "", req.Header.Get("X-Custom"))
})
t.Run("nil transport uses default", func(t *testing.T) {
rt := NewExtraHeadersRoundTripper(nil, map[string]string{})
assert.NotNil(t, rt.underlying)
})
}
type capturingMockRT struct {
fn func(*http.Request) (*http.Response, error)
}
func (m *capturingMockRT) RoundTrip(req *http.Request) (*http.Response, error) {
return m.fn(req)
}
// Keep the old name as an alias for backwards compatibility in case anything references it.
type extraHeadersMockRT = capturingMockRT
func TestExtractGrafanaInfoWithExtraHeaders(t *testing.T) {
t.Run("extra headers from env in ExtractGrafanaInfoFromEnv", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", `{"X-Tenant-ID": "tenant-123"}`)
ctx := ExtractGrafanaInfoFromEnv(context.Background())
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, map[string]string{"X-Tenant-ID": "tenant-123"}, config.ExtraHeaders)
})
t.Run("extra headers from env in ExtractGrafanaInfoFromHeaders", func(t *testing.T) {
t.Setenv("GRAFANA_EXTRA_HEADERS", `{"X-Tenant-ID": "tenant-456"}`)
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
config := GrafanaConfigFromContext(ctx)
assert.Equal(t, map[string]string{"X-Tenant-ID": "tenant-456"}, config.ExtraHeaders)
})
}
func TestOrgIDRoundTripper(t *testing.T) {
t.Run("adds org ID header to request", func(t *testing.T) {
var capturedReq *http.Request
mockRT := &capturingMockRT{
fn: func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: 200}, nil
},
}
rt := NewOrgIDRoundTripper(mockRT, 123)
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, "123", capturedReq.Header.Get(grafana_client.OrgIDHeader))
})
t.Run("does not add header when org ID is zero", func(t *testing.T) {
var capturedReq *http.Request
mockRT := &capturingMockRT{
fn: func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: 200}, nil
},
}
rt := NewOrgIDRoundTripper(mockRT, 0)
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Empty(t, capturedReq.Header.Get(grafana_client.OrgIDHeader))
})
t.Run("does not modify original request", func(t *testing.T) {
mockRT := &capturingMockRT{
fn: func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200}, nil
},
}
rt := NewOrgIDRoundTripper(mockRT, 42)
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Empty(t, req.Header.Get(grafana_client.OrgIDHeader))
})
t.Run("nil transport uses default", func(t *testing.T) {
rt := NewOrgIDRoundTripper(nil, 1)
assert.NotNil(t, rt.underlying)
})
}
func TestNewGrafanaClientOrgIDTransport(t *testing.T) {
t.Run("org ID header is sent on requests when configured", func(t *testing.T) {
var capturedHeaders http.Header
ts := newTestHTTPServer(t, func(w http.ResponseWriter, r *http.Request) {
capturedHeaders = r.Header.Clone()
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`[]`))
})
ctx := WithGrafanaConfig(context.Background(), GrafanaConfig{
OrgID: 99,
})
c := NewGrafanaClient(ctx, ts.URL, "test-key", nil, 99)
require.NotNil(t, c)
// Make a real request through the client
_, _ = c.Search.Search(nil, nil)
assert.Equal(t, "99", capturedHeaders.Get(grafana_client.OrgIDHeader))
})
t.Run("org ID header is not sent when org ID is zero", func(t *testing.T) {
var capturedHeaders http.Header
ts := newTestHTTPServer(t, func(w http.ResponseWriter, r *http.Request) {
capturedHeaders = r.Header.Clone()
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`[]`))
})
ctx := WithGrafanaConfig(context.Background(), GrafanaConfig{
OrgID: 0,
})
c := NewGrafanaClient(ctx, ts.URL, "test-key", nil, 0)
require.NotNil(t, c)
_, _ = c.Search.Search(nil, nil)
assert.Empty(t, capturedHeaders.Get(grafana_client.OrgIDHeader))
})
}
func newTestHTTPServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
t.Helper()
ts := httptest.NewServer(handler)
t.Cleanup(ts.Close)
return ts
}