-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not update event in parallel #1428
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ type protocol struct { | |
timeout time.Duration | ||
compressLevel int | ||
|
||
// the beat name | ||
beat []byte | ||
|
||
eventsBuffer *bytes.Buffer | ||
} | ||
|
||
|
@@ -43,6 +46,7 @@ func newClientProcol( | |
conn net.Conn, | ||
timeout time.Duration, | ||
compressLevel int, | ||
beat string, | ||
) (*protocol, error) { | ||
|
||
// validate by creating and discarding zlib writer with configured level | ||
|
@@ -55,11 +59,17 @@ func newClientProcol( | |
w.Close() | ||
} | ||
|
||
encodedBeat, err := json.Marshal(beat) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protocol{ | ||
conn: conn, | ||
timeout: timeout, | ||
compressLevel: compressLevel, | ||
eventsBuffer: bytes.NewBuffer(nil), | ||
beat: encodedBeat, | ||
}, nil | ||
} | ||
|
||
|
@@ -263,7 +273,7 @@ func (p *protocol) serializeDataFrame( | |
// payloadLen (bytes): uint32 | ||
// payload: JSON document | ||
|
||
jsonEvent, err := json.Marshal(event) | ||
jsonEvent, err := serializeEvent(event, p.beat) | ||
if err != nil { | ||
debug("Fail to json encode event (%v): %#v", err, event) | ||
return err | ||
|
@@ -285,6 +295,49 @@ func (p *protocol) serializeDataFrame( | |
return nil | ||
} | ||
|
||
func serializeEvent(event common.MapStr, beat []byte) ([]byte, error) { | ||
buf := bytes.NewBuffer(nil) | ||
buf.WriteRune('{') | ||
|
||
if _, hasMeta := event["@metadata"]; !hasMeta { | ||
typ := event["type"].(string) | ||
|
||
buf.WriteString(`"@metadata":{"type":`) | ||
tmp, err := json.Marshal(typ) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf.Write(tmp) | ||
|
||
buf.WriteString(`,"beat":`) | ||
buf.Write(beat) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this also be passed through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. json.Marshal is done line 62. Didn't want to marshal over and over again |
||
buf.WriteString(`},`) | ||
} | ||
|
||
for k, v := range event { | ||
// append key | ||
tmp, err := json.Marshal(k) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf.Write(tmp) | ||
buf.WriteRune(':') | ||
|
||
// append value | ||
tmp, err = json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf.Write(tmp) | ||
buf.WriteRune(',') | ||
} | ||
|
||
b := buf.Bytes() | ||
b[len(b)-1] = '}' | ||
|
||
return b, nil | ||
} | ||
|
||
func writeUint32(out io.Writer, v uint32) error { | ||
return binary.Write(out, binary.BigEndian, v) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some unit tests for this function would be good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's tested (block-box testing sendEvents), as we've got some logstash unit tests forwarding events via sendEvents with server part decoding json messages for validation.