2021-09-15 12:50:27 +00:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-16 10:15:42 +00:00
|
|
|
"net/url"
|
2021-09-15 12:50:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/elastic/go-elasticsearch/v7"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
|
|
|
)
|
|
|
|
|
2021-09-15 13:11:34 +00:00
|
|
|
const (
|
|
|
|
ElasticSearch_INDEX = "pubsub"
|
2021-09-15 14:03:51 +00:00
|
|
|
|
|
|
|
ElasticSearch_DOC_LOTUS = "doc_lotus"
|
|
|
|
ElasticSearch_DOC_PUBSUB = "doc_pubsub"
|
2021-09-15 13:11:34 +00:00
|
|
|
)
|
2021-09-15 12:50:27 +00:00
|
|
|
|
2021-09-16 10:15:42 +00:00
|
|
|
func NewElasticSearchTransport(connectionString string) (TracerTransport, error) {
|
|
|
|
conUrl, err := url.Parse(connectionString)
|
|
|
|
|
|
|
|
username := conUrl.User.Username()
|
|
|
|
password, _ := conUrl.User.Password()
|
|
|
|
cfg := elasticsearch.Config{
|
|
|
|
Addresses: []string{
|
|
|
|
"https://" + conUrl.Host,
|
|
|
|
},
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return &elasticSearchTransport{
|
|
|
|
cl: es,
|
2021-09-15 12:58:36 +00:00
|
|
|
}, nil
|
2021-09-15 12:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type elasticSearchTransport struct {
|
|
|
|
cl *elasticsearch.Client
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:16:12 +00:00
|
|
|
func (est *elasticSearchTransport) Transport(evt TracerTransportEvent) error {
|
2021-09-15 14:03:51 +00:00
|
|
|
var e interface{}
|
|
|
|
var docId string
|
2021-09-15 14:16:12 +00:00
|
|
|
if evt.lotusTraceEvent != nil {
|
|
|
|
e = *evt.lotusTraceEvent
|
2021-09-16 14:49:47 +00:00
|
|
|
docId = ElasticSearch_DOC_PUBSUB
|
2021-09-15 14:16:12 +00:00
|
|
|
} else if evt.pubsubTraceEvent != nil {
|
|
|
|
e = *evt.pubsubTraceEvent
|
2021-09-16 14:49:47 +00:00
|
|
|
docId = ElasticSearch_DOC_PUBSUB
|
2021-09-15 14:03:51 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:16:12 +00:00
|
|
|
jsonEvt, err := json.Marshal(e)
|
2021-09-15 14:03:51 +00:00
|
|
|
if err != nil {
|
2021-09-15 14:05:44 +00:00
|
|
|
return fmt.Errorf("error while marshaling event: %s", err)
|
2021-09-15 14:03:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 12:50:27 +00:00
|
|
|
req := esapi.IndexRequest{
|
2021-09-15 13:11:34 +00:00
|
|
|
Index: ElasticSearch_INDEX,
|
2021-09-15 14:03:51 +00:00
|
|
|
DocumentID: docId,
|
2021-09-15 14:16:12 +00:00
|
|
|
Body: strings.NewReader(string(jsonEvt)),
|
2021-09-15 12:50:27 +00:00
|
|
|
Refresh: "true",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the request with the client.
|
|
|
|
res, err := req.Do(context.Background(), est.cl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if res.IsError() {
|
|
|
|
return fmt.Errorf("[%s] Error indexing document ID=%s", res.Status(), req.DocumentID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|