-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
message.go
67 lines (58 loc) · 2.01 KB
/
message.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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 2023-present Datadog, Inc.
package windowsevent
import (
"time"
"github.com/DataDog/datadog-agent/pkg/logs/message"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// Message implements StructedMessage interface for Windows Event Log messages.
type Message struct { //nolint:revive
data *Map
}
// Render renders the structured log information into JSON, for further encoding before
// being sent to the intake.
func (m *Message) Render() ([]byte, error) {
data, err := m.data.Json()
if err != nil {
return nil, err
}
log.Trace("Rendered JSON in structured message:", string(data))
return data, nil
}
// GetContent returns the content part of the structured log.
func (m *Message) GetContent() []byte {
message := m.data.GetMessage()
if message == "" {
log.Error("Message not containing any message")
return []byte{}
}
return []byte(message)
}
// SetContent sets the content part of the structured log.
func (m *Message) SetContent(content []byte) {
// we want to store it typed as a string for the json
// marshaling to properly marshal it as a string.
_ = m.data.SetMessage(string(content))
}
// MapToMessage packages a Map into either an unstructured message.Message or a structured one.
func MapToMessage(m *Map, source *sources.LogSource, processRawMessage bool) (*message.Message, error) {
// old behaviour using an unstructured message with raw data
if processRawMessage {
jsonEvent, err := m.Json()
if err != nil {
return nil, err
}
return message.NewMessageWithSource(jsonEvent, message.StatusInfo, source, time.Now().UnixNano()), nil
}
// new behaviour returning a structured message
return message.NewStructuredMessage(
&Message{data: m},
message.NewOrigin(source),
message.StatusInfo,
time.Now().UnixNano(),
), nil
}