Add elasticsearch transport
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package tracer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v7"
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
logging "github.com/ipfs/go-log/v2"
|
||||
)
|
||||
|
||||
var rpclog = logging.Logger("elasticsearch")
|
||||
|
||||
func NewElasticSearchTransport() TracerTransport {
|
||||
es, err := elasticsearch.NewDefaultClient()
|
||||
|
||||
if err != nil {
|
||||
rpclog.Fatalf("Error on creating elastic search client: %+v", err)
|
||||
}
|
||||
|
||||
return &elasticSearchTransport{
|
||||
cl: es,
|
||||
}
|
||||
}
|
||||
|
||||
type elasticSearchTransport struct {
|
||||
cl *elasticsearch.Client
|
||||
}
|
||||
|
||||
func (est *elasticSearchTransport) Transport(jsonEvent []byte) error {
|
||||
req := esapi.IndexRequest{
|
||||
Index: "PeerScore",
|
||||
DocumentID: "1", // todo
|
||||
Body: strings.NewReader(string(jsonEvent)),
|
||||
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)
|
||||
} else {
|
||||
// Deserialize the response into a map.
|
||||
var r map[string]interface{}
|
||||
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
|
||||
return err
|
||||
} else {
|
||||
rpclog.Infof("[%s] %s; version=%d", res.Status(), r["result"], int(r["_version"].(float64)))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
var log = logging.Logger("lotus-tracer")
|
||||
|
||||
func NewLotusTracer(tt TracerTransport, pid peer.ID) LotusTracer {
|
||||
func NewLotusTracer(tt []TracerTransport, pid peer.ID) LotusTracer {
|
||||
return &lotusTracer{
|
||||
tt: tt,
|
||||
pid: pid,
|
||||
@@ -20,7 +20,7 @@ func NewLotusTracer(tt TracerTransport, pid peer.ID) LotusTracer {
|
||||
}
|
||||
|
||||
type lotusTracer struct {
|
||||
tt TracerTransport
|
||||
tt []TracerTransport
|
||||
pid peer.ID
|
||||
}
|
||||
|
||||
@@ -67,10 +67,13 @@ func (lt *lotusTracer) TraceLotusEvent(evt *LotusTraceEvent) {
|
||||
return
|
||||
}
|
||||
|
||||
err = lt.tt.Transport(jsonEvent)
|
||||
if err != nil {
|
||||
log.Errorf("error while transporting peer scores: %s", err)
|
||||
for _, t := range lt.tt {
|
||||
err = t.Transport(jsonEvent)
|
||||
if err != nil {
|
||||
log.Errorf("error while transporting peer scores: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (lt *lotusTracer) Trace(evt *pubsub_pb.TraceEvent) {
|
||||
@@ -80,8 +83,10 @@ func (lt *lotusTracer) Trace(evt *pubsub_pb.TraceEvent) {
|
||||
return
|
||||
}
|
||||
|
||||
err = lt.tt.Transport(jsonEvent)
|
||||
if err != nil {
|
||||
log.Errorf("error while transporting trace event: %s", err)
|
||||
for _, t := range lt.tt {
|
||||
err = t.Transport(jsonEvent)
|
||||
if err != nil {
|
||||
log.Errorf("error while transporting trace event: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user