in cmd/apmtool/espoll.go [127:165]
func Main(ctx context.Context, cfg config) error {
if cfg.query == "" {
return errors.New("query cannot be empty")
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: cfg.tlsSkipVerify}
client, err := elasticsearch.NewClient(elasticsearch.Config{
Username: cfg.esUsername,
Password: cfg.esPassword,
Addresses: strings.Split(cfg.esURL, ","),
Transport: transport,
MaxRetries: 5,
RetryBackoff: func(attempt int) time.Duration {
backoff := (500 * time.Millisecond) * (1 << (attempt - 1))
if backoff > maxElasticsearchBackoff {
backoff = maxElasticsearchBackoff
}
return backoff
},
})
if err != nil {
return err
}
esClient := espoll.WrapClient(client)
result, err := esClient.SearchIndexMinDocs(ctx,
int(cfg.hits), cfg.target, stringMarshaler(cfg.query),
espoll.WithTimeout(cfg.timeout),
)
if err != nil {
return fmt.Errorf("search request returned error: %w", err)
}
if err := json.NewEncoder(os.Stdout).Encode(result); err != nil {
return fmt.Errorf("failed to encode search result: %w", err)
}
return nil
}