lotus/node/modules/tracer/elasticsearch_transport.go

112 lines
2.4 KiB
Go
Raw Normal View History

2021-09-15 12:50:27 +00:00
package tracer
import (
2023-05-17 12:58:53 +00:00
"bytes"
2021-09-15 12:50:27 +00:00
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
2021-09-15 12:50:27 +00:00
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esutil"
2021-09-15 12:50:27 +00:00
)
2021-09-15 13:11:34 +00:00
const (
2021-09-29 11:12:42 +00:00
ElasticSearchDefaultIndex = "lotus-pubsub"
flushInterval = 10 * time.Second
flushBytes = 1024 * 1024 // MB
esWorkers = 2 // TODO: hardcoded
2021-09-15 13:11:34 +00:00
)
2021-09-15 12:50:27 +00:00
func NewElasticSearchTransport(connectionString string, elasticsearchIndex string) (TracerTransport, error) {
conUrl, err := url.Parse(connectionString)
2021-09-24 10:40:33 +00:00
if err != nil {
return nil, err
}
username := conUrl.User.Username()
password, _ := conUrl.User.Password()
cfg := elasticsearch.Config{
Addresses: []string{
2021-09-24 10:58:32 +00:00
conUrl.Scheme + "://" + conUrl.Host,
},
2023-05-17 12:58:53 +00:00
Username: username,
Password: password,
Transport: &http.Transport{},
}
es, err := elasticsearch.NewClient(cfg)
2021-09-15 12:50:27 +00:00
if err != nil {
2021-09-15 12:58:36 +00:00
return nil, err
2021-09-15 12:50:27 +00:00
}
var esIndex string
if elasticsearchIndex != "" {
esIndex = elasticsearchIndex
} else {
2021-09-29 11:12:42 +00:00
esIndex = ElasticSearchDefaultIndex
}
// Create the BulkIndexer to batch ES trace submission
bi, err := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
Index: esIndex,
Client: es,
NumWorkers: esWorkers,
FlushBytes: int(flushBytes),
FlushInterval: flushInterval,
OnError: func(ctx context.Context, err error) {
log.Errorf("Error persisting queries %s", err.Error())
},
})
if err != nil {
return nil, err
}
2021-09-15 12:50:27 +00:00
return &elasticSearchTransport{
2023-05-17 12:58:53 +00:00
cl: es,
bi: bi,
esIndex: esIndex,
2021-09-15 12:58:36 +00:00
}, nil
2021-09-15 12:50:27 +00:00
}
type elasticSearchTransport struct {
cl *elasticsearch.Client
2023-05-17 12:58:53 +00:00
bi esutil.BulkIndexer
esIndex string
2021-09-15 12:50:27 +00:00
}
2021-09-15 14:16:12 +00:00
func (est *elasticSearchTransport) Transport(evt TracerTransportEvent) error {
var e interface{}
2021-09-17 13:14:39 +00:00
2021-09-15 14:16:12 +00:00
if evt.lotusTraceEvent != nil {
e = *evt.lotusTraceEvent
} else if evt.pubsubTraceEvent != nil {
e = *evt.pubsubTraceEvent
} else {
return nil
}
2021-09-15 14:16:12 +00:00
jsonEvt, err := json.Marshal(e)
if err != nil {
2021-09-15 14:05:44 +00:00
return fmt.Errorf("error while marshaling event: %s", err)
}
return est.bi.Add(
context.Background(),
esutil.BulkIndexerItem{
Action: "index",
Body: bytes.NewReader(jsonEvt),
OnFailure: func(ctx context.Context, item esutil.BulkIndexerItem, res esutil.BulkIndexerResponseItem, err error) {
if err != nil {
log.Errorf("unable to submit trace - %s", err)
} else {
log.Errorf("unable to submit trace %s: %s", res.Error.Type, res.Error.Reason)
}
},
},
)
2021-09-15 12:50:27 +00:00
}