Skip to content

Commit

Permalink
feat(reporter/http): uses an interface for http client (#155)
Browse files Browse the repository at this point in the history
* feat(reporter/http): uses an interface for http client so users can plug other clients.
  • Loading branch information
jcchavezs authored and basvanbeek committed Nov 19, 2019
1 parent bafd576 commit d761b19
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions reporter/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"bytes"
"context"
"log"
"net/http"
"os"
Expand All @@ -37,10 +38,15 @@ const (
defaultMaxBacklog = 1000
)

// HTTPDoer will do a request to the Zipkin HTTP Collector
type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
}

// httpReporter will send spans to a Zipkin HTTP Collector using Zipkin V2 API.
type httpReporter struct {
url string
client *http.Client
client HTTPDoer
logger *log.Logger
batchInterval time.Duration
batchSize int
Expand All @@ -52,6 +58,7 @@ type httpReporter struct {
quit chan struct{}
shutdown chan error
reqCallback RequestCallbackFn
reqTimeout time.Duration
serializer reporter.SpanSerializer
}

Expand Down Expand Up @@ -150,7 +157,10 @@ func (r *httpReporter) sendBatch() error {
r.reqCallback(req)
}

resp, err := r.client.Do(req)
ctx, cancel := context.WithTimeout(req.Context(), r.reqTimeout)
defer cancel()

resp, err := r.client.Do(req.WithContext(ctx))
if err != nil {
r.logger.Printf("failed to send the request: %s\n", err.Error())
return err
Expand All @@ -176,9 +186,9 @@ type RequestCallbackFn func(*http.Request)
// ReporterOption sets a parameter for the HTTP Reporter
type ReporterOption func(r *httpReporter)

// Timeout sets maximum timeout for http request.
// Timeout sets maximum timeout for the http request through its context.
func Timeout(duration time.Duration) ReporterOption {
return func(r *httpReporter) { r.client.Timeout = duration }
return func(r *httpReporter) { r.reqTimeout = duration }
}

// BatchSize sets the maximum batch size, after which a collect will be
Expand All @@ -199,8 +209,9 @@ func BatchInterval(d time.Duration) ReporterOption {
return func(r *httpReporter) { r.batchInterval = d }
}

// Client sets a custom http client to use.
func Client(client *http.Client) ReporterOption {
// Client sets a custom http client to use under the interface HTTPDoer
// which includes a `Do` method with same signature as the *http.Client
func Client(client HTTPDoer) ReporterOption {
return func(r *httpReporter) { r.client = client }
}

Expand Down Expand Up @@ -233,7 +244,7 @@ func NewReporter(url string, opts ...ReporterOption) reporter.Reporter {
r := httpReporter{
url: url,
logger: log.New(os.Stderr, "", log.LstdFlags),
client: &http.Client{Timeout: defaultTimeout},
client: &http.Client{},
batchInterval: defaultBatchInterval,
batchSize: defaultBatchSize,
maxBacklog: defaultMaxBacklog,
Expand All @@ -244,6 +255,7 @@ func NewReporter(url string, opts ...ReporterOption) reporter.Reporter {
shutdown: make(chan error, 1),
batchMtx: &sync.Mutex{},
serializer: reporter.JSONSerializer{},
reqTimeout: defaultTimeout,
}

for _, opt := range opts {
Expand Down

0 comments on commit d761b19

Please sign in to comment.