http sender add raw protocal
@@ -19,9 +19,10 @@ import (
)
const (
- SendProtocalCSV = "csv"
- SendProtocalJson = "json"
- SendProtocalWholeJson = "body_json"
+ SendProtocolCSV = "csv"
+ SendProtocolJson = "json"
+ SendProtocolWholeJson = "body_json"
+ SendProtocolRaw = "raw"
)
var _ sender.SkipDeepCopySender = &Sender{}
@@ -53,15 +54,15 @@ func NewSender(c conf.MapConf) (sender.Sender, error) {
gZip, _ := c.GetBoolOr(sender.KeyHttpSenderGzip, true)
csvHead, _ := c.GetBoolOr(sender.KeyHttpSenderCsvHead, true)
csvSplit, _ := c.GetStringOr(sender.KeyHttpSenderCsvSplit, "\t")
- protocol, _ := c.GetStringOr(sender.KeyHttpSenderProtocol, SendProtocalJson)
+ protocol, _ := c.GetStringOr(sender.KeyHttpSenderProtocol, SendProtocolJson)
runnerName, _ := c.GetStringOr(KeyRunnerName, sender.UnderfinedRunnerName)
switch protocol {
- case SendProtocalCSV:
+ case SendProtocolCSV:
if csvSplit == "" {
csvSplit = "\t"
}
- case SendProtocalJson, SendProtocalWholeJson:
+ case SendProtocolJson, SendProtocolWholeJson, SendProtocolRaw:
default:
return nil, fmt.Errorf("runner[%v] create sender error, protocol %v is not support", runnerName, protocol)
}
@@ -84,18 +85,22 @@ func (h *Sender) Name() string {
func (h *Sender) Send(data []Data) (err error) {
var sendBytes []byte
switch h.protocol {
- case SendProtocalJson:
+ case SendProtocolJson:
if sendBytes, err = h.convertToJsonBytes(data); err != nil {
return err
}
- case SendProtocalCSV:
+ case SendProtocolCSV:
if sendBytes, err = h.convertToCsvBytes(data); err != nil {
return err
}
- case SendProtocalWholeJson:
+ case SendProtocolWholeJson:
if sendBytes, err = jsoniter.Marshal(data); err != nil {
return err
}
+ case SendProtocolRaw:
+ if sendBytes, err = h.convertToRawBytes(data); err != nil {
+ return err
+ }
default:
return fmt.Errorf("runner[%v] Sender[%v] send data error, protocol %v is not support", h.runnerName, h.Name(), h.protocol)
}
@@ -106,6 +111,25 @@ func (h *Sender) Close() error {
return nil
}
+func (h *Sender) convertToRawBytes(datas []Data) ([]byte, error) {
+ var buf bytes.Buffer
+ for _, data := range datas {
+ if data["raw"] == nil {
+ continue
+ }
+ switch newVal := data["raw"].(type) {
+ case string:
+ buf.WriteString(newVal)
+ case []byte:
+ buf.Write(newVal)
+ default:
+ return nil, fmt.Errorf("value[%v] expect as string or []byte,actual get %T", data["raw"], data["raw"])
+ }
+ buf.WriteString("\n")
+ }
+ return buf.Bytes(), nil
+}
+
func (h *Sender) convertToJsonBytes(datas []Data) (byteData []byte, err error) {
dataArray := make([]string, len(datas))
for i, data := range datas {
