Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 83bd012

Browse files
committed
pass a JSON encoded message as JSON, string as is
1 parent df8eb69 commit 83bd012

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"time"
67

@@ -136,6 +137,6 @@ func synthRecord(err error) Record {
136137
return Record{
137138
Command: "journald-cloudwatch-logs",
138139
Priority: ERROR,
139-
Message: err.Error(),
140+
Message: json.RawMessage(err.Error()),
140141
}
141142
}

record.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package main
22

3+
import "encoding/json"
4+
35
type Priority int
46

57
var (
@@ -25,28 +27,28 @@ var PriorityJSON = map[Priority][]byte{
2527
}
2628

2729
type Record struct {
28-
InstanceId string `json:"instanceId,omitempty"`
29-
TimeUsec int64 `json:"-"`
30-
PID int `json:"pid" journald:"_PID"`
31-
UID int `json:"uid" journald:"_UID"`
32-
GID int `json:"gid" journald:"_GID"`
33-
Command string `json:"cmdName,omitempty" journald:"_COMM"`
34-
Executable string `json:"exe,omitempty" journald:"_EXE"`
35-
CommandLine string `json:"cmdLine,omitempty" journald:"_CMDLINE"`
36-
SystemdUnit string `json:"systemdUnit,omitempty" journald:"_SYSTEMD_UNIT"`
37-
BootId string `json:"bootId,omitempty" journald:"_BOOT_ID"`
38-
MachineId string `json:"machineId,omitempty" journald:"_MACHINE_ID"`
39-
Hostname string `json:"hostname,omitempty" journald:"_HOSTNAME"`
40-
Transport string `json:"transport,omitempty" journald:"_TRANSPORT"`
41-
Priority Priority `json:"priority" journald:"PRIORITY"`
42-
Message string `json:"message" journald:"MESSAGE"`
43-
MessageId string `json:"messageId,omitempty" journald:"MESSAGE_ID"`
44-
Errno int `json:"machineId,omitempty" journald:"ERRNO"`
45-
Syslog RecordSyslog `json:"syslog,omitempty"`
46-
Kernel RecordKernel `json:"kernel,omitempty"`
47-
Container_Name string `json:"containerName,omitempty" journald:"CONTAINER_NAME"`
48-
Container_Tag string `json:"containerTag,omitempty" journald:"CONTAINER_TAG"`
49-
Container_ID string `json:"containerID,omitempty" journald:"CONTAINER_ID"`
30+
InstanceId string `json:"instanceId,omitempty"`
31+
TimeUsec int64 `json:"-"`
32+
PID int `json:"pid" journald:"_PID"`
33+
UID int `json:"uid" journald:"_UID"`
34+
GID int `json:"gid" journald:"_GID"`
35+
Command string `json:"cmdName,omitempty" journald:"_COMM"`
36+
Executable string `json:"exe,omitempty" journald:"_EXE"`
37+
CommandLine string `json:"cmdLine,omitempty" journald:"_CMDLINE"`
38+
SystemdUnit string `json:"systemdUnit,omitempty" journald:"_SYSTEMD_UNIT"`
39+
BootId string `json:"bootId,omitempty" journald:"_BOOT_ID"`
40+
MachineId string `json:"machineId,omitempty" journald:"_MACHINE_ID"`
41+
Hostname string `json:"hostname,omitempty" journald:"_HOSTNAME"`
42+
Transport string `json:"transport,omitempty" journald:"_TRANSPORT"`
43+
Priority Priority `json:"priority" journald:"PRIORITY"`
44+
Message json.RawMessage `json:"message" journald:"MESSAGE"`
45+
MessageId string `json:"messageId,omitempty" journald:"MESSAGE_ID"`
46+
Errno int `json:"machineId,omitempty" journald:"ERRNO"`
47+
Syslog RecordSyslog `json:"syslog,omitempty"`
48+
Kernel RecordKernel `json:"kernel,omitempty"`
49+
Container_Name string `json:"containerName,omitempty" journald:"CONTAINER_NAME"`
50+
Container_Tag string `json:"containerTag,omitempty" journald:"CONTAINER_TAG"`
51+
Container_ID string `json:"containerID,omitempty" journald:"CONTAINER_ID"`
5052
}
5153

5254
type RecordSyslog struct {

unmarshal.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"reflect"
67
"strconv"
8+
"strings"
79
"time"
810

911
"github.com/coreos/go-systemd/sdjournal"
@@ -54,6 +56,17 @@ func unmarshalRecord(journal *sdjournal.Journal, toVal reflect.Value) error {
5456
// the front, so we'll trim those off.
5557
value = value[len(jdKey)+1:]
5658

59+
if fieldType.Name() == "RawMessage" {
60+
if !strings.HasPrefix(value, `{"`) {
61+
value = `"` + value + `"`
62+
}
63+
// fix unwanted characters to JSON message
64+
value = strings.Replace(value, "\n", `\n`, -1)
65+
value = strings.Replace(value, "\t", `\t`, -1)
66+
fieldVal.SetBytes(json.RawMessage(value))
67+
continue
68+
}
69+
5770
switch fieldTypeKind {
5871
case reflect.Int:
5972
intVal, err := strconv.Atoi(value)

0 commit comments

Comments
 (0)