categraf/logs/processor/proto.go

33 lines
966 B
Go
Raw Normal View History

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 (
"time"
2022-06-02 19:55:13 +08:00
"flashcat.cloud/categraf/logs/message"
"flashcat.cloud/categraf/logs/pb"
2022-05-31 01:23:19 +08:00
)
// ProtoEncoder is a shared proto encoder.
var ProtoEncoder Encoder = &protoEncoder{}
// protoEncoder transforms a message into a protobuf byte array.
type protoEncoder struct{}
// Encode encodes a message into a protobuf byte array.
func (p *protoEncoder) Encode(msg *message.Message, redactedMsg []byte) ([]byte, error) {
return (&pb.Log{
Message: toValidUtf8(redactedMsg),
Status: msg.GetStatus(),
Timestamp: time.Now().UTC().UnixNano(),
Hostname: msg.GetHostname(),
Service: msg.Origin.Service(),
Source: msg.Origin.Source(),
Tags: msg.Origin.Tags(),
}).Marshal()
}