2022-05-31 01:23:19 +08:00
|
|
|
// Unless explicitly stated otherwise all files in this repository are licensed
|
|
|
|
// under the Apache License Version 2.0.
|
|
|
|
// This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
|
|
// Copyright 2016-present Datadog, Inc.
|
|
|
|
|
|
|
|
package processor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2022-06-02 19:55:13 +08:00
|
|
|
"flashcat.cloud/categraf/logs/message"
|
2022-05-31 01:23:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const nanoToMillis = 1000000
|
|
|
|
|
|
|
|
// JSONEncoder is a shared json encoder.
|
|
|
|
var JSONEncoder Encoder = &jsonEncoder{}
|
|
|
|
|
|
|
|
// jsonEncoder transforms a message into a JSON byte array.
|
|
|
|
type jsonEncoder struct{}
|
|
|
|
|
|
|
|
// JSON representation of a message.
|
|
|
|
type jsonPayload struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
2022-06-10 08:47:04 +08:00
|
|
|
Hostname string `json:"agent_hostname"`
|
|
|
|
Service string `json:"fcservice"`
|
2022-06-02 16:52:56 +08:00
|
|
|
Source string `json:"fcsource"`
|
|
|
|
Tags string `json:"fctags"`
|
2022-05-31 01:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode encodes a message into a JSON byte array.
|
|
|
|
func (j *jsonEncoder) Encode(msg *message.Message, redactedMsg []byte) ([]byte, error) {
|
|
|
|
ts := time.Now().UTC()
|
|
|
|
if !msg.Timestamp.IsZero() {
|
|
|
|
ts = msg.Timestamp
|
|
|
|
}
|
|
|
|
return json.Marshal(jsonPayload{
|
|
|
|
Message: toValidUtf8(redactedMsg),
|
|
|
|
Status: msg.GetStatus(),
|
|
|
|
Timestamp: ts.UnixNano() / nanoToMillis,
|
|
|
|
Hostname: msg.GetHostname(),
|
|
|
|
Service: msg.Origin.Service(),
|
|
|
|
Source: msg.Origin.Source(),
|
|
|
|
Tags: msg.Origin.TagsToString(),
|
|
|
|
})
|
|
|
|
}
|