-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from grafana/instrument-http-client
Add http client tracing instrumentation
- Loading branch information
Showing
4 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package util | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/opentracing-contrib/go-stdlib/nethttp" | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
var defaultTransport http.RoundTripper = &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}).DialContext, | ||
ForceAttemptHTTP2: true, | ||
MaxIdleConns: 200, | ||
MaxIdleConnsPerHost: 200, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
} | ||
|
||
type RoundTripperFunc func(req *http.Request) (*http.Response, error) | ||
|
||
func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req) | ||
} | ||
|
||
// InstrumentedHTTPClient returns a HTTP client with tracing instrumented default RoundTripper. | ||
func InstrumentedHTTPClient() *http.Client { | ||
return &http.Client{ | ||
Transport: WrapWithInstrumentedHTTPTransport(defaultTransport), | ||
} | ||
} | ||
|
||
// WrapWithInstrumentedHTTPTransport wraps the given RoundTripper with an tracing instrumented one. | ||
func WrapWithInstrumentedHTTPTransport(next http.RoundTripper) http.RoundTripper { | ||
next = &nethttp.Transport{RoundTripper: next} | ||
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
req, tr := nethttp.TraceRequest(opentracing.GlobalTracer(), req) | ||
defer tr.Finish() | ||
return next.RoundTrip(req) | ||
}) | ||
} |