From bd4ce9be6bb70fb98082a1ff91e55030678fbe49 Mon Sep 17 00:00:00 2001 From: Matthew Dolan Date: Tue, 10 Oct 2017 12:10:07 -0700 Subject: [PATCH] Update to the new v2 reporting path. (#115) --- http_collector_client.go | 27 ++++++++++++++++++++------- tracer.go | 14 +++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/http_collector_client.go b/http_collector_client.go index 2f69ef12f..8fbebc363 100644 --- a/http_collector_client.go +++ b/http_collector_client.go @@ -9,11 +9,13 @@ import ( "io/ioutil" "net/http" "time" + "net/url" + "fmt" ) const ( collectorHttpMethod = "POST" - collectorHttpPath = "/v1/report" + collectorHttpPath = "/api/v2/reports" collectorHttpContentTypeHeaderValue = "application/octet-stream" contentTypeHeaderKey = "Content-Type" @@ -30,8 +32,8 @@ type httpCollectorClient struct { reportTimeout time.Duration // Remote service that will receive reports. - socketAddress string - client *http.Client + url *url.URL + client *http.Client // converters converter *protoConverter @@ -47,15 +49,26 @@ func (closer *transportCloser) Close() error { return nil } -func newHttpCollectorClient(opts Options, reporterID uint64, attributes map[string]string) *httpCollectorClient { +func newHttpCollectorClient( + opts Options, + reporterID uint64, + attributes map[string]string, +) (*httpCollectorClient, error) { + url, err := url.Parse(opts.Collector.HostPort()) + if err != nil { + fmt.Println("collector config does not produce valid url", err) + return nil, err + } + url.Path = collectorHttpPath + return &httpCollectorClient{ reporterID: reporterID, accessToken: opts.AccessToken, attributes: attributes, reportTimeout: opts.ReportTimeout, - socketAddress: opts.Collector.HostPort(), + url: url, converter: newProtoConverter(opts), - } + }, nil } func (client *httpCollectorClient) ConnectClient() (Connection, error) { @@ -112,7 +125,7 @@ func (client *httpCollectorClient) toRequest( requestBody := bytes.NewReader(buf) - request, err := http.NewRequest(collectorHttpMethod, client.socketAddress+collectorHttpPath, requestBody) + request, err := http.NewRequest(collectorHttpMethod, client.url.String(), requestBody) if err != nil { return nil, err } diff --git a/tracer.go b/tracer.go index eb25fffeb..9857d1ea1 100644 --- a/tracer.go +++ b/tracer.go @@ -121,7 +121,11 @@ func NewTracer(opts Options) Tracer { impl.buffer.setCurrent(now) - impl.client = newClient(opts, impl.reporterID, attributes) + impl.client, err = newClient(opts, impl.reporterID, attributes) + if err != nil { + fmt.Println("Failed to create to Collector client!", err) + return nil + } conn, err := impl.client.ConnectClient() if err != nil { @@ -144,9 +148,9 @@ func NewTracer(opts Options) Tracer { } -func newClient(opts Options, reporterId uint64, attributes map[string]string) collectorClient { +func newClient(opts Options, reporterId uint64, attributes map[string]string) (collectorClient, error) { if opts.UseThrift { - return newThriftCollectorClient(opts, reporterId, attributes) + return newThriftCollectorClient(opts, reporterId, attributes), nil } if opts.UseHttp { @@ -154,11 +158,11 @@ func newClient(opts Options, reporterId uint64, attributes map[string]string) co } if opts.UseGRPC { - return newGrpcCollectorClient(opts, reporterId, attributes) + return newGrpcCollectorClient(opts, reporterId, attributes), nil } // No transport specified, defaulting to GRPC - return newGrpcCollectorClient(opts, reporterId, attributes) + return newGrpcCollectorClient(opts, reporterId, attributes), nil } func (tracer *tracerImpl) Options() Options {