2021-09-15 11:41:03 +00:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
2021-09-15 14:03:51 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-15 11:41:03 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonTracerTransport struct {
|
|
|
|
out *os.File
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:22:18 +00:00
|
|
|
func NewJsonTracerTransport(file string) (TracerTransport, error) {
|
|
|
|
out, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-15 11:41:03 +00:00
|
|
|
return &jsonTracerTransport{
|
|
|
|
out: out,
|
2021-09-16 13:22:18 +00:00
|
|
|
}, nil
|
2021-09-15 11:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:16:12 +00:00
|
|
|
func (jtt *jsonTracerTransport) Transport(evt TracerTransportEvent) error {
|
2021-09-15 14:03:51 +00:00
|
|
|
var e interface{}
|
2021-09-15 14:16:12 +00:00
|
|
|
if evt.lotusTraceEvent != nil {
|
|
|
|
e = *evt.lotusTraceEvent
|
|
|
|
} else if evt.pubsubTraceEvent != nil {
|
|
|
|
e = *evt.pubsubTraceEvent
|
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 {
|
|
|
|
return fmt.Errorf("error while marshaling event: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:42:18 +00:00
|
|
|
_, err = jtt.out.WriteString(string(jsonEvt) + "\n")
|
2021-09-15 11:41:03 +00:00
|
|
|
return err
|
|
|
|
}
|