Skip to content

Commit 81e8543

Browse files
sd2kclaude
andauthored
feat: add BaseTransport field to GrafanaConfig (#726)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d1ee007 commit 81e8543

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

mcpgrafana.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ type GrafanaConfig struct {
193193
// MaxLokiLogLimit is the maximum number of log lines that can be returned
194194
// from Loki queries.
195195
MaxLokiLogLimit int
196+
197+
// BaseTransport is an optional base HTTP transport used as the innermost
198+
// layer of the middleware chain in NewGrafanaClient. When set, it replaces
199+
// the default http.Transport that NewGrafanaClient would otherwise create.
200+
// The caller can use this to provide a pre-configured transport with custom
201+
// connection pooling, timeouts, or tracing instrumentation.
202+
// Note: NewGrafanaClient still wraps this transport with ExtraHeaders,
203+
// OrgID, UserAgent, and otelhttp layers.
204+
BaseTransport http.RoundTripper
196205
}
197206

198207
const (
@@ -732,25 +741,30 @@ func NewGrafanaClient(ctx context.Context, grafanaURL, apiKey string, auth *url.
732741
transportField := v.FieldByName("Transport")
733742
if transportField.IsValid() && transportField.CanSet() {
734743
if _, ok := transportField.Interface().(http.RoundTripper); ok {
735-
// Wrap with timeout transport, then user agent, then otel
736-
timeoutTransport := &http.Transport{
737-
Proxy: http.ProxyFromEnvironment,
738-
DialContext: (&net.Dialer{
739-
Timeout: timeout,
740-
KeepAlive: 30 * time.Second,
741-
}).DialContext,
742-
TLSHandshakeTimeout: timeout,
743-
ResponseHeaderTimeout: timeout,
744-
ExpectContinueTimeout: 1 * time.Second,
745-
ForceAttemptHTTP2: true,
746-
MaxIdleConns: 100,
747-
IdleConnTimeout: 90 * time.Second,
748-
}
749-
// Copy TLS config if present
750-
if cfg.TLSConfig != nil {
751-
timeoutTransport.TLSClientConfig = cfg.TLSConfig
744+
// Use caller-provided base transport or create a default one.
745+
var rt http.RoundTripper
746+
if config.BaseTransport != nil {
747+
rt = config.BaseTransport
748+
} else {
749+
timeoutTransport := &http.Transport{
750+
Proxy: http.ProxyFromEnvironment,
751+
DialContext: (&net.Dialer{
752+
Timeout: timeout,
753+
KeepAlive: 30 * time.Second,
754+
}).DialContext,
755+
TLSHandshakeTimeout: timeout,
756+
ResponseHeaderTimeout: timeout,
757+
ExpectContinueTimeout: 1 * time.Second,
758+
ForceAttemptHTTP2: true,
759+
MaxIdleConns: 100,
760+
IdleConnTimeout: 90 * time.Second,
761+
}
762+
// Copy TLS config if present
763+
if cfg.TLSConfig != nil {
764+
timeoutTransport.TLSClientConfig = cfg.TLSConfig
765+
}
766+
rt = timeoutTransport
752767
}
753-
var rt http.RoundTripper = timeoutTransport
754768
if len(config.ExtraHeaders) > 0 {
755769
rt = NewExtraHeadersRoundTripper(rt, config.ExtraHeaders)
756770
}

0 commit comments

Comments
 (0)