-
Notifications
You must be signed in to change notification settings - Fork 1
/
es.go
50 lines (42 loc) · 1.17 KB
/
es.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
/*
This package is an entry point in the same way as package elasticsearch in the official go client.
Different name is chosen to avoid corresponding warning.
Acquiring client is different only in package name and type of passed Config struct:
esClient, err := es.NewClient(es.Config{})
*/
package es
import (
"github.com/elastic/go-elasticsearch/v7"
)
// Returning standard Client from elasticsearch package but with modified transport
func NewClient(serviceEsCfg Config) (*elasticsearch.Client, error) {
var client *elasticsearch.Client
var err error
cfg := elasticsearch.Config{
Addresses: serviceEsCfg.Addresses,
}
client, err = elasticsearch.NewClient(cfg)
if err != nil {
return nil, err
}
if serviceEsCfg.Tracing {
client.Transport = EsTransportWithTracing{
Name: serviceEsCfg.Name,
EsTransport: client.Transport,
}
}
if serviceEsCfg.Metrics {
client.Transport = EsTransportWithMetrics{
Name: serviceEsCfg.Name,
EsTransport: client.Transport,
}
}
return client, nil
}
func MustNew(serviceEsCfg Config) (*elasticsearch.Client, error) {
client, err := NewClient(serviceEsCfg)
if err != nil {
panic(err)
}
return client, nil
}