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

Provide the ability to override HTTPTransport configuration #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"compress/zlib"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
mrand "math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -336,25 +336,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 +512,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 +916,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
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestShouldExcludeErr(t *testing.T) {
regexpStrs := []string{"ERR_TIMEOUT", "should.exclude", "(?i)^big$"}

client := &Client{
Transport: newTransport(),
Transport: NewHTTPTransport(nil),
Tags: nil,
context: &context{},
queue: make(chan *outgoingPacket, MaxQueueBuffer),
Expand Down