Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Support custom HTTP headers when reporting spans over HTTP (#479)
Browse files Browse the repository at this point in the history
Signed-off-by: Albert Teoh <[email protected]>
  • Loading branch information
albertteoh authored and yurishkuro committed Dec 17, 2019
1 parent 15ec52a commit 667bc9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
13 changes: 9 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ type ReporterConfig struct {
// Password instructs reporter to include a password for basic http authentication when sending spans to
// jaeger-collector. Can be set by exporting an environment variable named JAEGER_PASSWORD
Password string `yaml:"password"`

// HTTPHeaders instructs the reporter to add these headers to the http request when reporting spans.
// This field takes effect only when using HTTPTransport by setting the CollectorEndpoint.
HTTPHeaders map[string]string `yaml:"http_headers"`
}

// BaggageRestrictionsConfig configures the baggage restrictions manager which can be used to whitelist
Expand Down Expand Up @@ -397,11 +401,12 @@ func (rc *ReporterConfig) NewReporter(

func (rc *ReporterConfig) newTransport() (jaeger.Transport, error) {
switch {
case rc.CollectorEndpoint != "" && rc.User != "" && rc.Password != "":
return transport.NewHTTPTransport(rc.CollectorEndpoint, transport.HTTPBatchSize(1),
transport.HTTPBasicAuth(rc.User, rc.Password)), nil
case rc.CollectorEndpoint != "":
return transport.NewHTTPTransport(rc.CollectorEndpoint, transport.HTTPBatchSize(1)), nil
httpOptions := []transport.HTTPOption{transport.HTTPBatchSize(1), transport.HTTPHeaders(rc.HTTPHeaders)}
if rc.User != "" && rc.Password != "" {
httpOptions = append(httpOptions, transport.HTTPBasicAuth(rc.User, rc.Password))
}
return transport.NewHTTPTransport(rc.CollectorEndpoint, httpOptions...), nil
default:
return jaeger.NewUDPTransport(rc.LocalAgentHostPort, 0)
}
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ func TestHTTPTransportType(t *testing.T) {
require.IsType(t, expect, sender)
}

func TestHTTPTransportTypeWithAuth(t *testing.T) {
rc := &ReporterConfig{
CollectorEndpoint: "http://1.2.3.4:5678/api/traces",
User: "auth_user",
Password: "auth_pass",
}
expect := transport.NewHTTPTransport(rc.CollectorEndpoint)
sender, err := rc.newTransport()
require.NoError(t, err)
require.IsType(t, expect, sender)
}

func TestDefaultConfig(t *testing.T) {
cfg := Configuration{}
_, _, err := cfg.NewTracer(Metrics(metrics.NullFactory), Logger(log.NullLogger))
Expand Down
11 changes: 11 additions & 0 deletions transport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type HTTPTransport struct {
spans []*j.Span
process *j.Process
httpCredentials *HTTPBasicAuthCredentials
headers map[string]string
}

// HTTPBasicAuthCredentials stores credentials for HTTP basic auth.
Expand Down Expand Up @@ -76,6 +77,13 @@ func HTTPRoundTripper(transport http.RoundTripper) HTTPOption {
}
}

// HTTPHeaders defines the HTTP headers that will be attached to the jaeger client's HTTP request
func HTTPHeaders(headers map[string]string) HTTPOption {
return func(c *HTTPTransport) {
c.headers = headers
}
}

// NewHTTPTransport returns a new HTTP-backend transport. url should be an http
// url of the collector to handle POST request, typically something like:
// http://hostname:14268/api/traces?format=jaeger.thrift
Expand Down Expand Up @@ -136,6 +144,9 @@ func (c *HTTPTransport) send(spans []*j.Span) error {
return err
}
req.Header.Set("Content-Type", "application/x-thrift")
for k, v := range c.headers {
req.Header.Set(k, v)
}

if c.httpCredentials != nil {
req.SetBasicAuth(c.httpCredentials.username, c.httpCredentials.password)
Expand Down
13 changes: 13 additions & 0 deletions transport/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestHTTPTransport(t *testing.T) {
"http://localhost:10001/api/v1/spans",
HTTPBatchSize(1),
HTTPBasicAuth(httpUsername, httpPassword),
HTTPHeaders(map[string]string{"my-key": "my-value"}),
)

tracer, closer := jaeger.NewTracer(
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestHTTPTransport(t *testing.T) {
&HTTPBasicAuthCredentials{username: httpUsername, password: httpPassword},
server.authCredentials[0],
)
assert.Equal(t, "my-value", server.getHeader().Get("my-key"))
}

func TestHTTPOptions(t *testing.T) {
Expand All @@ -88,17 +90,20 @@ func TestHTTPOptions(t *testing.T) {
HTTPBatchSize(123),
HTTPTimeout(123*time.Millisecond),
HTTPRoundTripper(roundTripper),
HTTPHeaders(map[string]string{"my-key": "my-value"}),
)
assert.Equal(t, 123, sender.batchSize)
assert.Equal(t, 123*time.Millisecond, sender.client.Timeout)
assert.Equal(t, roundTripper, sender.client.Transport)
assert.Equal(t, "my-value", sender.headers["my-key"])
}

type httpServer struct {
t *testing.T
batches []*j.Batch
authCredentials []*HTTPBasicAuthCredentials
mutex sync.RWMutex
header http.Header
}

func (s *httpServer) getBatches() []*j.Batch {
Expand All @@ -113,6 +118,12 @@ func (s *httpServer) credentials() []*HTTPBasicAuthCredentials {
return s.authCredentials
}

func (s *httpServer) getHeader() http.Header {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.header
}

// TODO this function and zipkin/http_test.go#newHTTPServer look like twins lost at birth
func newHTTPServer(t *testing.T) *httpServer {
server := &httpServer{
Expand Down Expand Up @@ -151,6 +162,8 @@ func newHTTPServer(t *testing.T) *httpServer {
server.batches = append(server.batches, batch)
u, p, _ := r.BasicAuth()
server.authCredentials = append(server.authCredentials, &HTTPBasicAuthCredentials{username: u, password: p})

server.header = r.Header
})

go func() {
Expand Down

0 comments on commit 667bc9e

Please sign in to comment.