Skip to content

Commit

Permalink
Add support for RemoteEndpoint in transport/client. (openzipkin#170)
Browse files Browse the repository at this point in the history
* Add support for `RemoteEndpoint` in transport/client.

* Allow remote endpoint as functional option instead of only the service name
  • Loading branch information
dengliming authored Aug 13, 2020
1 parent f19d47b commit faa50ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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

0 comments on commit faa50ce

Please sign in to comment.