-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
encoder.go
47 lines (41 loc) · 1.18 KB
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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-2020 Datadog, Inc.
package processor
import (
"unicode"
"unicode/utf8"
"github.com/DataDog/datadog-agent/pkg/logs/message"
"github.com/DataDog/datadog-agent/pkg/util"
)
// Encoder turns a message into a raw byte array ready to be sent.
type Encoder interface {
Encode(msg *message.Message, redactedMsg []byte) ([]byte, error)
}
// toValidUtf8 ensures all characters are UTF-8.
func toValidUtf8(msg []byte) string {
if utf8.Valid(msg) {
return string(msg)
}
str := make([]rune, 0, len(msg))
for i := range msg {
r, size := utf8.DecodeRune(msg[i:])
if r == utf8.RuneError && size == 1 {
str = append(str, unicode.ReplacementChar)
} else {
str = append(str, r)
}
}
return string(str)
}
// getHostname returns the name of the host.
func getHostname() string {
hostname, err := util.GetHostname()
if err != nil {
// this scenario is not likely to happen since
// the agent can not start without a hostname
hostname = "unknown"
}
return hostname
}