Added support for custom http client for transmitter

This commit is contained in:
d0danie 2019-08-31 11:45:22 -05:00
Родитель d813d77253
Коммит c99dbe79f1
6 изменённых файлов: 18 добавлений и 6 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -6,6 +6,7 @@
# Folders # Folders
_obj _obj
_test _test
.idea
# Architecture specific extensions/prefixes # Architecture specific extensions/prefixes
*.[568vq] *.[568vq]

Просмотреть файл

@ -1,6 +1,7 @@
package appinsights package appinsights
import ( import (
"net/http"
"os" "os"
"runtime" "runtime"
"time" "time"
@ -21,6 +22,9 @@ type TelemetryConfiguration struct {
// Maximum time to wait before sending a batch of telemetry. // Maximum time to wait before sending a batch of telemetry.
MaxBatchInterval time.Duration MaxBatchInterval time.Duration
// Customized http client if desired (will use http.DefaultClient otherwise)
Client *http.Client
} }
// Creates a new TelemetryConfiguration object with the specified // Creates a new TelemetryConfiguration object with the specified

Просмотреть файл

@ -15,4 +15,8 @@ func TestTelemetryConfiguration(t *testing.T) {
if config.EndpointUrl != defaultEndpoint { if config.EndpointUrl != defaultEndpoint {
t.Errorf("EndpointUrl is %s, want %s", 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, batchSize: config.MaxBatchSize,
batchInterval: config.MaxBatchInterval, batchInterval: config.MaxBatchInterval,
throttle: newThrottleManager(), throttle: newThrottleManager(),
transmitter: newTransmitter(config.EndpointUrl), transmitter: newTransmitter(config.EndpointUrl, config.Client),
} }
go channel.acceptLoop() go channel.acceptLoop()

Просмотреть файл

@ -16,6 +16,7 @@ type transmitter interface {
type httpTransmitter struct { type httpTransmitter struct {
endpoint string endpoint string
client *http.Client
} }
type transmissionResult struct { type transmissionResult struct {
@ -50,8 +51,11 @@ const (
serviceUnavailableResponse = 503 serviceUnavailableResponse = 503
) )
func newTransmitter(endpointAddress string) transmitter { func newTransmitter(endpointAddress string, client *http.Client) transmitter {
return &httpTransmitter{endpointAddress} if client == nil {
client = http.DefaultClient
}
return &httpTransmitter{endpointAddress, client}
} }
func (transmitter *httpTransmitter) Transmit(payload []byte, items telemetryBufferItems) (*transmissionResult, error) { 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("Content-Type", "application/x-json-stream")
req.Header.Set("Accept-Encoding", "gzip, deflate") req.Header.Set("Accept-Encoding", "gzip, deflate")
client := http.DefaultClient resp, err := transmitter.client.Do(req)
resp, err := client.Do(req)
if err != nil { if err != nil {
diagnosticsWriter.Printf("Failed to transmit telemetry: %s", err.Error()) diagnosticsWriter.Printf("Failed to transmit telemetry: %s", err.Error())
return nil, err return nil, err

Просмотреть файл

@ -74,7 +74,7 @@ func newTestClientServer() (transmitter, *testServer) {
server.responseData = make([]byte, 0) server.responseData = make([]byte, 0)
server.responseHeaders = make(map[string]string) 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 return client, server
} }