Added support for custom http client for transmitter
This commit is contained in:
Родитель
d813d77253
Коммит
c99dbe79f1
|
@ -6,6 +6,7 @@
|
|||
# Folders
|
||||
_obj
|
||||
_test
|
||||
.idea
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package appinsights
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
@ -21,6 +22,9 @@ type TelemetryConfiguration struct {
|
|||
|
||||
// Maximum time to wait before sending a batch of telemetry.
|
||||
MaxBatchInterval time.Duration
|
||||
|
||||
// Customized http client if desired (will use http.DefaultClient otherwise)
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
// Creates a new TelemetryConfiguration object with the specified
|
||||
|
|
|
@ -15,4 +15,8 @@ func TestTelemetryConfiguration(t *testing.T) {
|
|||
if config.EndpointUrl != defaultEndpoint {
|
||||
t.Errorf("EndpointUrl is %s, want %s", config.EndpointUrl, defaultEndpoint)
|
||||
}
|
||||
|
||||
if config.Client != nil {
|
||||
t.Errorf("Client is not nil, want nil")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func NewInMemoryChannel(config *TelemetryConfiguration) *InMemoryChannel {
|
|||
batchSize: config.MaxBatchSize,
|
||||
batchInterval: config.MaxBatchInterval,
|
||||
throttle: newThrottleManager(),
|
||||
transmitter: newTransmitter(config.EndpointUrl),
|
||||
transmitter: newTransmitter(config.EndpointUrl, config.Client),
|
||||
}
|
||||
|
||||
go channel.acceptLoop()
|
||||
|
|
|
@ -16,6 +16,7 @@ type transmitter interface {
|
|||
|
||||
type httpTransmitter struct {
|
||||
endpoint string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
type transmissionResult struct {
|
||||
|
@ -50,8 +51,11 @@ const (
|
|||
serviceUnavailableResponse = 503
|
||||
)
|
||||
|
||||
func newTransmitter(endpointAddress string) transmitter {
|
||||
return &httpTransmitter{endpointAddress}
|
||||
func newTransmitter(endpointAddress string, client *http.Client) transmitter {
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
return &httpTransmitter{endpointAddress, client}
|
||||
}
|
||||
|
||||
func (transmitter *httpTransmitter) Transmit(payload []byte, items telemetryBufferItems) (*transmissionResult, error) {
|
||||
|
@ -78,8 +82,7 @@ func (transmitter *httpTransmitter) Transmit(payload []byte, items telemetryBuff
|
|||
req.Header.Set("Content-Type", "application/x-json-stream")
|
||||
req.Header.Set("Accept-Encoding", "gzip, deflate")
|
||||
|
||||
client := http.DefaultClient
|
||||
resp, err := client.Do(req)
|
||||
resp, err := transmitter.client.Do(req)
|
||||
if err != nil {
|
||||
diagnosticsWriter.Printf("Failed to transmit telemetry: %s", err.Error())
|
||||
return nil, err
|
||||
|
|
|
@ -74,7 +74,7 @@ func newTestClientServer() (transmitter, *testServer) {
|
|||
server.responseData = make([]byte, 0)
|
||||
server.responseHeaders = make(map[string]string)
|
||||
|
||||
client := newTransmitter(fmt.Sprintf("http://%s/v2/track", server.server.Listener.Addr().String()))
|
||||
client := newTransmitter(fmt.Sprintf("http://%s/v2/track", server.server.Listener.Addr().String()), nil)
|
||||
|
||||
return client, server
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче