Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 34eb7aa

Browse files
committed
Provide the ability to override HTTPTransport configuration
1 parent f04e748 commit 34eb7aa

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

client.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"compress/zlib"
77
"crypto/rand"
88
"crypto/tls"
9+
"crypto/x509"
910
"encoding/base64"
1011
"encoding/hex"
1112
"encoding/json"
@@ -336,25 +337,9 @@ func (c *context) interfaces() []Interface {
336337
// Packets will be dropped if the buffer is full. Used by NewClient.
337338
var MaxQueueBuffer = 100
338339

339-
func newTransport() Transport {
340-
t := &HTTPTransport{}
341-
rootCAs, err := gocertifi.CACerts()
342-
if err != nil {
343-
log.Println("raven: failed to load root TLS certificates:", err)
344-
} else {
345-
t.Client = &http.Client{
346-
Transport: &http.Transport{
347-
Proxy: http.ProxyFromEnvironment,
348-
TLSClientConfig: &tls.Config{RootCAs: rootCAs},
349-
},
350-
}
351-
}
352-
return t
353-
}
354-
355340
func newClient(tags map[string]string) *Client {
356341
client := &Client{
357-
Transport: newTransport(),
342+
Transport: NewHTTPTransport(nil),
358343
Tags: tags,
359344
context: &context{},
360345
sampleRate: 1.0,
@@ -528,6 +513,16 @@ func (client *Client) SetSampleRate(rate float32) error {
528513
return nil
529514
}
530515

516+
// SetTransport sets the client's Transport
517+
func (client *Client) SetTransport(t Transport) {
518+
client.mu.Lock()
519+
defer client.mu.Unlock()
520+
client.Transport = t
521+
}
522+
523+
// SetTransport sets the Transport on the default *Client
524+
func SetTransport(t Transport) { DefaultClient.SetTransport(t) }
525+
531526
// SetRelease sets the "release" tag on the default *Client
532527
func SetRelease(release string) { DefaultClient.SetRelease(release) }
533528

@@ -922,6 +917,34 @@ type HTTPTransport struct {
922917
*http.Client
923918
}
924919

920+
// HTTPTransportOptions are options to configure the HTTPTransport
921+
type HTTPTransportOptions struct {
922+
InsecureSkipVerify bool
923+
RootCAs *x509.CertPool
924+
}
925+
926+
func NewHTTPTransport(o *HTTPTransportOptions) *HTTPTransport {
927+
if o == nil {
928+
o = &HTTPTransportOptions{}
929+
}
930+
if o.RootCAs == nil {
931+
// This can't actually error, so there's no point.
932+
rootCAs, _ := gocertifi.CACerts()
933+
o.RootCAs = rootCAs
934+
}
935+
return &HTTPTransport{
936+
Client: &http.Client{
937+
Transport: &http.Transport{
938+
Proxy: http.ProxyFromEnvironment,
939+
TLSClientConfig: &tls.Config{
940+
RootCAs: o.RootCAs,
941+
InsecureSkipVerify: o.InsecureSkipVerify,
942+
},
943+
},
944+
},
945+
}
946+
}
947+
925948
func (t *HTTPTransport) Send(url, authHeader string, packet *Packet) error {
926949
if url == "" {
927950
return nil

0 commit comments

Comments
 (0)