forked from elastic/go-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customization.go
85 lines (67 loc) · 2.01 KB
/
customization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
// +build ignore
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/elastic/go-elasticsearch/v8"
)
// This example demonstrates how to provide a custom transport implementation to the client
// in order to read or manipulate the requests and responses, eg. for custom logging
// implementation, passing custom headers to the request, and so on.
//
// CountingTransport adds a custom header to the request, logs the information about
// the request and response, and counts the number of requests to the client.
//
// Since it implements the `http.RoundTripper` interface, it can be passed
// to the client as a custom HTTP transport implementation.
//
type CountingTransport struct {
count uint64
}
// RoundTrip executes a request and returns a response.
//
func (t *CountingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var b bytes.Buffer
atomic.AddUint64(&t.count, 1)
req.Header.Set("Accept", "application/yaml")
req.Header.Set("X-Request-ID", "foo-123")
res, err := http.DefaultTransport.RoundTrip(req)
b.WriteString(strings.Repeat("-", 80) + "\n")
fmt.Fprintf(&b, "%s %s", req.Method, req.URL.String())
if err == nil {
fmt.Fprintf(&b, " [%s] %s\n", res.Status, res.Header.Get("Content-Type"))
} else {
fmt.Fprintf(&b, "ERROR: %s\n", err)
}
b.WriteTo(os.Stdout)
return res, err
}
func main() {
var wg sync.WaitGroup
// Create the custom transport.
//
tp := CountingTransport{}
// Pass the custom transport to the client.
//
es, _ := elasticsearch.NewClient(
elasticsearch.Config{Transport: &tp},
)
for i := 0; i < 25; i++ {
wg.Add(1)
go func() {
defer wg.Done()
es.Info()
}()
}
wg.Wait()
fmt.Println(strings.Repeat("=", 80))
fmt.Printf("%80s\n", fmt.Sprintf("Total Requests: %d", atomic.LoadUint64(&tp.count)))
}