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 1 commit
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
Next Next commit
Add support for RemoteEndpoint in transport/client.
  • Loading branch information
dengliming committed Aug 11, 2020
commit b00ba7449e453d0f8db141597272a08b1f915d25
21 changes: 16 additions & 5 deletions middleware/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ var ErrValidTracerRequired = errors.New("valid tracer required")
// Client holds a Zipkin instrumented HTTP Client.
type Client struct {
*http.Client
tracer *zipkin.Tracer
httpTrace bool
defaultTags map[string]string
transportOptions []TransportOption
tracer *zipkin.Tracer
httpTrace bool
defaultTags map[string]string
transportOptions []TransportOption
remoteServiceName string
}

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

// WithRemoteServiceName will set the value for the remote endpoint's service name on
// all spans.
func WithRemoteServiceName(name string) ClientOption {
return func(c *Client) {
c.remoteServiceName = name
}
}

// 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 +97,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),
TransportRemoteServiceName(c.remoteServiceName),
)
transport, err := NewTransport(tracer, c.transportOptions...)
if err != nil {
Expand All @@ -106,7 +116,8 @@ func (c *Client) DoWithAppSpan(req *http.Request, name string) (res *http.Respon
parentContext = span.Context()
}

appSpan := c.tracer.StartSpan(name, zipkin.Parent(parentContext))
ep, _ := zipkin.NewEndpoint(c.remoteServiceName, req.Host)
appSpan := c.tracer.StartSpan(name, zipkin.Parent(parentContext), zipkin.RemoteEndpoint(ep))

zipkin.TagHTTPMethod.Set(appSpan, req.Method)
zipkin.TagHTTPPath.Set(appSpan, req.URL.Path)
Expand Down
7 changes: 7 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",
}

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

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

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

res, err = client.Do(req)
Expand Down
12 changes: 11 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
remoteServiceName string
}

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

// TransportRemoteServiceName will set the value for the remote endpoint's service name on
// all spans.
func TransportRemoteServiceName(name string) TransportOption {
return func(c *transport) {
c.remoteServiceName = name
}
}

// TransportLogger allows to plug a logger into the transport
func TransportLogger(l *log.Logger) TransportOption {
return func(t *transport) {
Expand Down Expand Up @@ -142,8 +151,9 @@ 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) {
ep, _ := zipkin.NewEndpoint(t.remoteServiceName, req.Host)
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(ep),
)

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