Skip to content
Oliver Eilhard edited this page Mar 2, 2015 · 12 revisions

For advanced scenarios, you can provide your own http.Client / http.Transport. You need to create your own http.Client, set its Transport field, then configure the new client with elastic.SetHttpClient(...).

Here is an example that counts requests.

// CountingTransport will count requests.
type CountingTransport struct {
        N    int64              // number of requests passing this transport
	next http.RoundTripper  // next round-tripper or http.DefaultTransport if nil
}

// RoundTrip implements a transport that will count requests.
func (tr *CountingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
        atomic. AddInt64(&tr.N, 1)
	if tr.next != nil {
		return tr.next.RoundTrip(r)
	}
	return http.DefaultTransport.RoundTrip(r)
}

...
myHttpClient := &http.Client{Transport: &CountingTransport{}}
client, err := elastic.NewClient(elastic.SetHttpClient(myHttpClient))
Clone this wiki locally