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

Commit

Permalink
Provide the ability to override HTTPTransport configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Dec 7, 2018
1 parent f04e748 commit 34eb7aa
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"compress/zlib"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -336,25 +337,9 @@ func (c *context) interfaces() []Interface {
// Packets will be dropped if the buffer is full. Used by NewClient.
var MaxQueueBuffer = 100

func newTransport() Transport {
t := &HTTPTransport{}
rootCAs, err := gocertifi.CACerts()
if err != nil {
log.Println("raven: failed to load root TLS certificates:", err)
} else {
t.Client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{RootCAs: rootCAs},
},
}
}
return t
}

func newClient(tags map[string]string) *Client {
client := &Client{
Transport: newTransport(),
Transport: NewHTTPTransport(nil),
Tags: tags,
context: &context{},
sampleRate: 1.0,
Expand Down Expand Up @@ -528,6 +513,16 @@ func (client *Client) SetSampleRate(rate float32) error {
return nil
}

// SetTransport sets the client's Transport
func (client *Client) SetTransport(t Transport) {
client.mu.Lock()
defer client.mu.Unlock()
client.Transport = t
}

// SetTransport sets the Transport on the default *Client
func SetTransport(t Transport) { DefaultClient.SetTransport(t) }

// SetRelease sets the "release" tag on the default *Client
func SetRelease(release string) { DefaultClient.SetRelease(release) }

Expand Down Expand Up @@ -922,6 +917,34 @@ type HTTPTransport struct {
*http.Client
}

// HTTPTransportOptions are options to configure the HTTPTransport
type HTTPTransportOptions struct {
InsecureSkipVerify bool
RootCAs *x509.CertPool
}

func NewHTTPTransport(o *HTTPTransportOptions) *HTTPTransport {
if o == nil {
o = &HTTPTransportOptions{}
}
if o.RootCAs == nil {
// This can't actually error, so there's no point.
rootCAs, _ := gocertifi.CACerts()
o.RootCAs = rootCAs
}
return &HTTPTransport{
Client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
RootCAs: o.RootCAs,
InsecureSkipVerify: o.InsecureSkipVerify,
},
},
},
}
}

func (t *HTTPTransport) Send(url, authHeader string, packet *Packet) error {
if url == "" {
return nil
Expand Down

0 comments on commit 34eb7aa

Please sign in to comment.