Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RemoteEndpoint in transport/client. #170

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion middleware/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Client struct {
httpTrace bool
defaultTags map[string]string
transportOptions []TransportOption
remoteEndpoint *model.Endpoint
}

// ClientOption allows optional configuration of Client.
Expand Down Expand Up @@ -71,6 +72,13 @@ func TransportOptions(options ...TransportOption) ClientOption {
}
}

// WithRemoteEndpoint will set the remote endpoint for all spans.
func WithRemoteEndpoint(remoteEndpoint *model.Endpoint) ClientOption {
return func(c *Client) {
c.remoteEndpoint = remoteEndpoint
}
}

// NewClient returns an HTTP Client adding Zipkin instrumentation around an
// embedded standard Go http.Client.
func NewClient(tracer *zipkin.Tracer, options ...ClientOption) (*Client, error) {
Expand All @@ -88,6 +96,7 @@ func NewClient(tracer *zipkin.Tracer, options ...ClientOption) (*Client, error)
// the following Client settings override provided transport settings.
RoundTripper(c.Client.Transport),
TransportTrace(c.httpTrace),
TransportRemoteEndpoint(c.remoteEndpoint),
)
transport, err := NewTransport(tracer, c.transportOptions...)
if err != nil {
Expand All @@ -106,7 +115,7 @@ func (c *Client) DoWithAppSpan(req *http.Request, name string) (res *http.Respon
parentContext = span.Context()
}

appSpan := c.tracer.StartSpan(name, zipkin.Parent(parentContext))
appSpan := c.tracer.StartSpan(name, zipkin.Parent(parentContext), zipkin.RemoteEndpoint(c.remoteEndpoint))

zipkin.TagHTTPMethod.Set(appSpan, req.Method)
zipkin.TagHTTPPath.Set(appSpan, req.URL.Path)
Expand Down
10 changes: 10 additions & 0 deletions middleware/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func TestHTTPClient(t *testing.T) {
"conf.timeout": "default",
}

remoteEndpoint, _ := zipkin.NewEndpoint("google-service", "1.2.3.4:80")
client, err := httpclient.NewClient(
tracer,
httpclient.WithClient(&http.Client{}),
httpclient.ClientTrace(true),
httpclient.ClientTags(clientTags),
httpclient.TransportOptions(httpclient.TransportTags(transportTags)),
httpclient.WithRemoteEndpoint(remoteEndpoint),
)
if err != nil {
t.Fatalf("unable to create http client: %+v", err)
Expand All @@ -65,6 +67,14 @@ func TestHTTPClient(t *testing.T) {
t.Errorf("Span Count want 2+, have %d", len(spans))
}

rep := spans[0].RemoteEndpoint
if rep == nil {
t.Errorf("Span remoteEndpoint must not nil")
}
if rep.ServiceName != remoteEndpoint.ServiceName {
t.Errorf("Span remoteEndpoint ServiceName want %s, have %s", remoteEndpoint.ServiceName, rep.ServiceName)
}

req, _ = http.NewRequest("GET", "https://www.google.com", nil)

res, err = client.Do(req)
Expand Down
10 changes: 9 additions & 1 deletion middleware/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type transport struct {
errResponseReader *ErrResponseReader
logger *log.Logger
requestSampler RequestSamplerFunc
remoteEndpoint *model.Endpoint
}

// TransportOption allows one to configure optional transport configuration.
Expand Down Expand Up @@ -100,6 +101,13 @@ func TransportErrResponseReader(r ErrResponseReader) TransportOption {
}
}

// TransportRemoteEndpoint will set the remote endpoint for all spans.
func TransportRemoteEndpoint(remoteEndpoint *model.Endpoint) TransportOption {
return func(c *transport) {
c.remoteEndpoint = remoteEndpoint
}
}

// TransportLogger allows to plug a logger into the transport
func TransportLogger(l *log.Logger) TransportOption {
return func(t *transport) {
Expand Down Expand Up @@ -143,7 +151,7 @@ func NewTransport(tracer *zipkin.Tracer, options ...TransportOption) (http.Round
// RoundTrip satisfies the RoundTripper interface.
func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error) {
sp, _ := t.tracer.StartSpanFromContext(
req.Context(), req.URL.Scheme+"/"+req.Method, zipkin.Kind(model.Client),
req.Context(), req.URL.Scheme+"/"+req.Method, zipkin.Kind(model.Client), zipkin.RemoteEndpoint(t.remoteEndpoint),
)

for k, v := range t.defaultTags {
Expand Down